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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Parser and writer for the [`PLS` playlist format](https://en.wikipedia.org/wiki/PLS_(file_format)).
//!
//! # Examples
//!
//! Reading PLS':
//!
//! ```
//! # use pls::{PlaylistElement, ElementLength};
//! assert_eq!(pls::parse(&mut &b"[playlist]\n\
//!                               File1=Track 1.mp3\n\
//!                               Title1=Unknown Artist - Track 1\n\
//!                               \n\
//!                               File2=Track 2.mp3\n\
//!                               Length2=420\n\
//!                               \n\
//!                               File3=Track 3.mp3\n\
//!                               Length3=-1\n\
//!                               \n\
//!                               NumberOfEntries=3\n"[..]).unwrap(),
//!            vec![PlaylistElement {
//!                path: "Track 1.mp3".to_string(),
//!                title: Some("Unknown Artist - Track 1".to_string()),
//!                len: ElementLength::Unknown,
//!            },
//!            PlaylistElement {
//!                path: "Track 2.mp3".to_string(),
//!                title: None,
//!                len: ElementLength::Seconds(420),
//!            },
//!            PlaylistElement {
//!                path: "Track 3.mp3".to_string(),
//!                title: None,
//!                len: ElementLength::Unknown,
//!            }]);
//! ```
//!
//! Writing PLS':
//!
//! ```
//! # use pls::{PlaylistElement, ElementLength};
//! let mut buf = Vec::new();
//! pls::write(&[PlaylistElement {
//!                path: "Track 1.mp3".to_string(),
//!                title: Some("Unknown Artist - Track 1".to_string()),
//!                len: ElementLength::Unknown,
//!            },
//!            PlaylistElement {
//!                path: "Track 2.mp3".to_string(),
//!                title: None,
//!                len: ElementLength::Seconds(420),
//!            },
//!            PlaylistElement {
//!                path: "Track 3.mp3".to_string(),
//!                title: None,
//!                len: ElementLength::Unknown,
//!            }],
//!            &mut buf).unwrap();
//! assert_eq!(String::from_utf8(buf).unwrap(),
//!            "[playlist]\n\
//!             File1=Track 1.mp3\n\
//!             Title1=Unknown Artist - Track 1\n\
//!             \n\
//!             File2=Track 2.mp3\n\
//!             Length2=420\n\
//!             \n\
//!             File3=Track 3.mp3\n\
//!             \n\
//!             NumberOfEntries=3\n\
//!             Version=2\n")
//! ```


extern crate ini as _ini;

use std::io::{self, Write, Read};
use std::error::Error as ErrorT;
use std::num::ParseIntError;
use _ini::ini;
use std::fmt;


/// A single element of a playlist
///
/// # Examples
///
/// ```
/// # use pls::{PlaylistElement, ElementLength};
/// # use std::io;
/// # struct File { d: &'static [u8] };
/// # impl File {
/// #     fn open(_: &str) -> File { File { d: &b"[playlist]\n\
/// #                                             File1=Track 1.mp3\n\
/// #                                             Title1=Unknown Artist - Track 1\n\
/// #                                             Length1=420\n\
/// #                                             \n\
/// #                                             NumberOfEntries=1\n\
/// #                                             Version=2\n"[..] } }
/// # }
/// # impl io::Read for File {
/// #     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.d.read(buf) }
/// # }
/// let elements = pls::parse(&mut File::open("Unknown Artist.pls")).unwrap();
/// # assert_eq!(elements,
/// #            vec![PlaylistElement {
/// #                path: "Track 1.mp3".to_string(),
/// #                title: Some("Unknown Artist - Track 1".to_string()),
/// #                len: ElementLength::Seconds(420),
/// #            }]);
/// ```
///
/// ```
/// # use pls::{PlaylistElement, ElementLength};
/// # use std::io;
/// # struct File { f: () };
/// # impl File {
/// #     fn create(_: &str) -> File { File { f: () } }
/// # }
/// # impl io::Write for File {
/// #     fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
/// #     fn flush(&mut self) -> io::Result<()> { Ok(()) }
/// # }
/// pls::write(&[PlaylistElement {
///                path: "Track 1.mp3".to_string(),
///                title: Some("Unknown Artist - Track 1".to_string()),
///                len: ElementLength::Seconds(420),
///            }],
///            &mut File::create("Unknown Artist.pls")).unwrap();
/// ```
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct PlaylistElement {
    /// Path specified in the `File#` key, unconstrained
    pub path: String,
    /// Title specified by the `Title#` key or `None` if omitted
    pub title: Option<String>,
    /// Length specified by the `Length#` key or `Unknown` if omitted
    pub len: ElementLength,
}

/// Playlist element's length
///
/// `Unknown` if omitted or set to `-1`
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum ElementLength {
    /// Length was specified in `Length#` field
    Seconds(u64),
    /// Length was omitted or set to `-1`
    Unknown,
}

/// All ways parsing can fail
#[derive(Debug)]
pub enum ParseError {
    /// Specified version was not `2`
    InvalidVersion(u64),
    /// The whole `[playlist]` section's missing
    MissingPlaylistSection,
    /// Some required key is missing
    MissingKey(String),
    /// An integer was not actually an integer
    InvalidInteger(ParseIntError),
    /// Other `.ini` parsing errors
    Ini(ini::Error),
}


/// Parse a playlist
///
/// The parser is very lenient and allows pretty much everything as long as the required stuff's in.
///
/// # Examples
///
/// ```
/// # use pls::{PlaylistElement, ElementLength};
/// assert_eq!(pls::parse(&mut &b"[playlist]\n\
///                               File1=Track 1.mp3\n\
///                               Title1=Unknown Artist - Track 1\n\
///                               \n\
///                               File2=Track 2.mp3\n\
///                               Length2=420\n\
///                               \n\
///                               File3=Track 3.mp3\n\
///                               Length3=-1\n\
///                               \n\
///                               NumberOfEntries=3\n"[..]).unwrap(),
///            vec![PlaylistElement {
///                path: "Track 1.mp3".to_string(),
///                title: Some("Unknown Artist - Track 1".to_string()),
///                len: ElementLength::Unknown,
///            },
///            PlaylistElement {
///                path: "Track 2.mp3".to_string(),
///                title: None,
///                len: ElementLength::Seconds(420),
///            },
///            PlaylistElement {
///                path: "Track 3.mp3".to_string(),
///                title: None,
///                len: ElementLength::Unknown,
///            }]);
/// ```
pub fn parse<R: Read>(what: &mut R) -> Result<Vec<PlaylistElement>, ParseError> {
    let p = try!(ini::Ini::read_from(what));
    let play = try!(p.section(Some("playlist")).ok_or(ParseError::MissingPlaylistSection));

    if let Some(v) = play.get("Version") {
        let v = try!(v.parse());
        if v != 2 {
            return Err(ParseError::InvalidVersion(v));
        }
    }

    if let Some(e) = play.get("NumberOfEntries") {
        let e: u64 = try!(e.parse());
        let mut elems = Vec::with_capacity(e as usize);
        for i in 1..e + 1 {
            elems.push(PlaylistElement {
                path: try!(play.get(&format!("File{}", i)).ok_or_else(|| ParseError::MissingKey(format!("File{}", i)))).clone(),
                title: play.get(&format!("Title{}", i)).cloned(),
                len: try!(ElementLength::parse(play.get(&format!("Length{}", i)))),
            })
        }
        Ok(elems)
    } else {
        Err(ParseError::MissingKey("NumberOfEntries".to_string()))
    }
}

/// Write a playlist to the specified output stream
///
/// # Examples
///
/// ```
/// # use pls::{PlaylistElement, ElementLength};
/// let mut buf = Vec::new();
/// pls::write(&[PlaylistElement {
///                path: "Track 1.mp3".to_string(),
///                title: Some("Unknown Artist - Track 1".to_string()),
///                len: ElementLength::Unknown,
///            },
///            PlaylistElement {
///                path: "Track 2.mp3".to_string(),
///                title: None,
///                len: ElementLength::Seconds(420),
///            },
///            PlaylistElement {
///                path: "Track 3.mp3".to_string(),
///                title: None,
///                len: ElementLength::Unknown,
///            }],
///            &mut buf).unwrap();
/// assert_eq!(String::from_utf8(buf).unwrap(),
///            "[playlist]\n\
///             File1=Track 1.mp3\n\
///             Title1=Unknown Artist - Track 1\n\
///             \n\
///             File2=Track 2.mp3\n\
///             Length2=420\n\
///             \n\
///             File3=Track 3.mp3\n\
///             \n\
///             NumberOfEntries=3\n\
///             Version=2\n")
/// ```
pub fn write<'i, I: IntoIterator<Item = &'i PlaylistElement>, W: Write>(what: I, to: &mut W) -> io::Result<()> {
    try!(writeln!(to, "[playlist]"));

    let mut ent = 0u64;
    for (i, &PlaylistElement { ref path, ref title, ref len }) in what.into_iter().enumerate() {
        try!(writeln!(to, "File{}={}", i + 1, path));

        if let Some(title) = title.as_ref() {
            try!(writeln!(to, "Title{}={}", i + 1, title));
        }

        if let ElementLength::Seconds(s) = *len {
            try!(writeln!(to, "Length{}={}", i + 1, s));
        }

        try!(writeln!(to, ""));
        ent += 1;
    }

    try!(writeln!(to, "NumberOfEntries={}", ent));
    try!(writeln!(to, "Version=2"));

    Ok(())
}


impl ElementLength {
    fn parse<S: AsRef<str>>(what: Option<S>) -> Result<ElementLength, ParseError> {
        if let Some(what) = what {
            let what = what.as_ref();
            if what == "-1" {
                Ok(ElementLength::Unknown)
            } else {
                Ok(ElementLength::Seconds(try!(what.parse())))
            }
        } else {
            Ok(ElementLength::Unknown)
        }
    }
}


impl From<ini::Error> for ParseError {
    fn from(e: ini::Error) -> ParseError {
        ParseError::Ini(e)
    }
}

impl From<ParseIntError> for ParseError {
    fn from(e: ParseIntError) -> ParseError {
        ParseError::InvalidInteger(e)
    }
}

impl ErrorT for ParseError {
    fn description(&self) -> &str {
        match *self {
            ParseError::InvalidVersion(_) => "invalid version specified",
            ParseError::MissingPlaylistSection => "[playlist] section missing",
            ParseError::MissingKey(_) => "required key missing",
            ParseError::InvalidInteger(ref e) => e.description(),
            ParseError::Ini(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&ErrorT> {
        match *self {
            ParseError::InvalidInteger(ref e) => Some(e),
            ParseError::Ini(ref e) => Some(e),
            _ => None,
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ParseError::InvalidVersion(v) => write!(f, "Invalid version {} specified", v),
            ParseError::MissingPlaylistSection => write!(f, "Missing [playlist] section"),
            ParseError::MissingKey(ref k) => write!(f, "Key \"{}\" missing", k),
            ParseError::InvalidInteger(ref e) => e.fmt(f),
            ParseError::Ini(ref e) => e.fmt(f),
        }
    }
}

impl Clone for ParseError {
    fn clone(&self) -> ParseError {
        match *self {
            ParseError::InvalidVersion(v) => ParseError::InvalidVersion(v),
            ParseError::MissingPlaylistSection => ParseError::MissingPlaylistSection,
            ParseError::MissingKey(ref k) => ParseError::MissingKey(k.clone()),
            ParseError::InvalidInteger(ref e) => ParseError::InvalidInteger(e.clone()),
            ParseError::Ini(ref e) => ParseError::Ini(ini::Error { msg: e.msg.clone(), ..*e }),
        }
    }
}

impl PartialEq for ParseError {
    fn eq(&self, rhs: &ParseError) -> bool {
        match (self, rhs) {
            (&ParseError::InvalidVersion(v), &ParseError::InvalidVersion(rv)) => v == rv,
            (&ParseError::MissingPlaylistSection, &ParseError::MissingPlaylistSection) => true,
            (&ParseError::MissingKey(ref k), &ParseError::MissingKey(ref rk)) => k == rk,
            (&ParseError::InvalidInteger(ref e), &ParseError::InvalidInteger(ref re)) => e == re,
            (&ParseError::Ini(ref e), &ParseError::Ini(ref re)) => e.line == re.line && e.col == re.col && e.msg == re.msg,
            (_, _) => false,
        }
    }
}