auto merge of #15238 : aochagavia/rust/json, r=alexcrichton
### Breaking changes: * **Removed unnecessary `box` from enum variant (`Object(Box<Object>)` becomes `Object(Object)`)** * **Deprecated `Encoder::str_encode`** ### Other changes: * Tried to make the code more idiomatic * Renamed the `wr` field of the `Encoder` and `PrettyEncoder` structs to `writer` * Replaced some `from_utf8` by `from_utf8_owned` to avoid unnecessary allocations * Removed unnecessary `unsafe` code * Added `encode` and `decode` shortcut functions * Implemented `FromStr` for `Json` * Implemented `ToJson` for tuples of arity up to 12 * Fixed some details in the documentation ### Questions * ~~The `encode` shortcut function does the same as the `Encoder::str_encode` function. It seems wrong to me that two functions do exactly the same. Should we deprecate `Encoder::str_encode`?~~ * ~~Do we really want the ToJson trait for tuples? At the moment we have it for (), (A, B), (A, B, C). I would like to remove them.~~ * ~~We are using `String` as key in the `TreeMap` representing a `Json` object. It would be better to use `&str`, but this would require to annotate lots of lifetimes. Is there any easy solution for this?~~ * There is a lot of duplicated code (`PrettyEncoder` copies about 50 lines from `Encoder`). In an OO language this could be solved very elegantly by using inheritance and overriding. What can we do here to reduce the amount of boilerplate? [breaking-change]
This commit is contained in:
commit
8bc286f998
|
@ -408,18 +408,17 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
|
||||||
// "crate": { parsed crate ... },
|
// "crate": { parsed crate ... },
|
||||||
// "plugins": { output of plugins ... }
|
// "plugins": { output of plugins ... }
|
||||||
// }
|
// }
|
||||||
let mut json = box std::collections::TreeMap::new();
|
let mut json = std::collections::TreeMap::new();
|
||||||
json.insert("schema".to_string(),
|
json.insert("schema".to_string(), json::String(SCHEMA_VERSION.to_string()));
|
||||||
json::String(SCHEMA_VERSION.to_string()));
|
let plugins_json = res.move_iter()
|
||||||
let plugins_json = box res.move_iter()
|
.filter_map(|opt| {
|
||||||
.filter_map(|opt| {
|
match opt {
|
||||||
match opt {
|
None => None,
|
||||||
None => None,
|
Some((string, json)) => {
|
||||||
Some((string, json)) => {
|
Some((string.to_string(), json))
|
||||||
Some((string.to_string(), json))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).collect();
|
}
|
||||||
|
}).collect();
|
||||||
|
|
||||||
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
|
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
|
||||||
// straight to the Rust JSON representation.
|
// straight to the Rust JSON representation.
|
||||||
|
@ -429,7 +428,7 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
|
||||||
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
|
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
|
||||||
krate.encode(&mut encoder).unwrap();
|
krate.encode(&mut encoder).unwrap();
|
||||||
}
|
}
|
||||||
str::from_utf8(w.unwrap().as_slice()).unwrap().to_string()
|
str::from_utf8_owned(w.unwrap()).unwrap()
|
||||||
};
|
};
|
||||||
let crate_json = match json::from_str(crate_json_str.as_slice()) {
|
let crate_json = match json::from_str(crate_json_str.as_slice()) {
|
||||||
Ok(j) => j,
|
Ok(j) => j,
|
||||||
|
@ -440,6 +439,5 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
|
||||||
json.insert("plugins".to_string(), json::Object(plugins_json));
|
json.insert("plugins".to_string(), json::Object(plugins_json));
|
||||||
|
|
||||||
let mut file = try!(File::create(&dst));
|
let mut file = try!(File::create(&dst));
|
||||||
try!(json::Object(json).to_writer(&mut file));
|
json::Object(json).to_writer(&mut file)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1104,7 +1104,7 @@ fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
|
||||||
|
|
||||||
impl ToJson for Metric {
|
impl ToJson for Metric {
|
||||||
fn to_json(&self) -> json::Json {
|
fn to_json(&self) -> json::Json {
|
||||||
let mut map = box TreeMap::new();
|
let mut map = TreeMap::new();
|
||||||
map.insert("value".to_string(), json::Number(self.value));
|
map.insert("value".to_string(), json::Number(self.value));
|
||||||
map.insert("noise".to_string(), json::Number(self.noise));
|
map.insert("noise".to_string(), json::Number(self.noise));
|
||||||
json::Object(map)
|
json::Object(map)
|
||||||
|
|
|
@ -22,11 +22,11 @@ enum object {
|
||||||
int_value(i64),
|
int_value(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lookup(table: Box<json::Object>, key: String, default: String) -> String
|
fn lookup(table: json::Object, key: String, default: String) -> String
|
||||||
{
|
{
|
||||||
match table.find(&key.to_string()) {
|
match table.find(&key.to_string()) {
|
||||||
option::Some(&json::String(ref s)) => {
|
option::Some(&json::String(ref s)) => {
|
||||||
(*s).to_string()
|
s.to_string()
|
||||||
}
|
}
|
||||||
option::Some(value) => {
|
option::Some(value) => {
|
||||||
println!("{} was expected to be a string but is a {:?}", key, value);
|
println!("{} was expected to be a string but is a {:?}", key, value);
|
||||||
|
@ -42,7 +42,7 @@ fn add_interface(_store: int, managed_ip: String, data: json::Json) -> (String,
|
||||||
{
|
{
|
||||||
match &data {
|
match &data {
|
||||||
&json::Object(ref interface) => {
|
&json::Object(ref interface) => {
|
||||||
let name = lookup((*interface).clone(),
|
let name = lookup(interface.clone(),
|
||||||
"ifDescr".to_string(),
|
"ifDescr".to_string(),
|
||||||
"".to_string());
|
"".to_string());
|
||||||
let label = format!("{}-{}", managed_ip, name);
|
let label = format!("{}-{}", managed_ip, name);
|
||||||
|
|
Loading…
Reference in New Issue