Refine error API

This commit is contained in:
Tony Garnock-Jones 2019-10-15 20:44:19 +01:00
parent d2a57c839c
commit a00314d108
3 changed files with 7 additions and 10 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "preserves" name = "preserves"
version = "0.2.0" version = "0.2.1"
authors = ["Tony Garnock-Jones <tonyg@leastfixedpoint.com>"] authors = ["Tony Garnock-Jones <tonyg@leastfixedpoint.com>"]
edition = "2018" edition = "2018"
description = "Implementation of the Preserves serialization format via serde." description = "Implementation of the Preserves serialization format via serde."

View File

@ -211,7 +211,7 @@ mod decoder_tests {
#[cfg(test)] #[cfg(test)]
mod samples_tests { mod samples_tests {
use crate::symbol::Symbol; use crate::symbol::Symbol;
use crate::value::{Codec, Decoder, codec}; use crate::value::{Codec, Decoder, decoder::Error};
use crate::value::{Value, PlainValue, Map}; use crate::value::{Value, PlainValue, Map};
use crate::value::DecodePlaceholderMap; use crate::value::DecodePlaceholderMap;
use crate::value::to_value; use crate::value::to_value;
@ -237,7 +237,7 @@ mod samples_tests {
DecodeShort(#[serde(with = "serde_bytes")] Vec<u8>), DecodeShort(#[serde(with = "serde_bytes")] Vec<u8>),
} }
#[test] fn run() -> codec::Result<()> { #[test] fn run() -> std::io::Result<()> {
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap(); let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
let mut d = Decoder::<_, PlainValue>::new(&mut fh, None); let mut d = Decoder::<_, PlainValue>::new(&mut fh, None);
let tests: TestCases = from_value(&d.next().unwrap()).unwrap(); let tests: TestCases = from_value(&d.next().unwrap()).unwrap();
@ -270,14 +270,14 @@ mod samples_tests {
TestCase::DecodeError(ref bin) => { TestCase::DecodeError(ref bin) => {
match codec.decode(&mut &bin[..]) { match codec.decode(&mut &bin[..]) {
Ok(_) => panic!("Unexpected success"), Ok(_) => panic!("Unexpected success"),
Err(codec::Error::Syntax(_)) => (), Err(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(&mut &bin[..]) { match codec.decode(&mut &bin[..]) {
Ok(_) => panic!("Unexpected success"), Ok(_) => panic!("Unexpected success"),
Err(codec::Error::Eof) => (), Err(Error::Eof) => (),
Err(e) => panic!("Unexpected error {:?}", e), Err(e) => panic!("Unexpected error {:?}", e),
} }
} }

View File

@ -3,9 +3,6 @@ use decoder::{Decoder, DecodePlaceholderMap};
use encoder::{Encoder, EncodePlaceholderMap}; use encoder::{Encoder, EncodePlaceholderMap};
use std::io::{Read, Write}; use std::io::{Read, Write};
pub type Error = decoder::Error;
pub type Result<T> = decoder::Result<T>;
pub struct Codec<N: NestedValue> { pub struct Codec<N: NestedValue> {
pub decode_placeholders: Option<DecodePlaceholderMap<N>>, pub decode_placeholders: Option<DecodePlaceholderMap<N>>,
pub encode_placeholders: Option<EncodePlaceholderMap<N>>, pub encode_placeholders: Option<EncodePlaceholderMap<N>>,
@ -29,11 +26,11 @@ impl<N: NestedValue> Codec<N> {
Encoder::new(write, self.encode_placeholders.as_ref()) Encoder::new(write, self.encode_placeholders.as_ref())
} }
pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result<N> { pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> decoder::Result<N> {
self.decoder(read).next() self.decoder(read).next()
} }
pub fn encode_bytes(&self, v: &N) -> Result<Vec<u8>> { pub fn encode_bytes(&self, v: &N) -> std::io::Result<Vec<u8>> {
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
self.encoder(&mut buf).write(v)?; self.encoder(&mut buf).write(v)?;
Ok(buf) Ok(buf)