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