esexpr_binary/
lib.rs

1//! Binary format for `ESExpr`.
2#![no_std]
3
4extern crate alloc;
5extern crate core;
6#[cfg(feature = "std")]
7extern crate std;
8
9mod append_only_string_list;
10
11mod async_macros;
12mod format;
13/// IO Traits for `ESExpr`.
14pub mod io;
15mod reader;
16mod writer;
17
18pub use reader::*;
19pub use writer::{GeneratorError, ExprGenerator, ExprGeneratorAsync, ExprGeneratorSync};
20
21#[cfg(test)]
22mod test {
23	use alloc::borrow::Cow;
24	use alloc::vec::Vec;
25	use core::convert::Infallible;
26	use core::str::FromStr;
27
28	use esexpr::{ESExpr, ESExprCodec};
29	use num_bigint::{BigInt, BigUint};
30
31	use super::{ExprGenerator, ExprGeneratorSync, ExprParserSync, parse_sync};
32
33	#[test]
34	fn encode_int() {
35		fn check(n: &str, mut enc: &[u8]) {
36			let n = BigUint::from_str(n).unwrap();
37
38			let mut buff: Vec<u8> = Vec::new();
39
40			let mut eg = ExprGenerator::<_, Infallible>::new(&mut buff);
41			eg.generate(&ESExpr::Int(Cow::Owned(BigInt::from(n.clone())))).unwrap();
42
43			assert_eq!(enc, &buff);
44
45			let m = BigUint::decode_esexpr(parse_sync::<Infallible>(&mut enc).read_next_expr().unwrap()).unwrap();
46			assert_eq!(n, m);
47		}
48
49		check("4", &[0x24]);
50		check(
51			"9223372036854775807",
52			&[0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07],
53		);
54		check(
55			"18446744073709551615",
56			&[0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F],
57		);
58		check(
59			"12345678901234567890",
60			&[0x32, 0xAD, 0xE1, 0xC7, 0xF5, 0x8C, 0xD3, 0xD2, 0xDA, 0x0A],
61		);
62		check(
63			"98765432109876543210",
64			&[0x3A, 0xEE, 0xCF, 0xC9, 0xF2, 0xB8, 0x9A, 0x95, 0xD5, 0x55],
65		);
66	}
67}