2022-11-03 16:17:31 +00:00
|
|
|
pub mod boundary;
|
|
|
|
pub mod domain;
|
|
|
|
pub mod error;
|
2022-10-31 20:21:28 +00:00
|
|
|
pub mod float;
|
2022-11-03 20:12:15 +00:00
|
|
|
pub mod hex;
|
|
|
|
pub mod packed;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub mod reader;
|
|
|
|
pub mod repr;
|
2022-10-31 20:21:28 +00:00
|
|
|
pub mod signed_integer;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub mod source;
|
2022-11-04 09:04:48 +00:00
|
|
|
pub mod text;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub mod types;
|
|
|
|
pub mod writer;
|
|
|
|
|
2022-11-03 16:48:44 +00:00
|
|
|
pub use domain::*;
|
2022-11-03 20:12:15 +00:00
|
|
|
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;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub use reader::Reader;
|
|
|
|
pub use repr::Annotations;
|
|
|
|
pub use repr::Atom;
|
|
|
|
pub use repr::Bytes;
|
|
|
|
pub use repr::Embedded;
|
|
|
|
pub use repr::IOValue;
|
|
|
|
pub use repr::Map;
|
2022-11-04 09:04:48 +00:00
|
|
|
pub use repr::PlainValue;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub use repr::NoValue;
|
|
|
|
pub use repr::Record;
|
|
|
|
pub use repr::Set;
|
|
|
|
pub use repr::Symbol;
|
|
|
|
pub use repr::Value;
|
2022-11-04 14:24:01 +00:00
|
|
|
pub use repr::copy_via;
|
|
|
|
pub use repr::iovalue;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub use repr::owned;
|
|
|
|
pub use repr::value;
|
2022-10-31 20:21:28 +00:00
|
|
|
pub use signed_integer::SignedInteger;
|
2022-11-03 16:48:44 +00:00
|
|
|
pub use source::BinarySource;
|
|
|
|
pub use source::BytesBinarySource;
|
|
|
|
pub use source::IOBinarySource;
|
2022-11-04 09:04:48 +00:00
|
|
|
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;
|
2022-11-03 16:17:31 +00:00
|
|
|
pub use types::AtomClass;
|
|
|
|
pub use types::CompoundClass;
|
|
|
|
pub use types::ValueClass;
|
|
|
|
pub use writer::Writer;
|
2022-11-04 09:04:48 +00:00
|
|
|
pub use writer::write_value;
|
2022-10-31 20:21:28 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod demo {
|
|
|
|
use crate::*;
|
|
|
|
|
2022-11-02 12:34:39 +00:00
|
|
|
fn getit<'a>(d: &'a dyn Value<NoValue>, k: &str) -> Option<&'a dyn Value<NoValue>> {
|
2022-11-02 15:26:21 +00:00
|
|
|
d.get(&k)
|
2022-11-02 12:34:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 20:21:28 +00:00
|
|
|
#[test] fn a() {
|
2022-11-04 09:04:48 +00:00
|
|
|
let l: PlainValue<NoValue> = "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)]);
|
2022-11-02 16:03:38 +00:00
|
|
|
|
2022-11-04 09:04:48 +00:00
|
|
|
let mut v: Map<PlainValue<NoValue>, PlainValue<NoValue>> = 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), "<foo bar zot>".parse().unwrap());
|
2022-10-31 20:21:28 +00:00
|
|
|
let w: &dyn Value<NoValue> = &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")));
|
2022-11-02 16:03:38 +00:00
|
|
|
for (kk, vv) in w.entries() {
|
2022-11-04 09:04:48 +00:00
|
|
|
println!("{:#?} ==> {:#?}", kk, vv);
|
2022-10-31 20:21:28 +00:00
|
|
|
}
|
2022-11-02 12:34:39 +00:00
|
|
|
|
|
|
|
// {
|
|
|
|
// use std::io::BufRead;
|
|
|
|
// for line in std::io::stdin().lock().lines() {
|
|
|
|
// let line = line.unwrap();
|
2022-11-04 09:04:48 +00:00
|
|
|
// let key = line.parse::<PlainValue<NoValue>>().unwrap();
|
|
|
|
// let val = w.get(&key);
|
|
|
|
// println!("{:?} == {:?} ==> {:?} == {:?}", line, &key, val, getit(&v, &line));
|
2022-11-02 12:34:39 +00:00
|
|
|
// }
|
|
|
|
// }
|
2022-10-31 20:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 14:24:01 +00:00
|
|
|
|
|
|
|
#[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<Self, Self::Err> {
|
|
|
|
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<Dom> for DomCodec {
|
|
|
|
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
r: &mut R,
|
|
|
|
_read_annotations: bool,
|
|
|
|
) -> io::Result<Dom> {
|
|
|
|
let v = r.next_iovalue(false)?;
|
|
|
|
if v.as_bytestring().is_some() {
|
|
|
|
Ok(Dom::One)
|
|
|
|
} else {
|
|
|
|
Ok(Dom::Two)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DomainEncode<Dom> 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<PlainValue<'static, IOValue>> {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|