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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use {Entry, EntryExt, ExtInf};
use std;
use url;

/// A reader that reads the `M3U` format from the underlying reader.
///
/// A `Reader` is a streaming reader. It reads data from the underlying reader on demand and reads
/// no more than strictly necessary.
///
/// The inner `reader` `R` must be some buffered reader as the "#EXTM3U" header, "#EXTINF:" tags
/// and entries are each read from a single line of plain text.
///
/// A `Reader` will only attempt to read entries of type `E`.
pub struct Reader<R, E>
    where R: std::io::BufRead,
{
    /// The reader from which the `M3U` format is read.
    reader: R,
    /// String used for buffering read lines.
    line_buffer: String,
    /// The entry type that the `reader` will read.
    entry: std::marker::PhantomData<E>,
}

/// A `Reader` that specifically reads `Entry`s.
pub type EntryReader<R> = Reader<R, Entry>;
/// A `Reader` that specifically reads `EntryExt`s.
pub type EntryExtReader<R> = Reader<R, EntryExt>;

/// An iterator that yields `Entry`s.
///
/// All `Entry`s are lazily read from the inner buffered reader.
pub struct Entries<'r, R>
    where R: 'r + std::io::BufRead,
{
    reader: &'r mut EntryReader<R>,
}

/// An iterator that yields `EntryExt`s.
///
/// All `EntryExt`s are lazily read from the inner buffered reader.
pub struct EntryExts<'r, R>
    where R: 'r + std::io::BufRead,
{
    reader: &'r mut EntryExtReader<R>,
}

/// Errors that may occur when constructing a new `Reader<R, EntryExt>`.
#[derive(Debug)]
pub enum EntryExtReaderConstructionError {
    /// The "#EXTM3U" header was not found in the first line when attempting to
    /// construct a `Reader<R, EntryExt>` from some given `Reader`.
    HeaderNotFound,
    /// Errors produced by the `BufRead::read_line` method.
    BufRead(std::io::Error),
}

/// Errors that may occur when attempting to read an `EntryExt` from a read line `str`.
#[derive(Debug)]
pub enum ReadEntryExtError {
    /// Either the "#EXTINF:" tag was not found for the `EntryExt` or the duration and name
    /// following the tag were not correctly formatted.
    ///
    /// Assuming that the tag was simply omitted, the line will instead be parsed as an `Entry`.
    ExtInfNotFound(Entry),
    /// Errors produced by the `BufRead::read_line` method.
    BufRead(std::io::Error),
}


impl<R, E> Reader<R, E>
    where R: std::io::BufRead,
{

    fn new_inner(reader: R, line_buffer: String) -> Self {
        Reader {
            reader: reader,
            line_buffer: line_buffer,
            entry: std::marker::PhantomData,
        }
    }

    /// Produce the inner `reader`.
    pub fn into_inner(self) -> R {
        self.reader
    }

}

impl<R> EntryReader<R>
    where R: std::io::BufRead,
{

    /// Create a reader that reads the original, non-extended M3U `Entry` type.
    pub fn new(reader: R) -> Self {
        Self::new_inner(reader, String::new())
    }

    /// Attempt to read the next `Entry` from the inner reader.
    ///
    /// Returns `Ok(None)` when there are no more lines.
    ///
    /// Returns an `Err(std::io::Error)` if an error occurs when calling the inner `reader`'s
    /// `BufRead::read_line` method.
    fn read_next_entry(&mut self) -> Result<Option<Entry>, std::io::Error> {
        let Reader { ref mut reader, ref mut line_buffer, .. } = *self;
        read_next_entry(reader, line_buffer)
    }

    /// Produce an iterator that yields `Entry`s.
    ///
    /// All `Entry`s are lazily read from the inner buffered reader.
    pub fn entries(&mut self) -> Entries<R> {
        Entries { reader: self }
    }

}

impl<R> EntryExtReader<R>
    where R: std::io::BufRead,
{

    /// Create a reader that reads the extended M3U `EntryExt` type.
    ///
    /// The `#EXTM3U` header is read immediately.
    ///
    /// Reading `EntryExt`s will be done on demand.
    pub fn new_ext(mut reader: R) -> Result<Self, EntryExtReaderConstructionError> {
        let mut line_buffer = String::new();

        loop {
            let num_read_bytes = try!(reader.read_line(&mut line_buffer));
            let line = line_buffer.trim_left();

            // The first line of the extended M3U format should always be the "#EXTM3U" header.
            const HEADER: &'static str = "#EXTM3U";
            if line.len() >= HEADER.len() && &line[..HEADER.len()] == HEADER {
                break;
            }

            // Skip any empty lines that might be present at the top of the file.
            if num_read_bytes != 0 && line.is_empty() {
                continue;
            }

            // If the first non-empty line was not the header, return an error.
            return Err(EntryExtReaderConstructionError::HeaderNotFound);
        }

        Ok(Self::new_inner(reader, line_buffer))
    }

    /// Attempt to read the next `EntryExt` from the inner reader.
    ///
    /// This method attempts to read two non-empty, non-comment lines.
    ///
    /// The first is checked for the `EXTINF` tag which is used to create an `ExtInf`. Upon failure
    /// an `ExtInfNotFound` error is returned and the line is instead parsed as an `Entry`.
    ///
    /// If an `#EXTINF:` tag was read, next line is parsed as an `Entry`.
    ///
    /// Returns `Ok(None)` when there are no more lines.
    fn read_next_entry(&mut self) -> Result<Option<EntryExt>, ReadEntryExtError> {
        let Reader { ref mut reader, ref mut line_buffer, .. } = *self;

        const TAG: &'static str = "#EXTINF:";

        // Read an `ExtInf` from the given line.
        //
        // This function assumes the the line begins with "#EXTINF:" and will panic otherwise.
        fn read_extinf(mut line: &str) -> Option<ExtInf> {
            line = &line[TAG.len()..];

            // The duration and track title should be delimited by the first comma.
            let mut parts = line.splitn(2, ',');

            // Get the duration, or return `None` if there isn't any.
            let duration_secs = match parts.next().and_then(|s| s.parse().ok()) {
                Some(secs) => secs,
                None => return None,
            };

            // Get the name or set it as an empty string.
            let name = parts.next().map(|s| s.trim().into()).unwrap_or_else(String::new);

            Some(ExtInf {
                duration_secs: duration_secs,
                name: name,
            })
        }

        // Skip empty lines and comments until we find the "#EXTINF:" tag.
        loop {
            // Read the next line or return `None` if we're done.
            line_buffer.clear();
            if try!(reader.read_line(line_buffer)) == 0 {
                return Ok(None);
            }

            let extinf = {
                let line = line_buffer.trim_left();

                match line.chars().next() {
                    // Skip empty lines.
                    None => continue,
                    // Distinguish between comments and the "#EXTINF:" tag.
                    Some('#') => match line.len() >= TAG.len() && &line[..TAG.len()] == TAG {
                        // Skip comments.
                        false => continue,
                        // We've found the "#EXTINF:" tag.
                        true => read_extinf(line),
                    },
                    // Assume the "#EXTINF:" tag was omitted and this was intended to be an `Entry`.
                    // Due to the lack of official specification, it is unclear whether a mixture
                    // of tagged and non-tagged entries should be supported for the EXTM3U format.
                    Some(_) => {
                        let entry = read_entry(line.trim_right());
                        return Err(ReadEntryExtError::ExtInfNotFound(entry));
                    },
                }
            };

            // Read the next non-empty, non-comment line as an entry.
            let entry = match try!(read_next_entry(reader, line_buffer)) {
                None => return Ok(None),
                Some(entry) => entry,
            };

            return match extinf {
                Some(extinf) => Ok(Some(EntryExt {
                    entry: entry,
                    extinf: extinf,
                })),
                None => Err(ReadEntryExtError::ExtInfNotFound(entry)),
            }
        }
    }

    /// Produce an iterator that yields `EntryExt`s.
    ///
    /// All `EntryExt`s are lazily read from the inner buffered reader.
    pub fn entry_exts(&mut self) -> EntryExts<R> {
        EntryExts { reader: self }
    }

}

impl EntryReader<std::io::BufReader<std::fs::File>> {

    /// Attempts to create a reader that reads `Entry`s from the specified file.
    ///
    /// This is a convenience constructor that opens a `File`, wraps it in a `BufReader` and then
    /// constructs a `Reader` from it.
    pub fn open<P>(filename: P) -> Result<Self, std::io::Error>
        where P: AsRef<std::path::Path>,
    {
        let file = try!(std::fs::File::open(filename));
        let buf_reader = std::io::BufReader::new(file);
        Ok(Self::new(buf_reader))
    }

}
 
impl EntryExtReader<std::io::BufReader<std::fs::File>> {

    /// Attempts to create a reader that reads `EntryExt`s from the specified file.
    ///
    /// This is a convenience constructor that opens a `File`, wraps it in a `BufReader` and then
    /// constructs a `Reader` from it.
    pub fn open_ext<P>(filename: P) -> Result<Self, EntryExtReaderConstructionError>
        where P: AsRef<std::path::Path>,
    {
        let file = try!(std::fs::File::open(filename));
        let buf_reader = std::io::BufReader::new(file);
        Self::new_ext(buf_reader)
    }

}


/// Attempt to read the next `Entry` from the inner reader.
fn read_next_entry<R>(reader: &mut R, line_buffer: &mut String) -> Result<Option<Entry>, std::io::Error>
    where R: std::io::BufRead,
{
    loop {
        // Read the next line or return `None` if we're done.
        line_buffer.clear();
        if try!(reader.read_line(line_buffer)) == 0 {
            return Ok(None);
        }

        let line = line_buffer.trim_left();
        match line.chars().next() {
            // Skip empty lines.
            None => continue,
            // Skip comments.
            Some('#') => continue,
            // Break when we have a non-empty, non-comment line.
            _ => return Ok(Some(read_entry(line.trim_right()))),
        }
    }
}

/// Read an `Entry` from the given line.
///
/// First attempts to read a URL entry. A URL is only returned if `Some` `host_str` is parsed.
///
/// If a URL cannot be parsed, we assume the entry is a `Path`.
fn read_entry(line: &str) -> Entry {
    if let Ok(url) = url::Url::parse(line) {
        if url.host_str().is_some() {
            return Entry::Url(url);
        }

    }
    Entry::Path(line.into())
}


impl<'r, R> Iterator for Entries<'r, R>
    where R: std::io::BufRead,
{
    type Item = Result<Entry, std::io::Error>;
    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_next_entry() {
            Ok(Some(entry)) => Some(Ok(entry)),
            Ok(None) => None,
            Err(err) => Some(Err(err)),
        }
    }
}

impl<'r, R> Iterator for EntryExts<'r, R>
    where R: std::io::BufRead,
{
    type Item = Result<EntryExt, ReadEntryExtError>;
    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_next_entry() {
            Ok(Some(entry)) => Some(Ok(entry)),
            Ok(None) => None,
            Err(err) => Some(Err(err)),
        }
    }
}


impl From<std::io::Error> for EntryExtReaderConstructionError {
    fn from(err: std::io::Error) -> Self {
        EntryExtReaderConstructionError::BufRead(err)
    }
}

impl From<std::io::Error> for ReadEntryExtError {
    fn from(err: std::io::Error) -> Self {
        ReadEntryExtError::BufRead(err)
    }
}


impl std::error::Error for EntryExtReaderConstructionError {
    fn description(&self) -> &str {
        match *self {
            EntryExtReaderConstructionError::HeaderNotFound =>
                "the \"#EXTM3U\" header was not found",
            EntryExtReaderConstructionError::BufRead(ref err) =>
                std::error::Error::description(err),
        }
    }
    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            EntryExtReaderConstructionError::HeaderNotFound => None,
            EntryExtReaderConstructionError::BufRead(ref err) => Some(err),
        }
    }
}

impl std::error::Error for ReadEntryExtError {
    fn description(&self) -> &str {
        match *self {
            ReadEntryExtError::ExtInfNotFound(_) =>
                "the \"#EXTINF:\" tag was not found or was incorrectly formatted",
            ReadEntryExtError::BufRead(ref err) =>
                std::error::Error::description(err),
        }
    }
    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            ReadEntryExtError::ExtInfNotFound(_) => None,
            ReadEntryExtError::BufRead(ref err) => Some(err),
        }
    }
}


impl std::fmt::Display for EntryExtReaderConstructionError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match *self {
            EntryExtReaderConstructionError::HeaderNotFound =>
                write!(f, "{}", std::error::Error::description(self)),
            EntryExtReaderConstructionError::BufRead(ref err) =>
                err.fmt(f),
        }
    }
}

impl std::fmt::Display for ReadEntryExtError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match *self {
            ReadEntryExtError::ExtInfNotFound(_) =>
                write!(f, "{}", std::error::Error::description(self)),
            ReadEntryExtError::BufRead(ref err) =>
                err.fmt(f),
        }
    }
}