vdf_serde_format/
lib.rs

1//! Since none of the VDF implementations I tried on crates.io worked for my intended purposes, I wrote my own.
2//!
3//! Considering that this is a very badly documented data format, some data types (such as booleans) were implemented
4//! in a way that looks compatible with the format (much like an extension, in case it was not intended).
5//!
6//! # Usage
7//!
8//! ```rust
9//! use vdf_serde::{from_str, to_string};
10//! use serde::{Deserialize, Serialize};
11//! use std::collections::HashMap;
12//!
13//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
14//! struct Data {
15//!     name: String,
16//!     list_str: Vec<String>,
17//!     map: std::collections::HashMap<String, i64>,
18//! }
19//!
20//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
21//! struct Test {
22//!     int: u32,
23//!     seq: Vec<Data>,
24//! }
25//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
26//! struct TestContainer {
27//!     test: Test,
28//! }
29//!
30//! fn main() {
31//!     let test = Test {
32//!         int: 1,
33//!         seq: vec![
34//!             Data {
35//!                 name: "Better VDF".to_string(),
36//!                 list_str: vec![
37//!                     "value1".to_string(),
38//!                     "value2".to_string(),
39//!                     "value3".to_string(),
40//!                 ],
41//!                 map: [
42//!                     ("zbx".to_string(), 12318293),
43//!                     ("thc".to_string(), -12393180),
44//!                 ]
45//!                 .iter()
46//!                 .cloned()
47//!                 .collect(),
48//!             },
49//!             Data {
50//!                 name: "rrrrr".to_string(),
51//!                 list_str: vec![
52//!                     "1243".to_string(),
53//!                     "sadferw".to_string(),
54//!                     "batebt".to_string(),
55//!                 ],
56//!                 map: vec![("abc".to_string(), 444444), ("key".to_string(), -555555)]
57//!                     .iter()
58//!                     .cloned()
59//!                     .collect(),
60//!             },
61//!         ],
62//!     };
63//!     // Serialize it.
64//!     let result = TestContainer { test };
65//!     let result_str = to_string(&result).unwrap();
66//!     println!("Result:\n{}", result_str);
67//!     
68//!     // Deserialize it.
69//!     let deserialized: TestContainer = from_str(&result_str).unwrap();
70//!     println!("{:#?}", deserialized);
71//!     assert_eq!(result, deserialized);
72//! }
73//! ```
74
75mod deserializer;
76mod error;
77mod preprocessor;
78mod serializer;
79
80pub use deserializer::{from_str, Deserializer};
81pub use error::{Error, Result};
82pub use serializer::{to_string, Serializer};
83
84pub(crate) use preprocessor::{peek_expect_char, preprocess};
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use serde::{Deserialize, Serialize};
90
91    #[test]
92    fn it_works() {
93        #[derive(Serialize, Deserialize, Debug, PartialEq)]
94        struct Data {
95            name: String,
96            list_str: Vec<String>,
97            map: std::collections::HashMap<String, i64>,
98        }
99
100        #[derive(Serialize, Deserialize, Debug, PartialEq)]
101        struct Test {
102            int: u32,
103            seq: Vec<Data>,
104        }
105        #[derive(Serialize, Deserialize, Debug, PartialEq)]
106        struct TestContainer {
107            test: Test,
108        }
109
110        let test = Test {
111            int: 1,
112            seq: vec![
113                Data {
114                    name: "Better VDF".to_string(),
115                    list_str: vec![
116                        "value1".to_string(),
117                        "value2".to_string(),
118                        "value3".to_string(),
119                    ],
120                    map: [
121                        ("zbx".to_string(), 12318293),
122                        ("thc".to_string(), -12393180),
123                    ]
124                    .iter()
125                    .cloned()
126                    .collect(),
127                },
128                Data {
129                    name: "rrrrr".to_string(),
130                    list_str: vec![
131                        "1243".to_string(),
132                        "sadferw".to_string(),
133                        "batebt".to_string(),
134                    ],
135                    map: vec![("abc".to_string(), 444444), ("key".to_string(), -555555)]
136                        .iter()
137                        .cloned()
138                        .collect(),
139                },
140            ],
141        };
142        // Serialize it.
143        let result = TestContainer { test };
144        let result_str = to_string(&result).unwrap();
145        println!("Result:\n{}", result_str);
146
147        // Deserialize it.
148        let deserialized: TestContainer = from_str(&result_str).unwrap();
149        println!("{:#?}", deserialized);
150        assert_eq!(result, deserialized);
151    }
152}