Skip to main content

tysonscript_object_notation/
lib.rs

1use serde_core::{Deserialize, Serialize};
2
3use crate::{de::TsonDeserializer, ser::TsonSerializer};
4
5pub mod de;
6pub mod ser;
7
8pub fn to_string<T: Serialize>(value: &T) -> Result<String, crate::ser::Error> {
9    let mut serializer = TsonSerializer::new(Vec::new());
10    value.serialize(&mut serializer)?;
11    Ok(unsafe { String::from_utf8_unchecked(serializer.into_inner()) })
12}
13
14pub fn from_str<'a, T: Deserialize<'a>>(str: &'a str) -> Result<T, crate::de::Error> {
15    let mut deserializer = TsonDeserializer::new(str);
16    T::deserialize(&mut deserializer)
17}
18
19#[cfg(test)]
20mod tests {
21    use std::{collections::HashMap, net::SocketAddr};
22
23    use serde::{Deserialize, Serialize};
24
25    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
26    #[serde(rename_all = "lowercase")]
27    enum ServiceType {
28        Auth,
29        Main { auth_service: String },
30    }
31
32    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
33    enum Rgb {
34        Rgb(u8, u8, u8),
35        Recurse(Box<Rgb>),
36    }
37
38    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
39    struct ColorWrapper(Rgb);
40
41    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
42    struct EmbeddingModel {
43        url: String,
44        model: String,
45    }
46
47    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
48    struct Check;
49
50    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
51    struct Cli {
52        log_level: String,
53        addr: SocketAddr,
54        db_url: Option<String>,
55        notes: String,
56        wtf: (),
57        embedding_model: EmbeddingModel,
58        vibe: Check,
59        reembed: bool,
60        service_type: ServiceType,
61        other_service: ServiceType,
62        color: Rgb,
63        wrapped_color: ColorWrapper,
64        more_colors: Vec<(Rgb, u8, Rgb)>,
65        secret: Vec<u8>,
66        cipher: Vec<Vec<u8>>,
67        map: HashMap<String, Rgb>,
68        true_map: HashMap<u64, bool>,
69    }
70
71    #[test]
72    fn integration_test() {
73        let mut map = HashMap::new();
74        map.insert("red".to_owned(), Rgb::Rgb(255, 0, 0));
75        map.insert("green".to_owned(), Rgb::Rgb(0, 255, 0));
76        map.insert("blue".to_owned(), Rgb::Rgb(0, 0, 255));
77        let mut true_map = HashMap::new();
78        true_map.insert(42, true);
79        true_map.insert(67, false);
80        true_map.insert(69, true);
81        let to_serialize = Cli {
82            log_level: String::from("info"),
83            addr: "0.0.0.0:8080".parse().unwrap(),
84            // db_url: Some(String::from("postgres:///retro_game_exchange?host=/var/run/postgresql")),
85            db_url: None,
86            notes: String::from(
87                "Captains log:\nmaking the dumbest shit imaginable.\n\nWhy? Who knows? I don't",
88            ),
89            wtf: (),
90            embedding_model: EmbeddingModel {
91                url: String::from("http://host.docker.internal/v1"),
92                model: String::from("embeddinggemma-vllm"),
93            },
94            vibe: Check,
95            reembed: true,
96            service_type: ServiceType::Main {
97                auth_service: String::from("web-auth"),
98            },
99            other_service: ServiceType::Auth,
100            color: Rgb::Rgb(127, 255, 100),
101            wrapped_color: ColorWrapper(Rgb::Rgb(5, 6, 7)),
102            more_colors: vec![(Rgb::Rgb(1, 2, 3), 5, Rgb::Rgb(4, 5, 6)); 2],
103            secret: vec![14, 6, 7, 6, 87, 69, 78, 5, 6, 4, 64, 6, 45, 6],
104            cipher: vec![vec![1, 2, 3], vec![4, 5, 6, 7, 8, 9], vec![10]],
105            map,
106            true_map,
107        };
108        let string = crate::to_string(&to_serialize).unwrap();
109        println!("{}", string);
110        let from_string: Cli = crate::from_str(string.as_str()).unwrap();
111        println!("{:#?}", from_string);
112
113        assert_eq!(to_serialize, from_string);
114    }
115}