Skip to main content

boox_note_parser/
extra.rs

1use crate::error::Result;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct ExtraMetadata {
5    pub flag: u32,
6    pub app_build: u32,
7    pub key: String,
8    pub value: String,
9}
10
11impl ExtraMetadata {
12    pub fn read(mut reader: impl std::io::Read) -> Result<Self> {
13        let extra = protobuf::Extra::read(&mut reader)?;
14        Ok(Self {
15            flag: extra.flag,
16            app_build: extra.app_build,
17            key: extra.key,
18            value: extra.value,
19        })
20    }
21}
22
23mod protobuf {
24    use prost::Message;
25
26    use crate::error::Result;
27
28    #[derive(Clone, PartialEq, Message)]
29    pub struct Extra {
30        #[prost(uint32, tag = "1")]
31        pub flag: u32,
32        #[prost(uint32, tag = "2")]
33        pub app_build: u32,
34        #[prost(string, tag = "3")]
35        pub key: String,
36        #[prost(string, tag = "4")]
37        pub value: String,
38    }
39
40    impl Extra {
41        pub fn read(mut reader: impl std::io::Read) -> Result<Self> {
42            let mut buf = Vec::new();
43            reader.read_to_end(&mut buf)?;
44            Ok(Extra::decode(&buf[..])?)
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use std::io::Cursor;
52
53    use super::ExtraMetadata;
54
55    #[test]
56    fn decodes_extra_metadata() {
57        let bytes = [
58            0x08, 0x01, 0x10, 0xDA, 0xCE, 0x02, 0x1A, 0x0A, 0x73, 0x68, 0x61, 0x72, 0x65, 0x5F,
59            0x75, 0x73, 0x65, 0x72, 0x22, 0x20, 0x37, 0x61, 0x39, 0x36, 0x30, 0x63, 0x61, 0x37,
60            0x35, 0x33, 0x62, 0x30, 0x34, 0x32, 0x30, 0x65, 0x61, 0x32, 0x64, 0x35, 0x62, 0x38,
61            0x38, 0x64, 0x35, 0x37, 0x66, 0x37, 0x62, 0x66, 0x36, 0x32,
62        ];
63        let extra = ExtraMetadata::read(Cursor::new(bytes)).unwrap();
64
65        assert_eq!(extra.flag, 1);
66        assert_eq!(extra.app_build, 42842);
67        assert_eq!(extra.key, "share_user");
68        assert_eq!(extra.value, "7a960ca753b0420ea2d5b88d57f7bf62");
69    }
70}