preserves/implementations/rust/oo/src/text/mod.rs

165 lines
4.7 KiB
Rust

use std::io;
pub mod reader;
pub mod writer;
pub use reader::TextReader;
pub use writer::TextWriter;
use crate::BinarySource;
use crate::BytesBinarySource;
use crate::Domain;
use crate::DomainDecode;
use crate::IOValue;
use crate::PlainValue;
use crate::Reader;
pub fn from_str<'de, D: Domain + 'static, Dec: DomainDecode<D>>(
s: &'de str,
decode_embedded: &mut Dec,
) -> io::Result<PlainValue<'static, D>> {
BytesBinarySource::new(s.as_bytes()).text().next(false, decode_embedded)
}
pub fn iovalue_from_str(s: &str) -> io::Result<IOValue> {
BytesBinarySource::new(s.as_bytes()).text().next_iovalue(false)
}
pub fn annotated_from_str<'de, D: Domain + 'static, Dec: DomainDecode<D>>(
s: &'de str,
decode_embedded: &mut Dec,
) -> io::Result<PlainValue<'static, D>> {
BytesBinarySource::new(s.as_bytes()).text().next(true, decode_embedded)
}
pub fn annotated_iovalue_from_str(s: &str) -> io::Result<IOValue> {
BytesBinarySource::new(s.as_bytes()).text().next_iovalue(true)
}
#[cfg(test)]
mod formatting_tests {
use crate::IOValue;
use crate::test_domain::Dom;
use crate::ArcValue;
#[test] fn format_debug_and_parse() {
let v = "[1, {z: 2, a: #!\"One\"}, 3]".parse::<ArcValue<Dom>>().unwrap();
assert_eq!(format!("{:?}", &v), "[1, {a: #!\"One\", z: 2}, 3]");
}
#[test] fn format_pretty_debug_and_parse() {
let v = "[1, {z: 2, a: #!\"One\"}, 3]".parse::<ArcValue<Dom>>().unwrap();
assert_eq!(format!("{:#?}", &v), concat!(
"[\n",
" 1,\n",
" {\n",
" a: #!\"One\",\n",
" z: 2\n",
" },\n",
" 3\n",
"]"));
}
#[test] fn iovalue_parse() {
let v = "[1 @{a:b c:d} @\"foo\" #![2 3] 4]".parse::<IOValue>().unwrap();
assert_eq!(format!("{:#?}", &v), concat!(
"[\n",
" 1,\n",
" @{\n",
" a: b,\n",
" c: d\n",
" } @\"foo\" #![\n",
" 2,\n",
" 3\n",
" ],\n",
" 4\n",
"]"));
}
}
#[cfg(test)]
mod unusual_float_tests {
use std::io;
use crate::*;
fn d32(v: f32) {
println!("{}{}{}{}{:?}: preserves {:?}",
if v.is_nan() { "nan " } else { "" },
if v.is_infinite() { "inf " } else { "" },
if v.is_subnormal() { "sub " } else { "" },
if v.is_sign_positive() { "+ve " } else { "-ve " },
v,
iovalue(v));
}
fn d64(v: f64) {
println!("{}{}{}{}{:?}: preserves {:?}",
if v.is_nan() { "nan " } else { "" },
if v.is_infinite() { "inf " } else { "" },
if v.is_subnormal() { "sub " } else { "" },
if v.is_sign_positive() { "+ve " } else { "-ve " },
v,
iovalue(v));
}
#[test] fn subnormal32() -> io::Result<()> {
d32(f32::from_bits(0x00000100));
d32(f32::from_bits(0x80000100));
Ok(())
}
#[test] fn normal32() -> io::Result<()> {
d32(f32::from_bits(0x01000000));
d32(f32::from_bits(0x81000000));
Ok(())
}
#[test] fn nan32() -> io::Result<()> {
d32(f32::from_bits(0x7fc00000));
d32(f32::from_bits(0x7fc5a5a5));
d32(f32::from_bits(0x7f800001));
d32(f32::from_bits(0x7f85a5a5));
d32(f32::from_bits(0xffc00000));
d32(f32::from_bits(0xffc5a5a5));
d32(f32::from_bits(0xff800001));
d32(f32::from_bits(0xff85a5a5));
Ok(())
}
#[test] fn inf32() -> io::Result<()> {
d32(f32::from_bits(0x7f800000));
d32(f32::from_bits(0xff800000));
Ok(())
}
#[test] fn subnormal64() -> io::Result<()> {
d64(f64::from_bits(0x0000000000000100));
d64(f64::from_bits(0x8000000000000100));
Ok(())
}
#[test] fn normal64() -> io::Result<()> {
d64(f64::from_bits(0x0100000000000000));
d64(f64::from_bits(0x8100000000000000));
Ok(())
}
#[test] fn nan64() -> io::Result<()> {
d64(f64::from_bits(0x7ff8000000000000));
d64(f64::from_bits(0x7ff85a5a5a5a5a5a));
d64(f64::from_bits(0x7ff0000000000001));
d64(f64::from_bits(0x7ff05a5a5a5a5a5a));
d64(f64::from_bits(0xfff8000000000000));
d64(f64::from_bits(0xfff85a5a5a5a5a5a));
d64(f64::from_bits(0xfff0000000000001));
d64(f64::from_bits(0xfff05a5a5a5a5a5a));
Ok(())
}
#[test] fn inf64() -> io::Result<()> {
d64(f64::from_bits(0x7ff0000000000000));
d64(f64::from_bits(0xfff0000000000000));
Ok(())
}
}