pub mod boundary; pub mod domain; pub mod error; pub mod float; pub mod hex; pub mod packed; pub mod reader; pub mod repr; pub mod signed_integer; pub mod source; pub mod text; pub mod types; pub mod writer; pub use domain::*; pub use packed::PackedReader; pub use packed::PackedWriter; pub use packed::annotated_from_bytes; pub use packed::annotated_iovalue_from_bytes; pub use packed::from_bytes; pub use packed::iovalue_from_bytes; pub use reader::Reader; pub use repr::Annotations; pub use repr::ArcValue; pub use repr::Atom; pub use repr::Bytes; pub use repr::Embedded; pub use repr::IOValue; pub use repr::Map; pub use repr::PlainValue; pub use repr::NoValue; pub use repr::Record; pub use repr::Set; pub use repr::Symbol; pub use repr::Value; pub use repr::copy_via; pub use repr::iovalue; pub use repr::owned; pub use repr::value; pub use signed_integer::SignedInteger; pub use source::BinarySource; pub use source::BytesBinarySource; pub use source::IOBinarySource; pub use text::TextReader; pub use text::TextWriter; pub use text::annotated_from_str; pub use text::annotated_iovalue_from_str; pub use text::from_str; pub use text::iovalue_from_str; pub use types::AtomClass; pub use types::CompoundClass; pub use types::ValueClass; pub use writer::Writer; pub use writer::write_value; #[cfg(test)] mod demo { use crate::*; fn getit<'a>(d: &'a dyn Value, k: &str) -> Option<&'a dyn Value> { d.get(&k) } #[test] fn a() { let l: PlainValue = "label".parse().unwrap(); let r = Record::new(l.value_clone(), vec![owned(1), owned(2), owned(3)]); let r2 = Record::new(l, vec![owned(1), owned(2), owned(4)]); let mut v: Map, PlainValue> = Map::new(); v.insert("\"abc\"".parse().unwrap(), "def".parse().unwrap()); v.insert("abc".parse().unwrap(), "DEF".parse().unwrap()); v.insert(owned(123), "xyz".parse().unwrap()); v.insert(owned(vec![1, 2, 3]), "{a: 1, b: 2}".parse().unwrap()); v.insert(owned(r2), "bbb".parse().unwrap()); v.insert(owned(r), "".parse().unwrap()); let w: &dyn Value = &v; println!("GETw abc {:?}", w.get(&"abc")); println!("GETw 123 {:?}", w.get(&123)); println!("GETw qqq {:?}", w.get(&"qqq")); println!("GETv abc {:?}", v.get(value(&"abc"))); println!("GETv 123 {:?}", v.get(value(&123))); println!("GETv qqq {:?}", v.get(value(&"qqq"))); for (kk, vv) in w.entries() { println!("{:#?} ==> {:#?}", kk, vv); } // { // use std::io::BufRead; // for line in std::io::stdin().lock().lines() { // let line = line.unwrap(); // let key = line.parse::>().unwrap(); // let val = w.get(&key); // println!("{:?} == {:?} ==> {:?} == {:?}", line, &key, val, getit(&v, &line)); // } // } } } #[cfg(test)] mod test_domain { use std::io; use crate::*; #[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)] pub enum Dom { One, Two, } impl Domain for Dom { type Decode = DebugDomainCodec; type Encode = DebugDomainCodec; } impl std::str::FromStr for Dom { type Err = io::Error; fn from_str(s: &str) -> Result { match s { "One" => Ok(Dom::One), "Two" => Ok(Dom::Two), _ => Err(io::Error::new(io::ErrorKind::Other, "cannot parse preserves test domain")), } } } struct DomCodec; impl DomainDecode for DomCodec { fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, _read_annotations: bool, ) -> io::Result { let v = r.next_iovalue(false)?; if v.as_bytestring().is_some() { Ok(Dom::One) } else { Ok(Dom::Two) } } } impl DomainEncode for DomCodec { fn encode_embedded( &mut self, w: &mut dyn Writer, d: &Dom, ) -> io::Result<()> { match d { Dom::One => Bytes::new(vec![255, 255, 255, 255]).write(w, self), Dom::Two => Symbol::new(&format!("Dom::{:?}", d)).write(w, self), } } } fn dom_as_preserves(v: &Dom) -> io::Result> { Ok(match v { Dom::One => owned(Bytes::new(vec![255, 255, 255, 255])), Dom::Two => owned(Symbol::new(format!("Dom::{:?}", v))), }) } #[test] fn test_one() { let v = owned(vec![owned(1), owned(Embedded::new(Dom::One)), owned(2)]); assert_eq!(PackedWriter::encode_iovalue(&iovalue(copy_via(&v, &mut dom_as_preserves).unwrap())).unwrap(), [0xb5, 0x91, 0xb2, 0x04, 255, 255, 255, 255, 0x92, 0x84]); } #[test] fn test_two() { let v = owned(vec![owned(1), owned(Embedded::new(Dom::Two)), owned(2)]); assert_eq!(PackedWriter::encode_iovalue(&iovalue(copy_via(&v, &mut dom_as_preserves).unwrap())).unwrap(), [0xb5, 0x91, 0xb3, 0x08, 68, 111, 109, 58, 58, 84, 119, 111, 0x92, 0x84]); } }