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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use time::{Duration, Tm, strptime};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use std::collections::BTreeMap;
use std::str::FromStr;
use std::fmt;
use error::{Error, ParseError};
use convert::FromIter;
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Default)]
pub struct Id(pub u32);
impl Encodable for Id {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
self.0.encode(e)
}
}
impl Decodable for Id {
fn decode<S: Decoder>(d: &mut S) -> Result<Id, S::Error> {
d.read_u32().map(Id)
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Default, RustcEncodable)]
pub struct QueuePlace {
pub id: Id,
pub pos: u32,
pub prio: u8,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Range(pub Duration, pub Option<Duration>);
impl Encodable for Range {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_tuple(2, |e| {
e.emit_tuple_arg(0, |e| e.emit_i64(self.0.num_seconds())).and_then(|_| {
e.emit_tuple_arg(1, |e| {
e.emit_option(|e| {
self.1
.map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
.unwrap_or_else(|| e.emit_option_none())
})
})
})
})
}
}
impl Default for Range {
fn default() -> Range {
Range(Duration::seconds(0), None)
}
}
impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0
.num_seconds()
.fmt(f)
.and_then(|_| f.write_str(":"))
.and_then(|_| self.1.map(|v| v.num_seconds().fmt(f)).unwrap_or(Ok(())))
}
}
impl FromStr for Range {
type Err = ParseError;
fn from_str(s: &str) -> Result<Range, ParseError> {
let mut splits = s.split('-').flat_map(|v| v.parse().into_iter());
match (splits.next(), splits.next()) {
(Some(s), Some(e)) => Ok(Range(Duration::seconds(s), Some(Duration::seconds(e)))),
(None, Some(e)) => Ok(Range(Duration::zero(), Some(Duration::seconds(e)))),
(Some(s), None) => Ok(Range(Duration::seconds(s), None)),
(None, None) => Ok(Range(Duration::zero(), None)),
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Song {
pub file: String,
pub name: Option<String>,
pub title: Option<String>,
pub last_mod: Option<Tm>,
pub duration: Option<Duration>,
pub place: Option<QueuePlace>,
pub range: Option<Range>,
pub tags: BTreeMap<String, String>,
}
impl Encodable for Song {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_struct("Song", 8, |e| {
e.emit_struct_field("file", 0, |e| self.file.encode(e))
.and_then(|_| e.emit_struct_field("name", 1, |e| self.name.encode(e)))
.and_then(|_| e.emit_struct_field("title", 2, |e| self.title.encode(e)))
.and_then(|_| {
e.emit_struct_field("last_mod", 3, |e| {
e.emit_option(|e| {
self.last_mod
.as_ref()
.map(|m| e.emit_option_some(|e| m.to_timespec().sec.encode(e)))
.unwrap_or_else(|| e.emit_option_none())
})
})
})
.and_then(|_| {
e.emit_struct_field("duration", 4, |e| {
e.emit_option(|e| {
self.duration
.as_ref()
.map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
.unwrap_or_else(|| e.emit_option_none())
})
})
})
.and_then(|_| e.emit_struct_field("place", 5, |e| self.place.encode(e)))
.and_then(|_| e.emit_struct_field("range", 6, |e| self.range.encode(e)))
.and_then(|_| e.emit_struct_field("tags", 7, |e| self.tags.encode(e)))
})
}
}
impl FromIter for Song {
fn from_iter<I: Iterator<Item = Result<(String, String), Error>>>(iter: I) -> Result<Song, Error> {
let mut result = Song::default();
for res in iter {
let line = try!(res);
match &*line.0 {
"file" => result.file = line.1.to_owned(),
"Title" => result.title = Some(line.1.to_owned()),
"Last-Modified" => {
result.last_mod = try!(strptime(&*line.1, "%Y-%m-%dT%H:%M:%S%Z")
.map_err(ParseError::BadTime)
.map(Some))
}
"Name" => result.name = Some(line.1.to_owned()),
"Time" => result.duration = Some(Duration::seconds(try!(line.1.parse()))),
"Range" => result.range = Some(try!(line.1.parse())),
"Id" => {
match result.place {
None => {
result.place = Some(QueuePlace {
id: Id(try!(line.1.parse())),
pos: 0,
prio: 0,
})
}
Some(ref mut place) => place.id = Id(try!(line.1.parse())),
}
}
"Pos" => {
match result.place {
None => {
result.place = Some(QueuePlace {
pos: try!(line.1.parse()),
id: Id(0),
prio: 0,
})
}
Some(ref mut place) => place.pos = try!(line.1.parse()),
}
}
"Prio" => {
match result.place {
None => {
result.place = Some(QueuePlace {
prio: try!(line.1.parse()),
id: Id(0),
pos: 0,
})
}
Some(ref mut place) => place.prio = try!(line.1.parse()),
}
}
_ => {
result.tags.insert(line.0, line.1);
}
}
}
Ok(result)
}
}