Skip to content

Commit

Permalink
(fix) keep compatible with upstream changes.
Browse files Browse the repository at this point in the history
rust syntax extension changes:
rust-lang/rust#23085
reference fix:
tomjakubowski/json_macros@d4d3133
  • Loading branch information
sunng87 committed May 9, 2015
1 parent ad66742 commit 39a51e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ fn expand_derive_tojson(ct: &mut ExtCtxt, span: Span, _: &ast::MetaItem,
item: &ast::Item, push: &mut FnMut(P<ast::Item>)) {
if let ast::ItemStruct(ref struct_def, _) = item.node {
let struct_name = item.ident;
let conv_body: Vec<P<ast::Stmt>> = struct_def.fields.iter().map(|field| {
let conv_body: Vec<P<ast::Expr>> = struct_def.fields.iter().map(|field| {
if let ast::NamedField(name, _) = field.node.kind {
let name_str = name.as_str();
quote_stmt!(ct,
__container.insert($name_str.to_string(), self.$name.to_json())).unwrap()
quote_expr!(ct, {
__container.insert($name_str.to_string(), self.$name.to_json());
})
} else {
ct.span_fatal(span, "#[derive(ToJson)] doesn't support simple struct for now");
}
Expand All @@ -31,7 +32,7 @@ fn expand_derive_tojson(ct: &mut ExtCtxt, span: Span, _: &ast::MetaItem,
impl ::rustc_serialize::json::ToJson for $struct_name {
fn to_json(&self) -> ::rustc_serialize::json::Json {
let mut __container = ::std::collections::BTreeMap::new();
$conv_body
$conv_body;
::rustc_serialize::json::Json::Object(__container)
}
}).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ fn test_gen () {
println!("{}", pj);
assert_eq!(Json::String("Vidar Kjartansson".to_string()),
*pj.find("name").unwrap());
assert_eq!(Json::U64(25), *pj.find("age").unwrap());
assert_eq!(Json::String("Iceland".to_string()),
*pj.find("nationality").unwrap());
}

0 comments on commit 39a51e3

Please sign in to comment.