Skip to content

Commit 5bd8edc

Browse files
committed
libserialize: add error() to Decoder
A quick and dirty fix for #15036 until we get serious decoder reform. Right now it is impossible for a Decodable to signal a decode error, for example if it has only finitely many allowed values, is a string which must be encoded a certain way, needs a valid checksum, etc. For example in the libuuid implementation of Decodable an Option is unwrapped, meaning that a decode of a malformed UUID will cause the task to fail. Since this adds a method to the `Decoder` trait, all users will need to update their implementations to add it. The strategy used for the current implementations for JSON and EBML is to add a new entry to the error enum `ApplicationError(String)` which stores the string provided to `.error()`. [breaking-change]
1 parent b495933 commit 5bd8edc

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/librbml/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ pub enum EbmlEncoderTag {
105105
pub enum Error {
106106
IntTooBig(uint),
107107
Expected(String),
108-
IoError(std::io::IoError)
108+
IoError(std::io::IoError),
109+
ApplicationError(String)
109110
}
110111
// --------------------------------------
111112

@@ -119,11 +120,11 @@ pub mod reader {
119120

120121
use serialize;
121122

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

128129
pub type DecodeResult<T> = Result<T, Error>;
129130
// rbml reading
@@ -636,6 +637,10 @@ pub mod reader {
636637
debug!("read_map_elt_val(idx={})", idx);
637638
self.push_doc(EsMapVal, f)
638639
}
640+
641+
fn error(&mut self, err: &str) -> Error {
642+
ApplicationError(err.to_string())
643+
}
639644
}
640645
}
641646

src/libserialize/json.rs

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub enum DecoderError {
257257
ExpectedError(String, String),
258258
MissingFieldError(String),
259259
UnknownVariantError(String),
260+
ApplicationError(String)
260261
}
261262

262263
/// Returns a readable error string for a given error code.
@@ -2071,6 +2072,10 @@ impl ::Decoder<DecoderError> for Decoder {
20712072
debug!("read_map_elt_val(idx={})", idx);
20722073
f(self)
20732074
}
2075+
2076+
fn error(&mut self, err: &str) -> DecoderError {
2077+
ApplicationError(err.to_string())
2078+
}
20742079
}
20752080

20762081
/// A trait for converting values to JSON

src/libserialize/serialize.rs

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub trait Decoder<E> {
163163
fn read_map<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
164164
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
165165
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
166+
167+
// Failure
168+
fn error(&mut self, err: &str) -> E;
166169
}
167170

168171
pub trait Encodable<S:Encoder<E>, E> {

0 commit comments

Comments
 (0)