core: split into fmt::Show and fmt::String
fmt::Show is for debugging, and can and should be implemented for all public types. This trait is used with `{:?}` syntax. There still exists #[derive(Show)]. fmt::String is for types that faithfully be represented as a String. Because of this, there is no way to derive fmt::String, all implementations must be purposeful. It is used by the default format syntax, `{}`. This will break most instances of `{}`, since that now requires the type to impl fmt::String. In most cases, replacing `{}` with `{:?}` is the correct fix. Types that were being printed specifically for users should receive a fmt::String implementation to fix this. Part of #20013 [breaking-change]
This commit is contained in:
parent
8efd9901b6
commit
44440e5c18
252 changed files with 1996 additions and 1366 deletions
|
@ -215,7 +215,7 @@ use unicode::str::Utf16Item;
|
|||
use Encodable;
|
||||
|
||||
/// Represents a json value
|
||||
#[derive(Clone, PartialEq, PartialOrd)]
|
||||
#[derive(Clone, PartialEq, PartialOrd, Show)]
|
||||
pub enum Json {
|
||||
I64(i64),
|
||||
U64(u64),
|
||||
|
@ -332,7 +332,7 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
|
|||
|
||||
impl std::error::Error for DecoderError {
|
||||
fn description(&self) -> &str { "decoder error" }
|
||||
fn detail(&self) -> Option<std::string::String> { Some(self.to_string()) }
|
||||
fn detail(&self) -> Option<std::string::String> { Some(format!("{:?}", self)) }
|
||||
}
|
||||
|
||||
pub type EncodeResult = fmt::Result;
|
||||
|
@ -1889,7 +1889,7 @@ impl<T: Iterator<Item=char>> Builder<T> {
|
|||
match self.token {
|
||||
None => {}
|
||||
Some(Error(e)) => { return Err(e); }
|
||||
ref tok => { panic!("unexpected token {}", tok.clone()); }
|
||||
ref tok => { panic!("unexpected token {:?}", tok.clone()); }
|
||||
}
|
||||
result
|
||||
}
|
||||
|
@ -2004,7 +2004,7 @@ macro_rules! expect {
|
|||
match $e {
|
||||
Json::Null => Ok(()),
|
||||
other => Err(ExpectedError("Null".to_string(),
|
||||
format!("{}", other)))
|
||||
format!("{:?}", other)))
|
||||
}
|
||||
});
|
||||
($e:expr, $t:ident) => ({
|
||||
|
@ -2012,7 +2012,7 @@ macro_rules! expect {
|
|||
Json::$t(v) => Ok(v),
|
||||
other => {
|
||||
Err(ExpectedError(stringify!($t).to_string(),
|
||||
format!("{}", other)))
|
||||
format!("{:?}", other)))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -2024,20 +2024,20 @@ macro_rules! read_primitive {
|
|||
match self.pop() {
|
||||
Json::I64(f) => match num::cast(f) {
|
||||
Some(f) => Ok(f),
|
||||
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
|
||||
None => Err(ExpectedError("Number".to_string(), format!("{:?}", f))),
|
||||
},
|
||||
Json::U64(f) => match num::cast(f) {
|
||||
Some(f) => Ok(f),
|
||||
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
|
||||
None => Err(ExpectedError("Number".to_string(), format!("{:?}", f))),
|
||||
},
|
||||
Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{}", f))),
|
||||
Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{:?}", f))),
|
||||
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
|
||||
// is going to have a string here, as per JSON spec.
|
||||
Json::String(s) => match s.parse() {
|
||||
Some(f) => Ok(f),
|
||||
None => Err(ExpectedError("Number".to_string(), s)),
|
||||
},
|
||||
value => Err(ExpectedError("Number".to_string(), format!("{}", value))),
|
||||
value => Err(ExpectedError("Number".to_string(), format!("{:?}", value))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2077,7 +2077,7 @@ impl ::Decoder for Decoder {
|
|||
}
|
||||
},
|
||||
Json::Null => Ok(f64::NAN),
|
||||
value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
|
||||
value => Err(ExpectedError("Number".to_string(), format!("{:?}", value)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2095,7 +2095,7 @@ impl ::Decoder for Decoder {
|
|||
_ => ()
|
||||
}
|
||||
}
|
||||
Err(ExpectedError("single character string".to_string(), format!("{}", s)))
|
||||
Err(ExpectedError("single character string".to_string(), format!("{:?}", s)))
|
||||
}
|
||||
|
||||
fn read_str(&mut self) -> DecodeResult<string::String> {
|
||||
|
@ -2118,7 +2118,7 @@ impl ::Decoder for Decoder {
|
|||
let n = match o.remove(&"variant".to_string()) {
|
||||
Some(Json::String(s)) => s,
|
||||
Some(val) => {
|
||||
return Err(ExpectedError("String".to_string(), format!("{}", val)))
|
||||
return Err(ExpectedError("String".to_string(), format!("{:?}", val)))
|
||||
}
|
||||
None => {
|
||||
return Err(MissingFieldError("variant".to_string()))
|
||||
|
@ -2131,7 +2131,7 @@ impl ::Decoder for Decoder {
|
|||
}
|
||||
},
|
||||
Some(val) => {
|
||||
return Err(ExpectedError("Array".to_string(), format!("{}", val)))
|
||||
return Err(ExpectedError("Array".to_string(), format!("{:?}", val)))
|
||||
}
|
||||
None => {
|
||||
return Err(MissingFieldError("fields".to_string()))
|
||||
|
@ -2140,7 +2140,7 @@ impl ::Decoder for Decoder {
|
|||
n
|
||||
}
|
||||
json => {
|
||||
return Err(ExpectedError("String or Object".to_string(), format!("{}", json)))
|
||||
return Err(ExpectedError("String or Object".to_string(), format!("{:?}", json)))
|
||||
}
|
||||
};
|
||||
let idx = match names.iter().position(|n| *n == name[]) {
|
||||
|
@ -2439,7 +2439,7 @@ impl<'a, 'b> fmt::Writer for FormatShim<'a, 'b> {
|
|||
}
|
||||
}
|
||||
|
||||
impl fmt::Show for Json {
|
||||
impl fmt::String for Json {
|
||||
/// Encodes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut shim = FormatShim { inner: f };
|
||||
|
@ -2448,7 +2448,7 @@ impl fmt::Show for Json {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Show for PrettyJson<'a> {
|
||||
impl<'a> fmt::String for PrettyJson<'a> {
|
||||
/// Encodes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut shim = FormatShim { inner: f };
|
||||
|
@ -2457,7 +2457,15 @@ impl<'a> fmt::Show for PrettyJson<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
//NOTE(stage0): remove impl after snapshot
|
||||
impl<'a, T: Encodable> fmt::Show for AsJson<'a, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::String::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Encodable> fmt::String for AsJson<'a, T> {
|
||||
/// Encodes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut shim = FormatShim { inner: f };
|
||||
|
@ -2474,7 +2482,7 @@ impl<'a, T> AsPrettyJson<'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Encodable> fmt::Show for AsPrettyJson<'a, T> {
|
||||
impl<'a, T: Encodable> fmt::String for AsPrettyJson<'a, T> {
|
||||
/// Encodes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut shim = FormatShim { inner: f };
|
||||
|
@ -3140,9 +3148,9 @@ mod tests {
|
|||
Ok(json) => Decodable::decode(&mut Decoder::new(json))
|
||||
};
|
||||
match res {
|
||||
Ok(_) => panic!("`{}` parsed & decoded ok, expecting error `{}`",
|
||||
Ok(_) => panic!("`{:?}` parsed & decoded ok, expecting error `{:?}`",
|
||||
to_parse, expected),
|
||||
Err(ParseError(e)) => panic!("`{}` is not valid json: {}",
|
||||
Err(ParseError(e)) => panic!("`{:?}` is not valid json: {:?}",
|
||||
to_parse, e),
|
||||
Err(e) => {
|
||||
assert_eq!(e, expected);
|
||||
|
@ -3353,7 +3361,7 @@ mod tests {
|
|||
write!(&mut mem_buf, "{}", super::as_pretty_json(&hm)).unwrap();
|
||||
let json_str = from_utf8(mem_buf[]).unwrap();
|
||||
match from_str(json_str) {
|
||||
Err(_) => panic!("Unable to parse json_str: {}", json_str),
|
||||
Err(_) => panic!("Unable to parse json_str: {:?}", json_str),
|
||||
_ => {} // it parsed and we are good to go
|
||||
}
|
||||
}
|
||||
|
@ -3369,7 +3377,7 @@ mod tests {
|
|||
write!(&mut mem_buf, "{}", super::as_pretty_json(&hm)).unwrap();
|
||||
let json_str = from_utf8(mem_buf[]).unwrap();
|
||||
match from_str(json_str) {
|
||||
Err(_) => panic!("Unable to parse json_str: {}", json_str),
|
||||
Err(_) => panic!("Unable to parse json_str: {:?}", json_str),
|
||||
_ => {} // it parsed and we are good to go
|
||||
}
|
||||
}
|
||||
|
@ -3432,7 +3440,7 @@ mod tests {
|
|||
use Decodable;
|
||||
let json_str = "{\"1\":true}";
|
||||
let json_obj = match from_str(json_str) {
|
||||
Err(_) => panic!("Unable to parse json_str: {}", json_str),
|
||||
Err(_) => panic!("Unable to parse json_str: {:?}", json_str),
|
||||
Ok(o) => o
|
||||
};
|
||||
let mut decoder = Decoder::new(json_obj);
|
||||
|
@ -3445,7 +3453,7 @@ mod tests {
|
|||
use Decodable;
|
||||
let json_str = "{\"a\":true}";
|
||||
let json_obj = match from_str(json_str) {
|
||||
Err(_) => panic!("Unable to parse json_str: {}", json_str),
|
||||
Err(_) => panic!("Unable to parse json_str: {:?}", json_str),
|
||||
Ok(o) => o
|
||||
};
|
||||
let mut decoder = Decoder::new(json_obj);
|
||||
|
@ -3464,7 +3472,7 @@ mod tests {
|
|||
};
|
||||
let (ref expected_evt, ref expected_stack) = expected[i];
|
||||
if !parser.stack().is_equal_to(expected_stack.as_slice()) {
|
||||
panic!("Parser stack is not equal to {}", expected_stack);
|
||||
panic!("Parser stack is not equal to {:?}", expected_stack);
|
||||
}
|
||||
assert_eq!(&evt, expected_evt);
|
||||
i+=1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue