1
#[test]
2
4
fn test_serde_bytes() {
3
8
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
4
    #[serde(rename = "b")]
5
    struct BytesVal {
6
        pub b: serde_bytes::ByteBuf,
7
    }
8

            
9
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
10
    #[serde(untagged)]
11
    enum Bad {
12
        Bytes(BytesVal),
13
    }
14

            
15
4
    let s = ron::to_string(&serde_bytes::Bytes::new(b"test")).unwrap();
16
4

            
17
4
    assert_eq!(s, r#"b"test""#);
18

            
19
4
    let v: Bad = ron::from_str(r#"(b: b"test")"#).unwrap();
20
4

            
21
4
    assert_eq!(
22
4
        format!("{:?}", v),
23
4
        "Bytes(BytesVal { b: [116, 101, 115, 116] })"
24
4
    );
25

            
26
4
    let s = ron::to_string(&v).unwrap();
27
4

            
28
4
    assert_eq!(s, r#"(b:b"test")"#);
29
4
}
30

            
31
#[test]
32
4
fn test_bytes() {
33
8
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
34
    #[serde(rename = "b")]
35
    struct BytesVal {
36
        pub b: bytes::Bytes,
37
    }
38

            
39
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
40
    #[serde(untagged)]
41
    enum Bad {
42
        Bytes(BytesVal),
43
    }
44

            
45
4
    let s = ron::to_string(&bytes::Bytes::from("test")).unwrap();
46
4

            
47
4
    assert_eq!(s, r#"b"test""#);
48

            
49
4
    let v: Bad = ron::from_str(r#"(b: b"test")"#).unwrap();
50
4

            
51
4
    assert_eq!(format!("{:?}", v), r#"Bytes(BytesVal { b: b"test" })"#);
52

            
53
4
    let s = ron::to_string(&v).unwrap();
54
4

            
55
4
    assert_eq!(s, r#"(b:b"test")"#);
56
4
}
57

            
58
#[test]
59
4
fn test_strongly_typed_base64() {
60
    use base64::engine::{general_purpose::STANDARD as BASE64, Engine};
61

            
62
    enum Base64 {}
63

            
64
    impl Base64 {
65
4
        fn serialize<S: serde::Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
66
4
            serializer.serialize_str(&BASE64.encode(data))
67
4
        }
68

            
69
4
        fn deserialize<'de, D: serde::Deserializer<'de>>(
70
4
            deserializer: D,
71
4
        ) -> Result<Vec<u8>, D::Error> {
72
4
            let base64_str: &str = serde::Deserialize::deserialize(deserializer)?;
73
4
            BASE64.decode(base64_str).map_err(serde::de::Error::custom)
74
4
        }
75
    }
76

            
77
12
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
78
    #[serde(rename = "b")]
79
    struct BytesVal {
80
        #[serde(with = "Base64")]
81
        pub b: Vec<u8>,
82
    }
83

            
84
    #[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
85
    #[serde(untagged)]
86
    enum Bad {
87
        Bytes(BytesVal),
88
    }
89

            
90
4
    let v: Bad = ron::from_str(r#"(b: "dGVzdA==")"#).unwrap();
91
4

            
92
4
    assert_eq!(
93
4
        v,
94
4
        Bad::Bytes(BytesVal {
95
4
            b: b"test".to_vec()
96
4
        })
97
4
    );
98

            
99
4
    let s = ron::to_string(&v).unwrap();
100
4

            
101
4
    assert_eq!(s, r#"(b:"dGVzdA==")"#);
102
4
}