Mutable Read in Decoder

This commit is contained in:
Tony Garnock-Jones 2019-09-20 21:42:09 +01:00
parent 898a5d680d
commit 8cdd67f5ba
5 changed files with 32 additions and 28 deletions

View File

@ -180,7 +180,8 @@ mod decoder_tests {
use crate::value::Value; use crate::value::Value;
#[test] fn read_123() { #[test] fn read_123() {
let mut d = Decoder::new(&b"abc"[..], None); let mut buf = &b"abc"[..];
let mut d = Decoder::new(&mut buf, None);
assert_eq!(d.read().ok(), Some(97)); assert_eq!(d.read().ok(), Some(97));
assert_eq!(d.read().ok(), Some(98)); assert_eq!(d.read().ok(), Some(98));
assert_eq!(d.read().ok(), Some(99)); assert_eq!(d.read().ok(), Some(99));
@ -188,7 +189,8 @@ mod decoder_tests {
} }
#[test] fn skip_annotations_noskip() { #[test] fn skip_annotations_noskip() {
let mut d = Decoder::new(&b"\x0521"[..], None); let mut buf = &b"\x0521"[..];
let mut d = Decoder::new(&mut buf, None);
let v = d.next().unwrap(); let v = d.next().unwrap();
assert_eq!(v.annotations().len(), 1); assert_eq!(v.annotations().len(), 1);
assert_eq!(v.annotations()[0], Value::from(2).wrap()); assert_eq!(v.annotations()[0], Value::from(2).wrap());
@ -196,7 +198,8 @@ mod decoder_tests {
} }
#[test] fn skip_annotations_skip() { #[test] fn skip_annotations_skip() {
let mut d = Decoder::new(&b"\x0521"[..], None); let mut buf = &b"\x0521"[..];
let mut d = Decoder::new(&mut buf, None);
d.set_read_annotations(false); d.set_read_annotations(false);
let v = d.next().unwrap(); let v = d.next().unwrap();
assert_eq!(v.annotations().len(), 0); assert_eq!(v.annotations().len(), 0);
@ -235,7 +238,8 @@ mod samples_tests {
} }
#[test] fn run() -> codec::Result<()> { #[test] fn run() -> codec::Result<()> {
let mut d = Decoder::new(std::fs::File::open("../../tests/samples.bin").unwrap(), None); let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
let mut d = Decoder::new(&mut fh, None);
let tests: TestCases = from_value(&d.next().unwrap()).unwrap(); let tests: TestCases = from_value(&d.next().unwrap()).unwrap();
// println!("{:#?}", tests); // println!("{:#?}", tests);
@ -244,8 +248,8 @@ mod samples_tests {
println!("{:?} ==> {:?}", name, case); println!("{:?} ==> {:?}", name, case);
match case { match case {
TestCase::Test(ref bin, ref val) => { TestCase::Test(ref bin, ref val) => {
assert_eq!(&codec.decode(&codec.encode_bytes(val)?[..])?, val); assert_eq!(&codec.decode(&mut &codec.encode_bytes(val)?[..])?, val);
assert_eq!(&codec.decode(&bin[..])?, val); assert_eq!(&codec.decode(&mut &bin[..])?, val);
assert_eq!(&codec.encode_bytes(val)?, bin); assert_eq!(&codec.encode_bytes(val)?, bin);
} }
TestCase::NondeterministicTest(ref bin, ref val) => { TestCase::NondeterministicTest(ref bin, ref val) => {
@ -253,25 +257,25 @@ mod samples_tests {
// written so that while strictly // written so that while strictly
// "nondeterministic", the order of keys in // "nondeterministic", the order of keys in
// dictionaries follows Preserves order. // dictionaries follows Preserves order.
assert_eq!(&codec.decode(&codec.encode_bytes(val)?[..])?, val); assert_eq!(&codec.decode(&mut &codec.encode_bytes(val)?[..])?, val);
assert_eq!(&codec.decode(&bin[..])?, val); assert_eq!(&codec.decode(&mut &bin[..])?, val);
assert_eq!(&codec.encode_bytes(val)?, bin); assert_eq!(&codec.encode_bytes(val)?, bin);
} }
TestCase::StreamingTest(ref bin, ref val) => { TestCase::StreamingTest(ref bin, ref val) => {
assert_eq!(&codec.decode(&codec.encode_bytes(val)?[..])?, val); assert_eq!(&codec.decode(&mut &codec.encode_bytes(val)?[..])?, val);
assert_eq!(&codec.decode(&bin[..])?, val); assert_eq!(&codec.decode(&mut &bin[..])?, val);
} }
TestCase::ParseError(_) => (), TestCase::ParseError(_) => (),
TestCase::ParseShort(_) => (), TestCase::ParseShort(_) => (),
TestCase::DecodeError(ref bin) => { TestCase::DecodeError(ref bin) => {
match codec.decode(&bin[..]) { match codec.decode(&mut &bin[..]) {
Ok(_) => panic!("Unexpected success"), Ok(_) => panic!("Unexpected success"),
Err(codec::Error::Syntax(_)) => (), Err(codec::Error::Syntax(_)) => (),
Err(e) => panic!("Unexpected error {:?}", e), Err(e) => panic!("Unexpected error {:?}", e),
} }
} }
TestCase::DecodeShort(ref bin) => { TestCase::DecodeShort(ref bin) => {
match codec.decode(&bin[..]) { match codec.decode(&mut &bin[..]) {
Ok(_) => panic!("Unexpected success"), Ok(_) => panic!("Unexpected success"),
Err(codec::Error::Eof) => (), Err(codec::Error::Eof) => (),
Err(e) => panic!("Unexpected error {:?}", e), Err(e) => panic!("Unexpected error {:?}", e),

View File

@ -21,7 +21,7 @@ impl Codec {
Codec { decode_placeholders: None, encode_placeholders: None } Codec { decode_placeholders: None, encode_placeholders: None }
} }
pub fn decoder<'a, R: Read>(&'a self, read: R) -> Decoder<'a, R> { pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'r, 'a, R> {
Decoder::new(read, self.decode_placeholders.as_ref()) Decoder::new(read, self.decode_placeholders.as_ref())
} }
@ -29,7 +29,7 @@ impl Codec {
Encoder::new(write, self.encode_placeholders.as_ref()) Encoder::new(write, self.encode_placeholders.as_ref())
} }
pub fn decode<R: Read>(&self, read: R) -> Result<AValue> { pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result<AValue> {
self.decoder(read).next() self.decoder(read).next()
} }

View File

@ -40,16 +40,20 @@ impl Error {
pub type DecodePlaceholderMap = std::collections::BTreeMap<usize, Value>; pub type DecodePlaceholderMap = std::collections::BTreeMap<usize, Value>;
pub struct Decoder<'a, R: Read> { pub struct Decoder<'a, 'b, R: Read> {
read: R, read: &'a mut R,
index: usize, index: usize,
buf: Box<Option<Option<u8>>>, buf: Box<Option<Option<u8>>>,
placeholders: Option<&'a DecodePlaceholderMap>, placeholders: Option<&'b DecodePlaceholderMap>,
read_annotations: bool, read_annotations: bool,
} }
impl<'a, R: Read> Decoder<'a, R> { fn decodeint(bs: &[u8]) -> BigInt {
pub fn new(read: R, placeholders: Option<&'a DecodePlaceholderMap>) -> Self { BigInt::from_signed_bytes_be(bs)
}
impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
pub fn new(read: &'a mut R, placeholders: Option<&'b DecodePlaceholderMap>) -> Self {
Decoder{ Decoder{
read, read,
index: 0, index: 0,
@ -161,13 +165,9 @@ impl<'a, R: Read> Decoder<'a, R> {
} }
} }
pub fn decodeint(bs: &[u8]) -> BigInt {
BigInt::from_signed_bytes_be(bs)
}
pub fn decodebinary(minor: AtomMinor, bs: Vec<u8>) -> Result<AValue> { pub fn decodebinary(minor: AtomMinor, bs: Vec<u8>) -> Result<AValue> {
Ok(match minor { Ok(match minor {
AtomMinor::SignedInteger => Value::from(Self::decodeint(&bs)).wrap(), AtomMinor::SignedInteger => Value::from(decodeint(&bs)).wrap(),
AtomMinor::String => Value::from(std::str::from_utf8(&bs)?).wrap(), AtomMinor::String => Value::from(std::str::from_utf8(&bs)?).wrap(),
AtomMinor::ByteString => Value::from(bs).wrap(), AtomMinor::ByteString => Value::from(bs).wrap(),
AtomMinor::Symbol => Value::symbol(std::str::from_utf8(&bs)?).wrap(), AtomMinor::Symbol => Value::symbol(std::str::from_utf8(&bs)?).wrap(),

View File

@ -115,8 +115,8 @@ impl serde::Serializer for Serializer {
{ {
if name == crate::value::value::MAGIC { if name == crate::value::value::MAGIC {
let v = to_value(value)?; let v = to_value(value)?;
let buf: &[u8] = v.value().as_bytestring().ok_or(Error::InternalMagicError)?; let mut buf: &[u8] = v.value().as_bytestring().ok_or(Error::InternalMagicError)?;
crate::value::Decoder::new(buf, None).next().or(Err(Error::InternalMagicError)) crate::value::Decoder::new(&mut buf, None).next().or(Err(Error::InternalMagicError))
} else { } else {
// TODO: This is apparently discouraged, and we should apparently just serialize `value`? // TODO: This is apparently discouraged, and we should apparently just serialize `value`?
Ok(Value::simple_record(name, vec![to_value(value)?]).wrap()) Ok(Value::simple_record(name, vec![to_value(value)?]).wrap())

View File

@ -591,7 +591,7 @@ impl serde::Serialize for AValue {
impl<'de> serde::Deserialize<'de> for Value { impl<'de> serde::Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?; let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
let v = crate::value::Decoder::new(&buf[..], None).next() let v = crate::value::Decoder::new(&mut &buf[..], None).next()
.or(Err(serde::de::Error::custom("Internal error")))?; .or(Err(serde::de::Error::custom("Internal error")))?;
Ok(v.value().clone()) Ok(v.value().clone())
} }
@ -600,7 +600,7 @@ impl<'de> serde::Deserialize<'de> for Value {
impl<'de> serde::Deserialize<'de> for AValue { impl<'de> serde::Deserialize<'de> for AValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?; let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
crate::value::Decoder::new(&buf[..], None).next() crate::value::Decoder::new(&mut &buf[..], None).next()
.or(Err(serde::de::Error::custom("Internal error"))) .or(Err(serde::de::Error::custom("Internal error")))
} }
} }