Skip to content

libserialize: add new error() to Decoder for Decodable objects to signal parse errors #16130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub enum EbmlEncoderTag {
pub enum Error {
IntTooBig(uint),
Expected(String),
IoError(std::io::IoError)
IoError(std::io::IoError),
ApplicationError(String)
}
// --------------------------------------

Expand All @@ -119,11 +120,11 @@ pub mod reader {

use serialize;

use super::{ EsVec, EsMap, EsEnum, EsVecLen, EsVecElt, EsMapLen, EsMapKey,
EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64, EsI32, EsI16, EsI8,
EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal, EsEnumBody, EsUint,
EsOpaque, EsLabel, EbmlEncoderTag, Doc, TaggedDoc, Error, IntTooBig,
Expected };
use super::{ ApplicationError, EsVec, EsMap, EsEnum, EsVecLen, EsVecElt,
EsMapLen, EsMapKey, EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64,
EsI32, EsI16, EsI8, EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal,
EsEnumBody, EsUint, EsOpaque, EsLabel, EbmlEncoderTag, Doc, TaggedDoc,
Error, IntTooBig, Expected };

pub type DecodeResult<T> = Result<T, Error>;
// rbml reading
Expand Down Expand Up @@ -636,6 +637,10 @@ pub mod reader {
debug!("read_map_elt_val(idx={})", idx);
self.push_doc(EsMapVal, f)
}

fn error(&mut self, err: &str) -> Error {
ApplicationError(err.to_string())
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub enum DecoderError {
ExpectedError(String, String),
MissingFieldError(String),
UnknownVariantError(String),
ApplicationError(String)
}

/// Returns a readable error string for a given error code.
Expand Down Expand Up @@ -2071,6 +2072,10 @@ impl ::Decoder<DecoderError> for Decoder {
debug!("read_map_elt_val(idx={})", idx);
f(self)
}

fn error(&mut self, err: &str) -> DecoderError {
ApplicationError(err.to_string())
}
}

/// A trait for converting values to JSON
Expand Down
3 changes: 3 additions & 0 deletions src/libserialize/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ pub trait Decoder<E> {
fn read_map<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;

// Failure
fn error(&mut self, err: &str) -> E;
}

pub trait Encodable<S:Encoder<E>, E> {
Expand Down
22 changes: 21 additions & 1 deletion src/libuuid/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ impl<T: Encoder<E>, E> Encodable<T, E> for Uuid {
impl<T: Decoder<E>, E> Decodable<T, E> for Uuid {
/// Decode a UUID from a string
fn decode(d: &mut T) -> Result<Uuid, E> {
Ok(from_str(try!(d.read_str()).as_slice()).unwrap())
match from_str(try!(d.read_str()).as_slice()) {
Some(decode) => Ok(decode),
None => Err(d.error("Unable to decode UUID"))
}
}
}

Expand Down Expand Up @@ -802,6 +805,23 @@ mod test {
assert_eq!(u, u2);
}

#[test]
fn test_bad_decode() {
use serialize::json;
use serialize::{Encodable, Decodable};

let js_good = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a8".to_string());
let js_bad1 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7ah".to_string());
let js_bad2 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a".to_string());

let u_good: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_good));
let u_bad1: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad1));
let u_bad2: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad2));
assert!(u_good.is_ok());
assert!(u_bad1.is_err());
assert!(u_bad2.is_err());
}

#[test]
fn test_iterbytes_impl_for_uuid() {
use std::collections::HashSet;
Expand Down