aboutsummaryrefslogtreecommitdiffstats
path: root/src/reader.rs
blob: 64e666ea630fb9d017966012d7f3d628c6103bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use rustc_serialize::base64::FromBase64;
use std::io::Read;
use std::str::FromStr;
use xml::reader::ParserConfig;
use xml::reader::EventReader as XmlEventReader;
use xml::reader::events::XmlEvent;

#[derive(PartialEq, Debug, Clone)]
pub enum PlistEvent {
	StartArray,
	EndArray,

	StartDictionary,
	EndDictionary,
	DictionaryKey(String),

	BooleanValue(bool),
	DataValue(Vec<u8>),
	DateValue(String),
	FloatValue(f64),
	IntegerValue(i64),
	StringValue(String),

	Error(())
}

pub struct EventReader<R: Read> {
	xml_reader: XmlEventReader<R>
}

impl<R: Read> EventReader<R> {
	pub fn new(reader: R) -> EventReader<R> {
		let config = ParserConfig {
			trim_whitespace: false,
			whitespace_to_characters: true,
			cdata_to_characters: true,
			ignore_comments: true,
			coalesce_characters: true,
		};

		EventReader {
			xml_reader: XmlEventReader::with_config(reader, config)
		}
	}

	fn read_string<F>(&mut self, f: F) -> PlistEvent where F:FnOnce(String) -> PlistEvent {
		match self.xml_reader.next() {
			XmlEvent::Characters(s) => f(s),
			_ => PlistEvent::Error(())
		}
	}
}

impl<R: Read> Iterator for EventReader<R> {
	type Item = PlistEvent;

	fn next(&mut self) -> Option<PlistEvent> {
		loop {
			let first_event = self.xml_reader.next();
			match first_event {
				XmlEvent::StartElement { name, .. } => match &name.local_name[..] {
					"plist" => (),
					"array" => return Some(PlistEvent::StartArray),
					"dict" => return Some(PlistEvent::StartDictionary),
					"key" => return Some(self.read_string(|s| PlistEvent::DictionaryKey(s))),
					"true" => return Some(PlistEvent::BooleanValue(true)),
					"false" => return Some(PlistEvent::BooleanValue(false)),
					"data" => return Some(self.read_string(|s| {
						match FromBase64::from_base64(&s[..]) {
							Ok(b) => PlistEvent::DataValue(b),
							Err(_) => PlistEvent::Error(())
						}
					})),
					"date" => return Some(self.read_string(|s| PlistEvent::DateValue(s))),
					"integer" => return Some(self.read_string(|s| {
						match FromStr::from_str(&s)	{
							Ok(i) => PlistEvent::IntegerValue(i),
							Err(_) => PlistEvent::Error(())
						}
					})),
					"real" => return Some(self.read_string(|s| {
						match FromStr::from_str(&s)	{
							Ok(f) => PlistEvent::FloatValue(f),
							Err(_) => PlistEvent::Error(())
						}
					})),
					"string" => return Some(self.read_string(|s| PlistEvent::StringValue(s))),
					_ => return Some(PlistEvent::Error(()))
				},
				XmlEvent::EndElement { name, .. } => match &name.local_name[..] {
					"array" => return Some(PlistEvent::EndArray),
					"dict" => return Some(PlistEvent::EndDictionary),
					_ => ()
				},
				XmlEvent::EndDocument => return None,
				_ => ()
			}
		}
	}
}

#[cfg(test)]
mod tests {
	use std::fs::File;
	use std::path::Path;

	use reader::*;

	#[test]
	fn simple() {
		use reader::PlistEvent::*;

		let reader = File::open(&Path::new("./tests/data/simple.plist")).unwrap();
		let event_reader = EventReader::new(reader);
		let events: Vec<PlistEvent> = event_reader.collect();

		let comparison = &[
			StartDictionary,
			DictionaryKey("Author".to_owned()),
			StringValue("William Shakespeare".to_owned()),
			DictionaryKey("Lines".to_owned()),
			StartArray,
			StringValue("It is a tale told by an idiot,".to_owned()),
			StringValue("Full of sound and fury, signifying nothing.".to_owned()),
			EndArray,
			DictionaryKey("Birthdate".to_owned()),
			IntegerValue(1564),
			EndDictionary
		];

		assert_eq!(events, comparison);
	}
}