Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created April 4, 2012 02:13
Show Gist options
  • Save jsanders/b5094ff6698806f165b9 to your computer and use it in GitHub Desktop.
Save jsanders/b5094ff6698806f165b9 to your computer and use it in GitHub Desktop.
Rust deserialize XML first crack
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: [] });
}
@jsanders
Copy link
Author

jsanders commented Apr 4, 2012

Note that some of the middle of my last comment is out of date now, as I've changed the node type to be an enum, but you should compare the two approaches. Both of them seem to introduce one extra type that it doesn't feel like I should need - children in the old version and tag_node in the new version.

@lmarlow
Copy link

lmarlow commented Apr 4, 2012

I see list([json]) in json implementation, is list a builtin type or would that be useful instead of children?

@jsanders
Copy link
Author

jsanders commented Apr 4, 2012

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