Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow trailing characters to fit in the last field #197

Closed

Conversation

dragonnn
Copy link
Contributor

@dragonnn dragonnn commented Jan 8, 2024

With #182 merged it is responsable to assume a response can have a leading , at it end. Example response:

+QIRD: 69\r\n\x01 @\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x87,

I think it should be fine to completely remove this check for all cases, if that is not desired I can make it use the flag is_trailing_parsing had happen and then check if d.end() should be run.

@dragonnn
Copy link
Contributor Author

I hit more issues with parsing data from QIRD by some bytes getting interpreted as whitespace, I think it is not worth fixing it in that way in serde_at.
I worked around it by doing a custom serde parser:


#[derive(Clone, Debug, Deserialize)]
pub struct ReceivedData {
    #[serde(deserialize_with = "raw_bytes")]
    pub data: (usize, Box<[u8]>),
}

impl atat::AtatResp for ReceivedData {}

pub fn raw_bytes<'de, D>(deserializer: D) -> Result<(usize, Box<[u8]>), D::Error>
where
    D: Deserializer<'de>,
{
    struct __Visitor<'de> {
        marker: serde::__private::PhantomData<(usize, Box<[u8]>)>,
        lifetime: serde::__private::PhantomData<&'de ()>,
    }
    impl<'de> serde::de::Visitor<'de> for __Visitor<'de> {
        type Value = (usize, Box<[u8]>);
        fn expecting(&self, __formatter: &mut serde::__private::Formatter) -> serde::__private::fmt::Result {
            serde::__private::Formatter::write_str(__formatter, " Box<[u8]>")
        }

        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match v.split_once(|x| x == &b'\r') {
                Some((size, data)) => {
                    let size = core::str::from_utf8(size).unwrap();
                    let size = size.parse::<usize>().unwrap();
                    Ok((size, Box::from(&data[1..])))
                }
                None => {
                    if !v.is_empty() && v[0] == b'0' {
                        Ok((0, Box::from([])))
                    } else {
                        Err(E::invalid_value(Unexpected::Bytes(v), &self))
                    }
                }
            }
        }
    }

    deserializer.deserialize_bytes(__Visitor {
        marker: serde::__private::PhantomData::<(usize, Box<[u8]>)>,
        lifetime: serde::__private::PhantomData,
    })
}

@dragonnn dragonnn closed this Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant