1
Fork 0

Handle fallout in libserialize

API Changes:

- from_base64() returns Result<Vec<u8>, FromBase64Error>
- from_hex() returns Result<Vec<u8>, FromHexError>
- json::List is a Vec<Json>
- Decodable is no longer implemented on ~[T] (but Encodable still is)
- DecoderHelpers::read_to_vec() returns a Result<Vec<T>, E>
This commit is contained in:
Kevin Ballard 2014-05-03 23:34:26 -07:00
parent 2a0dac6f58
commit cd3f31d9d1
4 changed files with 73 additions and 88 deletions

View file

@ -258,7 +258,7 @@ pub enum Json {
Null,
}
pub type List = ~[Json];
pub type List = Vec<Json>;
pub type Object = TreeMap<~str, Json>;
/// The errors that can arise while parsing a JSON stream.
@ -2211,7 +2211,7 @@ impl<A:ToJson,B:ToJson> ToJson for (A, B) {
fn to_json(&self) -> Json {
match *self {
(ref a, ref b) => {
List(box [a.to_json(), b.to_json()])
List(vec![a.to_json(), b.to_json()])
}
}
}
@ -2221,7 +2221,7 @@ impl<A:ToJson,B:ToJson,C:ToJson> ToJson for (A, B, C) {
fn to_json(&self) -> Json {
match *self {
(ref a, ref b, ref c) => {
List(box [a.to_json(), b.to_json(), c.to_json()])
List(vec![a.to_json(), b.to_json(), c.to_json()])
}
}
}
@ -2298,12 +2298,12 @@ mod tests {
struct Inner {
a: (),
b: uint,
c: ~[~str],
c: Vec<~str>,
}
#[deriving(Eq, Encodable, Decodable, Show)]
struct Outer {
inner: ~[Inner],
inner: Vec<Inner>,
}
fn mk_object(items: &[(~str, Json)]) -> Json {
@ -2360,22 +2360,22 @@ mod tests {
#[test]
fn test_write_list() {
assert_eq!(List(~[]).to_str(), "[]".to_owned());
assert_eq!(List(~[]).to_pretty_str(), "[]".to_owned());
assert_eq!(List(vec![]).to_str(), "[]".to_owned());
assert_eq!(List(vec![]).to_pretty_str(), "[]".to_owned());
assert_eq!(List(~[Boolean(true)]).to_str(), "[true]".to_owned());
assert_eq!(List(vec![Boolean(true)]).to_str(), "[true]".to_owned());
assert_eq!(
List(~[Boolean(true)]).to_pretty_str(),
List(vec![Boolean(true)]).to_pretty_str(),
"\
[\n \
true\n\
]".to_owned()
);
let long_test_list = List(box [
let long_test_list = List(vec![
Boolean(false),
Null,
List(box [String("foo\nbar".to_owned()), Number(3.5)])]);
List(vec![String("foo\nbar".to_owned()), Number(3.5)])]);
assert_eq!(long_test_list.to_str(),
"[false,null,[\"foo\\nbar\",3.5]]".to_owned());
@ -2411,7 +2411,7 @@ mod tests {
);
let complex_obj = mk_object([
("b".to_owned(), List(box [
("b".to_owned(), List(vec![
mk_object([("c".to_owned(), String("\x0c\r".to_owned()))]),
mk_object([("d".to_owned(), String("".to_owned()))])
]))
@ -2443,7 +2443,7 @@ mod tests {
let a = mk_object([
("a".to_owned(), Boolean(true)),
("b".to_owned(), List(box [
("b".to_owned(), List(vec![
mk_object([("c".to_owned(), String("\x0c\r".to_owned()))]),
mk_object([("d".to_owned(), String("".to_owned()))])
]))
@ -2678,44 +2678,44 @@ mod tests {
assert_eq!(from_str("[1,]"), Err(SyntaxError(InvalidSyntax, 1, 4)));
assert_eq!(from_str("[6 7]"), Err(SyntaxError(InvalidSyntax, 1, 4)));
assert_eq!(from_str("[]"), Ok(List(~[])));
assert_eq!(from_str("[ ]"), Ok(List(~[])));
assert_eq!(from_str("[true]"), Ok(List(~[Boolean(true)])));
assert_eq!(from_str("[ false ]"), Ok(List(~[Boolean(false)])));
assert_eq!(from_str("[null]"), Ok(List(~[Null])));
assert_eq!(from_str("[]"), Ok(List(vec![])));
assert_eq!(from_str("[ ]"), Ok(List(vec![])));
assert_eq!(from_str("[true]"), Ok(List(vec![Boolean(true)])));
assert_eq!(from_str("[ false ]"), Ok(List(vec![Boolean(false)])));
assert_eq!(from_str("[null]"), Ok(List(vec![Null])));
assert_eq!(from_str("[3, 1]"),
Ok(List(~[Number(3.0), Number(1.0)])));
Ok(List(vec![Number(3.0), Number(1.0)])));
assert_eq!(from_str("\n[3, 2]\n"),
Ok(List(~[Number(3.0), Number(2.0)])));
Ok(List(vec![Number(3.0), Number(2.0)])));
assert_eq!(from_str("[2, [4, 1]]"),
Ok(List(~[Number(2.0), List(~[Number(4.0), Number(1.0)])])));
Ok(List(vec![Number(2.0), List(vec![Number(4.0), Number(1.0)])])));
}
#[test]
fn test_decode_list() {
let mut decoder = Decoder::new(from_str("[]").unwrap());
let v: ~[()] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[]);
let v: Vec<()> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![]);
let mut decoder = Decoder::new(from_str("[null]").unwrap());
let v: ~[()] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[()]);
let v: Vec<()> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![()]);
let mut decoder = Decoder::new(from_str("[true]").unwrap());
let v: ~[bool] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[true]);
let v: Vec<bool> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![true]);
let mut decoder = Decoder::new(from_str("[true]").unwrap());
let v: ~[bool] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[true]);
let v: Vec<bool> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![true]);
let mut decoder = Decoder::new(from_str("[3, 1]").unwrap());
let v: ~[int] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[3, 1]);
let v: Vec<int> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![3, 1]);
let mut decoder = Decoder::new(from_str("[[3], [1, 2]]").unwrap());
let v: ~[~[uint]] = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, ~[~[3], ~[1, 2]]);
let v: Vec<Vec<uint>> = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, vec![vec![3], vec![1, 2]]);
}
#[test]
@ -2750,7 +2750,7 @@ mod tests {
"{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(),
mk_object([
("a".to_owned(), Number(1.0)),
("b".to_owned(), List(~[Boolean(true)]))
("b".to_owned(), List(vec![Boolean(true)]))
]));
assert_eq!(from_str(
"{".to_owned() +
@ -2763,7 +2763,7 @@ mod tests {
"}").unwrap(),
mk_object([
("a".to_owned(), Number(1.0)),
("b".to_owned(), List(~[
("b".to_owned(), List(vec![
Boolean(true),
String("foo\nbar".to_owned()),
mk_object([
@ -2785,8 +2785,8 @@ mod tests {
assert_eq!(
v,
Outer {
inner: ~[
Inner { a: (), b: 2, c: ~["abc".to_owned(), "xyz".to_owned()] }
inner: vec![
Inner { a: (), b: 2, c: vec!["abc".to_owned(), "xyz".to_owned()] }
]
}
);
@ -2837,7 +2837,7 @@ mod tests {
x: f64,
y: bool,
z: ~str,
w: ~[DecodeStruct]
w: Vec<DecodeStruct>
}
#[deriving(Decodable)]
enum DecodeEnum {