Created
April 4, 2012 02:13
-
-
Save jsanders/b5094ff6698806f165b9 to your computer and use it in GitHub Desktop.
Rust deserialize XML first crack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std; | |
import io::reader_util; | |
enum node { | |
tag_node({ | |
name: str, | |
attributes: [attribute], | |
children: [node] | |
}), | |
text_node(str) | |
} | |
type attribute = { | |
name: str, | |
value: str | |
}; | |
fn is_eof(c: char) -> bool { c == -1 as char } | |
fn parse_tag_name(rdr: io::reader, first_c: char) -> str { | |
let mut c = rdr.read_char(); | |
let mut tag_name = str::from_char(first_c); | |
while !is_eof(c) && c != '>' { | |
tag_name += str::from_char(c); | |
c = rdr.read_char(); | |
} | |
ret tag_name; | |
} | |
#[doc = "Deserializes an xml node value from an io::reader"] | |
fn from_reader(rdr: io::reader) -> node { | |
let mut c = rdr.read_char(); | |
let mut tag_name = ""; | |
while !is_eof(c) { | |
if c == '<' { | |
c = rdr.read_char(); | |
if c != '/' { | |
tag_name = parse_tag_name(rdr, c); | |
} | |
} | |
c = rdr.read_char(); | |
} | |
ret tag_node({ name: tag_name, attributes: [], children: [] }); | |
} | |
#[doc = "Deserializes an xml node value from a string"] | |
fn from_str(s: str) -> node { | |
io::with_str_reader(s, from_reader) | |
} | |
#[test] | |
fn test_empty_tag() { | |
assert from_str("<tag></tag>") == tag_node({ name: "tag", attributes: [], children: [] }); | |
} |
list
is actually just the name of the subtype, and the [json]
syntax means that the list
subtype is an alias for an array (or vector maybe, not clear on the difference yet) of json
types. You can reference json
in the subtype because it's an enum
. If it were a type
, it would give you a recursive type error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see
list([json])
in json implementation, islist
a builtin type or would that be useful instead ofchildren
?