mirror of
https://github.com/rutgersc/m3u8-rs.git
synced 2025-01-03 11:48:41 +00:00
Upgraded docs to Nom 5
This commit is contained in:
parent
b810687652
commit
13405a09eb
2 changed files with 23 additions and 30 deletions
|
@ -3,7 +3,6 @@ extern crate m3u8_rs;
|
||||||
|
|
||||||
use m3u8_rs::playlist::{Playlist};
|
use m3u8_rs::playlist::{Playlist};
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use nom::IResult;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut file = std::fs::File::open("playlist.m3u8").unwrap();
|
let mut file = std::fs::File::open("playlist.m3u8").unwrap();
|
||||||
|
@ -13,9 +12,8 @@ fn main() {
|
||||||
let parsed = m3u8_rs::parse_playlist(&bytes);
|
let parsed = m3u8_rs::parse_playlist(&bytes);
|
||||||
|
|
||||||
let playlist = match parsed {
|
let playlist = match parsed {
|
||||||
IResult::Done(i, playlist) => playlist,
|
Result::Ok((i, playlist)) => playlist,
|
||||||
IResult::Error(e) => panic!("Parsing error: \n{}", e),
|
Result::Err(e) => panic!("Parsing error: \n{}", e),
|
||||||
IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match playlist {
|
match playlist {
|
||||||
|
@ -32,9 +30,8 @@ fn main_alt() {
|
||||||
let parsed = m3u8_rs::parse_playlist(&bytes);
|
let parsed = m3u8_rs::parse_playlist(&bytes);
|
||||||
|
|
||||||
match parsed {
|
match parsed {
|
||||||
IResult::Done(i, Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
|
Result::Ok((i, Playlist::MasterPlaylist(pl))) => println!("Master playlist:\n{:?}", pl),
|
||||||
IResult::Done(i, Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
|
Result::Ok((i, Playlist::MediaPlaylist(pl))) => println!("Media playlist:\n{:?}", pl),
|
||||||
IResult::Error(e) => panic!("Parsing error: \n{}", e),
|
Result::Err(e) => panic!("Parsing error: \n{}", e),
|
||||||
IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -1,7 +1,7 @@
|
||||||
//! A library to parse m3u8 playlists (HTTP Live Streaming) [link]
|
//! A library to parse m3u8 playlists (HTTP Live Streaming) [link]
|
||||||
//! (https://tools.ietf.org/html/draft-pantos-http-live-streaming-19).
|
//! (https://tools.ietf.org/html/draft-pantos-http-live-streaming-19).
|
||||||
//!
|
//!
|
||||||
//! #Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
//! Parsing a playlist and let the parser figure out if it's a media or master playlist.
|
//! Parsing a playlist and let the parser figure out if it's a media or master playlist.
|
||||||
//!
|
//!
|
||||||
|
@ -17,19 +17,10 @@
|
||||||
//! let mut bytes: Vec<u8> = Vec::new();
|
//! let mut bytes: Vec<u8> = Vec::new();
|
||||||
//! file.read_to_end(&mut bytes).unwrap();
|
//! file.read_to_end(&mut bytes).unwrap();
|
||||||
//!
|
//!
|
||||||
//! // Option 1: fn parse_playlist_res(input) -> Result<Playlist, _>
|
|
||||||
//! match m3u8_rs::parse_playlist_res(&bytes) {
|
|
||||||
//! Ok(Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
|
|
||||||
//! Ok(Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
|
|
||||||
//! Err(e) => println!("Error: {:?}", e)
|
|
||||||
//! }
|
|
||||||
//!
|
|
||||||
//! // Option 2: fn parse_playlist(input) -> IResult<_, Playlist, _>
|
|
||||||
//! match m3u8_rs::parse_playlist(&bytes) {
|
//! match m3u8_rs::parse_playlist(&bytes) {
|
||||||
//! IResult::Done(i, Playlist::MasterPlaylist(pl)) => println!("Master playlist:\n{:?}", pl),
|
//! Result::Ok((i, Playlist::MasterPlaylist(pl))) => println!("Master playlist:\n{:?}", pl),
|
||||||
//! IResult::Done(i, Playlist::MediaPlaylist(pl)) => println!("Media playlist:\n{:?}", pl),
|
//! Result::Ok((i, Playlist::MediaPlaylist(pl))) => println!("Media playlist:\n{:?}", pl),
|
||||||
//! IResult::Error(e) => panic!("Parsing error: \n{}", e),
|
//! Result::Err(e) => panic!("Parsing error: \n{}", e),
|
||||||
//! IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
|
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -47,7 +38,7 @@
|
||||||
//! let mut bytes: Vec<u8> = Vec::new();
|
//! let mut bytes: Vec<u8> = Vec::new();
|
||||||
//! file.read_to_end(&mut bytes).unwrap();
|
//! file.read_to_end(&mut bytes).unwrap();
|
||||||
//!
|
//!
|
||||||
//! if let IResult::Done(_, pl) = m3u8_rs::parse_master_playlist(&bytes) {
|
//! if let Result::Ok((_, pl)) = m3u8_rs::parse_master_playlist(&bytes) {
|
||||||
//! println!("{:?}", pl);
|
//! println!("{:?}", pl);
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
|
@ -110,10 +101,14 @@ use playlist::*;
|
||||||
// Playlist parser
|
// Playlist parser
|
||||||
// -----------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Parse a m3u8 playlist.
|
/// Parse an m3u8 playlist.
|
||||||
///
|
///
|
||||||
/// #Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::io::Read;
|
||||||
|
/// use m3u8_rs::playlist::{Playlist};
|
||||||
|
///
|
||||||
/// let mut file = std::fs::File::open("playlist.m3u8").unwrap();
|
/// let mut file = std::fs::File::open("playlist.m3u8").unwrap();
|
||||||
/// let mut bytes: Vec<u8> = Vec::new();
|
/// let mut bytes: Vec<u8> = Vec::new();
|
||||||
/// file.read_to_end(&mut bytes).unwrap();
|
/// file.read_to_end(&mut bytes).unwrap();
|
||||||
|
@ -121,15 +116,15 @@ use playlist::*;
|
||||||
/// let parsed = m3u8_rs::parse_playlist(&bytes);
|
/// let parsed = m3u8_rs::parse_playlist(&bytes);
|
||||||
///
|
///
|
||||||
/// let playlist = match parsed {
|
/// let playlist = match parsed {
|
||||||
/// IResult::Done(i, playlist) => playlist,
|
/// Result::Ok((i, playlist)) => playlist,
|
||||||
/// IResult::Error(e) => panic!("Parsing error: \n{}", e),
|
/// Result::Err(e) => panic!("Parsing error: \n{}", e),
|
||||||
/// IResult::Incomplete(e) => panic!("Parsing error: \n{:?}", e),
|
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// match playlist {
|
/// match playlist {
|
||||||
/// Playlist::MasterPlaylist(pl) => println!("Master playlist:\n{:?}", pl),
|
/// Playlist::MasterPlaylist(pl) => println!("Master playlist:\n{:?}", pl),
|
||||||
/// Playlist::MediaPlaylist(pl) => println!("Media playlist:\n{:?}", pl),
|
/// Playlist::MediaPlaylist(pl) => println!("Media playlist:\n{:?}", pl),
|
||||||
/// }
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn parse_playlist(input: &[u8]) -> IResult<&[u8], Playlist> {
|
pub fn parse_playlist(input: &[u8]) -> IResult<&[u8], Playlist> {
|
||||||
match is_master_playlist(input) {
|
match is_master_playlist(input) {
|
||||||
true => map(parse_master_playlist, Playlist::MasterPlaylist)(input),
|
true => map(parse_master_playlist, Playlist::MasterPlaylist)(input),
|
||||||
|
@ -137,9 +132,10 @@ pub fn parse_playlist(input: &[u8]) -> IResult<&[u8], Playlist> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a m3u8 playlist just like `parse_playlist`. This returns a Result<PLaylist,_>.
|
/// Parses an m3u8 playlist just like `parse_playlist`, except that this returns an [std::result::Result](std::result::Result) instead of a [nom::IResult](https://docs.rs/nom/1.2.3/nom/enum.IResult.html).
|
||||||
///
|
/// However, since [nom::IResult](nom::IResult) is now an [alias to Result](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_5.md), this is no longer needed.
|
||||||
/// #Examples
|
///
|
||||||
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use m3u8_rs::playlist::{Playlist};
|
/// use m3u8_rs::playlist::{Playlist};
|
||||||
|
|
Loading…
Reference in a new issue