2022-11-18 18:11:40 +00:00
|
|
|
//! Serde serialization and deserialization for nettext
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
mod de;
|
2022-11-18 13:15:30 +00:00
|
|
|
mod error;
|
|
|
|
mod ser;
|
|
|
|
|
2022-12-15 15:47:04 +00:00
|
|
|
pub use crate::BytesEncoding;
|
2022-11-18 18:11:40 +00:00
|
|
|
pub use de::{from_bytes, from_term, Deserializer};
|
2022-11-18 13:15:30 +00:00
|
|
|
pub use error::{Error, Result};
|
2022-11-18 15:26:44 +00:00
|
|
|
pub use ser::{to_bytes, to_term, Serializer};
|
2022-11-18 16:57:21 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-12-15 14:38:10 +00:00
|
|
|
use crate::debug;
|
2022-11-18 16:57:21 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
fn test_bidir<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(
|
|
|
|
input: T,
|
2022-12-15 14:36:41 +00:00
|
|
|
expected_concise: &str,
|
2022-11-18 16:57:21 +00:00
|
|
|
) {
|
2022-12-15 14:36:41 +00:00
|
|
|
eprintln!("Expecting: {}", expected_concise);
|
|
|
|
|
2022-11-18 16:57:21 +00:00
|
|
|
let ser = to_bytes(&input).unwrap();
|
2022-12-15 13:45:27 +00:00
|
|
|
let ser = debug(&ser);
|
|
|
|
eprintln!("Serialized: {}", ser);
|
|
|
|
assert_eq!(from_bytes::<T>(ser.as_bytes()).unwrap(), input);
|
2022-12-15 14:36:41 +00:00
|
|
|
|
|
|
|
let ser_concise = to_term(&input).unwrap().encode_concise();
|
|
|
|
let ser_concise = debug(&ser_concise);
|
|
|
|
eprintln!("Serialized (concise): {}", ser_concise);
|
|
|
|
assert_eq!(ser_concise, expected_concise);
|
|
|
|
assert_eq!(from_bytes::<T>(ser_concise.as_bytes()).unwrap(), input);
|
2022-12-15 15:47:04 +00:00
|
|
|
|
|
|
|
let ser_str_hex = input
|
|
|
|
.serialize(&mut Serializer {
|
|
|
|
string_format: BytesEncoding::Switch64 {
|
|
|
|
allow_whitespace: true,
|
|
|
|
},
|
|
|
|
bytes_format: BytesEncoding::Hex { split: true },
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.encode();
|
|
|
|
panic!("{}", debug(&ser_str_hex));
|
2022-11-18 16:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct() {
|
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
|
|
|
struct Test {
|
|
|
|
int: u32,
|
|
|
|
seq: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let input = Test {
|
|
|
|
int: 1,
|
|
|
|
seq: vec!["a".to_string(), "b".to_string()],
|
|
|
|
};
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"Test {int=1,seq=[YQ,Yg]}"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
2022-11-18 17:06:43 +00:00
|
|
|
|
|
|
|
let input = vec![
|
2022-11-18 17:59:06 +00:00
|
|
|
Test {
|
|
|
|
int: 1,
|
|
|
|
seq: vec!["a".to_string(), "b".to_string()],
|
|
|
|
},
|
|
|
|
Test {
|
|
|
|
int: 2,
|
|
|
|
seq: vec!["c".to_string(), "d".to_string()],
|
|
|
|
},
|
|
|
|
];
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"[Test {int=1,seq=[YQ,Yg]},Test {int=2,seq=[Yw,ZA]}]"#;
|
2022-11-18 17:06:43 +00:00
|
|
|
test_bidir(input, expected);
|
2022-11-18 16:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum() {
|
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
|
|
|
enum E {
|
|
|
|
Unit,
|
|
|
|
Newtype(u32),
|
|
|
|
Tuple(u32, u32),
|
|
|
|
Struct { a: u32 },
|
|
|
|
}
|
|
|
|
|
|
|
|
let input = E::Unit;
|
2022-12-15 13:45:27 +00:00
|
|
|
let expected = r#"E.Unit"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
|
|
|
|
let input = E::Newtype(1);
|
2022-12-15 13:45:27 +00:00
|
|
|
let expected = r#"E.Newtype 1"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
|
|
|
|
let input = E::Tuple(1, 2);
|
2022-12-15 13:45:27 +00:00
|
|
|
let expected = r#"E.Tuple 1 2"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
|
|
|
|
let input = E::Struct { a: 1 };
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"E.Struct {a=1}"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
|
|
|
|
let input = vec![
|
|
|
|
E::Unit,
|
|
|
|
E::Unit,
|
|
|
|
E::Newtype(1),
|
|
|
|
E::Tuple(1, 2),
|
|
|
|
E::Struct { a: 1 },
|
|
|
|
E::Tuple(3, 2),
|
|
|
|
];
|
2022-12-15 14:38:10 +00:00
|
|
|
let expected = r#"[E.Unit,E.Unit,E.Newtype 1,E.Tuple 1 2,E.Struct {a=1},E.Tuple 3 2]"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_seq1() {
|
|
|
|
let input = (1, 2, 3, 4);
|
2022-12-15 13:45:27 +00:00
|
|
|
let expected = r#"1 2 3 4"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-15 13:45:27 +00:00
|
|
|
fn test_list() {
|
|
|
|
let input = vec![1, 2, 3, 4];
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"[1,2,3,4]"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-15 13:45:27 +00:00
|
|
|
fn test_seqlist() {
|
|
|
|
let input = vec![(1, 2), (2, 3), (3, 4), (5, 6)];
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"[1 2,2 3,3 4,5 6]"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dict() {
|
|
|
|
let mut input = HashMap::new();
|
|
|
|
input.insert("hello".to_string(), "world".to_string());
|
|
|
|
input.insert("dont".to_string(), "panic".to_string());
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"{ZG9udA=cGFuaWM,aGVsbG8=d29ybGQ}"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
|
|
|
|
let mut input = HashMap::new();
|
|
|
|
input.insert(12, vec![42, 125]);
|
|
|
|
input.insert(33, vec![19, 22, 21]);
|
2022-12-15 14:36:41 +00:00
|
|
|
let expected = r#"{12=[42,125],33=[19,22,21]}"#;
|
2022-11-18 16:57:21 +00:00
|
|
|
test_bidir(input, expected);
|
|
|
|
}
|
|
|
|
}
|