serde_unit_struct/
lib.rs

1//! Copyright (c) 2022 Chris Riches
2//! (Licensed under MIT or Apache 2.0)
3//!
4//! (De)serialize a unit struct as its name.
5//! ```
6//! use serde_unit_struct::{Deserialize_unit_struct, Serialize_unit_struct};
7//!
8//! #[derive(Deserialize_unit_struct, Serialize_unit_struct)]
9//! struct Foo;
10//! ```
11
12#![no_std]
13
14pub use serde_unit_struct_derive::{Deserialize_unit_struct, Serialize_unit_struct};
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    use serde::{Deserialize, Serialize};
21
22    #[derive(Serialize_unit_struct, Deserialize_unit_struct)]
23    struct Foo;
24
25    #[derive(Serialize_unit_struct, Deserialize_unit_struct)]
26    struct Bar;
27
28    #[derive(Serialize, Deserialize)]
29    struct Config {
30        foo: Foo,
31        bar: Bar,
32    }
33
34    impl Config {
35        pub fn new() -> Self {
36            Self { foo: Foo, bar: Bar }
37        }
38    }
39
40    #[test]
41    fn serialize() {
42        let foo = serde_json::to_string(&Foo).expect("Serializing Foo failed");
43        assert_eq!(&foo, "\"Foo\"");
44
45        let bar = serde_json::to_string(&Bar).expect("Serializing Bar failed");
46        assert_eq!(&bar, "\"Bar\"");
47    }
48
49    #[test]
50    fn deserialize() {
51        let _foo = serde_json::from_str::<Foo>("\"Foo\"").expect("Deserializing Foo failed");
52        let _bar = serde_json::from_str::<Bar>("\"Bar\"").expect("Deserializing Bar failed");
53    }
54
55    #[test]
56    fn bad_deserialize() {
57        let foo_err = serde_json::from_str::<Foo>("\"Bar\"");
58        assert!(foo_err.is_err());
59        let bar_err = serde_json::from_str::<Bar>("\"Foo\"");
60        assert!(bar_err.is_err());
61    }
62
63    #[test]
64    fn config_bson() {
65        let conf = Config::new();
66        let bsn = bson::to_raw_document_buf(&conf).expect("Serializing config to bson failed");
67        bson::from_slice::<Config>(bsn.as_bytes()).expect("Deserializing config from bson failed");
68    }
69
70    #[test]
71    fn config_csv() {
72        let conf = Config::new();
73        let mut buf = [0u8; 128];
74        let mut writer = csv::Writer::from_writer(&mut buf[..]);
75        writer
76            .serialize(&conf)
77            .expect("Serializing config to csv failed");
78        drop(writer);
79        let mut reader = csv::Reader::from_reader(&buf[..]).into_deserialize::<Config>();
80        reader
81            .next()
82            .unwrap()
83            .expect("Deserializing config from csv failed");
84    }
85
86    #[test]
87    fn config_csv_bad() {
88        let csv_str = "foo,bar\nBar,Foo";
89        let mut reader = csv::Reader::from_reader(csv_str.as_bytes()).into_deserialize::<Config>();
90        assert!(reader.next().unwrap().is_err());
91    }
92
93    #[test]
94    fn config_postcard() {
95        let conf = Config::new();
96        let mut buf = [0u8; 128];
97        let pc =
98            postcard::to_slice(&conf, &mut buf).expect("Serializing config to postcard failed");
99        postcard::from_bytes::<Config>(pc).expect("Deserializing config from postcard failed");
100    }
101
102    #[test]
103    fn config_json() {
104        let conf = Config::new();
105        let json = serde_json::to_string(&conf).expect("Serializing config to json failed");
106        serde_json::from_str::<Config>(&json).expect("Deserializing config from json failed");
107    }
108
109    #[test]
110    fn config_json_bad() {
111        let json = "{\"foo\": \"foo\", \"bar\": \"foo\"}";
112        let err = serde_json::from_str::<Config>(json);
113        assert!(err.is_err());
114    }
115
116    #[test]
117    fn config_pickle() {
118        let conf = Config::new();
119        let ser_options = serde_pickle::SerOptions::new();
120        let pickle =
121            serde_pickle::to_vec(&conf, ser_options).expect("Serializing config to pickle failed");
122        let de_options = serde_pickle::DeOptions::new();
123        serde_pickle::from_slice::<Config>(&pickle, de_options)
124            .expect("Deserializing config from pickle failed");
125    }
126
127    #[test]
128    fn config_yaml() {
129        let conf = Config::new();
130        let yaml = serde_yaml::to_string(&conf).expect("Serializing config to yaml failed");
131        serde_yaml::from_str::<Config>(&yaml).expect("Deserializing config from yaml failed");
132    }
133
134    #[test]
135    fn config_yaml_bad() {
136        let yaml = "foo: Foo\nbar: 3";
137        let err = serde_yaml::from_str::<Config>(yaml);
138        assert!(err.is_err());
139    }
140
141    #[test]
142    fn config_toml() {
143        let conf = Config::new();
144        let tml = toml::to_string(&conf).expect("Serializing config to toml failed");
145        toml::from_str::<Config>(&tml).expect("Deserializing config from toml failed");
146    }
147
148    #[test]
149    fn config_toml_bad() {
150        let tml = "foo = \"Apple\"\nbar = \"Banana\"";
151        let err = toml::from_str::<Config>(tml);
152        assert!(err.is_err());
153    }
154}