mirror of
https://github.com/rutgersc/m3u8-rs.git
synced 2024-11-15 21:21:08 +00:00
A stub on updating nom to 5.1.0 version
This commit is contained in:
parent
279fefef72
commit
22571a3404
1 changed files with 77 additions and 74 deletions
151
src/lib.rs
151
src/lib.rs
|
@ -46,7 +46,7 @@
|
|||
//! let mut file = std::fs::File::open("masterplaylist.m3u8").unwrap();
|
||||
//! let mut bytes: Vec<u8> = Vec::new();
|
||||
//! file.read_to_end(&mut bytes).unwrap();
|
||||
//!
|
||||
//!
|
||||
//! if let IResult::Done(_, pl) = m3u8_rs::parse_master_playlist(&bytes) {
|
||||
//! println!("{:?}", pl);
|
||||
//! }
|
||||
|
@ -61,7 +61,7 @@
|
|||
//! use m3u8_rs::playlist::{MediaPlaylist, MediaPlaylistType, MediaSegment};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let playlist = MediaPlaylist {
|
||||
//! let playlist = MediaPlaylist {
|
||||
//! version: 6,
|
||||
//! target_duration: 3.0,
|
||||
//! media_sequence: 338559,
|
||||
|
@ -78,10 +78,10 @@
|
|||
//! ],
|
||||
//! ..Default::default()
|
||||
//! };
|
||||
//!
|
||||
//!
|
||||
//! //let mut v: Vec<u8> = Vec::new();
|
||||
//! //playlist.write_to(&mut v).unwrap();
|
||||
//!
|
||||
//!
|
||||
//! //let mut file = std::fs::File::open("playlist.m3u8").unwrap();
|
||||
//! //playlist.write_to(&mut file).unwrap();
|
||||
//! }
|
||||
|
@ -94,6 +94,7 @@ extern crate nom;
|
|||
pub mod playlist;
|
||||
|
||||
use nom::*;
|
||||
|
||||
use std::str;
|
||||
use std::f32;
|
||||
use std::string;
|
||||
|
@ -221,7 +222,7 @@ pub fn contains_master_tag(input: &[u8]) -> Option<(bool, String)> {
|
|||
}
|
||||
|
||||
named!(pub is_master_playlist_tag_line(&[u8]) -> Option<(bool, String)>,
|
||||
chain!(
|
||||
do_parse!(
|
||||
tag: opt!(alt!(
|
||||
map!(tag!("#EXT-X-STREAM-INF"), |t| (true, t))
|
||||
| map!(tag!("#EXT-X-I-FRAME-STREAM-INF"), |t| (true, t))
|
||||
|
@ -243,11 +244,11 @@ named!(pub is_master_playlist_tag_line(&[u8]) -> Option<(bool, String)>,
|
|||
| map!(tag!("#EXT-X-MAP"), |t| (false, t))
|
||||
| map!(tag!("#EXT-X-PROGRAM-DATE-TIME"), |t| (false, t))
|
||||
| map!(tag!("#EXT-X-DATERANGE"), |t| (false, t))
|
||||
))
|
||||
~ consume_line
|
||||
, || {
|
||||
)) >>
|
||||
consume_line >>
|
||||
( {
|
||||
tag.map(|(a,b)| (a, from_utf8_slice(b).unwrap()))
|
||||
}
|
||||
} )
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -256,9 +257,9 @@ named!(pub is_master_playlist_tag_line(&[u8]) -> Option<(bool, String)>,
|
|||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
pub fn parse_master_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MasterPlaylistTag>> {
|
||||
chain!(input,
|
||||
mut tags: many0!(chain!(m:master_playlist_tag ~ multispace?, || m)) ~ eof?,
|
||||
|| { tags.reverse(); tags }
|
||||
do_parse!(input,
|
||||
tags: many0!(do_parse!(m:master_playlist_tag >> opt!(character::complete::multispace1) >> (m) )) >> opt!(eof!()) >>
|
||||
( { tags.reverse(); tags } )
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -299,28 +300,28 @@ pub fn master_playlist_tag(input: &[u8]) -> IResult<&[u8], MasterPlaylistTag> {
|
|||
}
|
||||
|
||||
named!(pub variant_stream_tag<VariantStream>,
|
||||
chain!(tag!("#EXT-X-STREAM-INF:") ~ attributes: key_value_pairs,
|
||||
|| VariantStream::from_hashmap(attributes, false))
|
||||
do_parse!(tag!("#EXT-X-STREAM-INF:") >> attributes: key_value_pairs >>
|
||||
( VariantStream::from_hashmap(attributes, false)))
|
||||
);
|
||||
|
||||
named!(pub variant_i_frame_stream_tag<VariantStream>,
|
||||
chain!( tag!("#EXT-X-I-FRAME-STREAM-INF:") ~ attributes: key_value_pairs,
|
||||
|| VariantStream::from_hashmap(attributes, true))
|
||||
do_parse!( tag!("#EXT-X-I-FRAME-STREAM-INF:") >> attributes: key_value_pairs >>
|
||||
( VariantStream::from_hashmap(attributes, true)))
|
||||
);
|
||||
|
||||
named!(pub alternative_media_tag<AlternativeMedia>,
|
||||
chain!( tag!("#EXT-X-MEDIA:") ~ attributes: key_value_pairs,
|
||||
|| AlternativeMedia::from_hashmap(attributes))
|
||||
do_parse!( tag!("#EXT-X-MEDIA:") >> attributes: key_value_pairs >>
|
||||
( AlternativeMedia::from_hashmap(attributes)))
|
||||
);
|
||||
|
||||
named!(pub session_data_tag<SessionData>,
|
||||
chain!( tag!("#EXT-X-SESSION-DATA:") ~ attributes: key_value_pairs,
|
||||
|| SessionData::from_hashmap(attributes))
|
||||
do_parse!( tag!("#EXT-X-SESSION-DATA:") >> attributes: key_value_pairs >>
|
||||
( SessionData::from_hashmap(attributes)))
|
||||
);
|
||||
|
||||
named!(pub session_key_tag<SessionKey>,
|
||||
chain!( tag!("#EXT-X-SESSION-KEY:") ~ session_key: map!(key, SessionKey),
|
||||
|| session_key)
|
||||
do_parse!( tag!("#EXT-X-SESSION-KEY:") >> session_key: map!(key, SessionKey) >>
|
||||
( session_key))
|
||||
);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
@ -328,9 +329,9 @@ named!(pub session_key_tag<SessionKey>,
|
|||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
pub fn parse_media_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MediaPlaylistTag>> {
|
||||
chain!(input,
|
||||
mut tags: many0!(chain!(m:media_playlist_tag ~ multispace?, || m)) ~ eof?,
|
||||
|| { tags.reverse(); tags }
|
||||
do_parse!(input,
|
||||
tags: many0!(do_parse!(m:media_playlist_tag >> opt!(character::complete::multispace1) >> (m) )) >> opt!(eof!()) >>
|
||||
( { tags.reverse(); tags } )
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -355,10 +356,10 @@ pub fn media_playlist_tag(input: &[u8]) -> IResult<&[u8], MediaPlaylistTag> {
|
|||
map!(m3u_tag, MediaPlaylistTag::M3U)
|
||||
| map!(version_tag, MediaPlaylistTag::Version)
|
||||
|
||||
| map!(chain!(tag!("#EXT-X-TARGETDURATION:") ~ n:float,||n), MediaPlaylistTag::TargetDuration)
|
||||
| map!(chain!(tag!("#EXT-X-MEDIA-SEQUENCE:") ~ n:number,||n), MediaPlaylistTag::MediaSequence)
|
||||
| map!(chain!(tag!("#EXT-X-DISCONTINUITY-SEQUENCE:") ~ n:number,||n), MediaPlaylistTag::DiscontinuitySequence)
|
||||
| map!(chain!(tag!("#EXT-X-PLAYLIST-TYPE:") ~ t:playlist_type, ||t), MediaPlaylistTag::PlaylistType)
|
||||
| map!(do_parse!(tag!("#EXT-X-TARGETDURATION:") >> n:float >> (n)), MediaPlaylistTag::TargetDuration)
|
||||
| map!(do_parse!(tag!("#EXT-X-MEDIA-SEQUENCE:") >> n:number >> (n)), MediaPlaylistTag::MediaSequence)
|
||||
| map!(do_parse!(tag!("#EXT-X-DISCONTINUITY-SEQUENCE:") >> n:number >> (n)), MediaPlaylistTag::DiscontinuitySequence)
|
||||
| map!(do_parse!(tag!("#EXT-X-PLAYLIST-TYPE:") >> t:playlist_type >> (t)), MediaPlaylistTag::PlaylistType)
|
||||
| map!(tag!("#EXT-X-I-FRAMES-ONLY"), |_| MediaPlaylistTag::IFramesOnly)
|
||||
| map!(start_tag, MediaPlaylistTag::Start)
|
||||
| map!(tag!("#EXT-X-INDEPENDENT-SEGMENTS"), |_| MediaPlaylistTag::IndependentSegments)
|
||||
|
@ -370,7 +371,7 @@ pub fn media_playlist_tag(input: &[u8]) -> IResult<&[u8], MediaPlaylistTag> {
|
|||
|
||||
named!(pub playlist_type<MediaPlaylistType>,
|
||||
map_res!(
|
||||
map_res!(take_until_either_and_consume!("\r\n"), str::from_utf8),
|
||||
map_res!(take_until!("\r\n"), str::from_utf8),
|
||||
MediaPlaylistType::from_str
|
||||
)
|
||||
);
|
||||
|
@ -396,13 +397,13 @@ pub enum SegmentTag {
|
|||
|
||||
pub fn media_segment_tag(input: &[u8]) -> IResult<&[u8], SegmentTag> {
|
||||
alt!(input,
|
||||
map!(chain!(tag!("#EXTINF:") ~ e:duration_title_tag,||e), |(a,b)| SegmentTag::Extinf(a,b))
|
||||
| map!(chain!(tag!("#EXT-X-BYTERANGE:") ~ r:byte_range_val, || r), SegmentTag::ByteRange)
|
||||
map!(do_parse!(tag!("#EXTINF:") >> e:duration_title_tag >> (e)), |(a,b)| SegmentTag::Extinf(a,b))
|
||||
| map!(do_parse!(tag!("#EXT-X-BYTERANGE:") >> r:byte_range_val >> (r)), SegmentTag::ByteRange)
|
||||
| map!(tag!("#EXT-X-DISCONTINUITY"), |_| SegmentTag::Discontinuity)
|
||||
| map!(chain!(tag!("#EXT-X-KEY:") ~ k:key, || k), SegmentTag::Key)
|
||||
| map!(chain!(tag!("#EXT-X-MAP:") ~ m:map, || m), SegmentTag::Map)
|
||||
| map!(chain!(tag!("#EXT-X-PROGRAM-DATE-TIME:") ~ t:consume_line, || t), SegmentTag::ProgramDateTime)
|
||||
| map!(chain!(tag!("#EXT-X-DATE-RANGE:") ~ t:consume_line, || t), SegmentTag::DateRange)
|
||||
| map!(do_parse!(tag!("#EXT-X-KEY:") >> k:key >> (k)), SegmentTag::Key)
|
||||
| map!(do_parse!(tag!("#EXT-X-MAP:") >> m:map >> (m)), SegmentTag::Map)
|
||||
| map!(do_parse!(tag!("#EXT-X-PROGRAM-DATE-TIME:") >> t:consume_line >> (t)), SegmentTag::ProgramDateTime)
|
||||
| map!(do_parse!(tag!("#EXT-X-DATE-RANGE:") >> t:consume_line >> (t)), SegmentTag::DateRange)
|
||||
|
||||
| map!(ext_tag, SegmentTag::Unknown)
|
||||
| map!(comment_tag, SegmentTag::Comment)
|
||||
|
@ -412,13 +413,13 @@ pub fn media_segment_tag(input: &[u8]) -> IResult<&[u8], SegmentTag> {
|
|||
}
|
||||
|
||||
named!(pub duration_title_tag<(f32, Option<String>)>,
|
||||
chain!(
|
||||
duration: float
|
||||
~ tag!(",")?
|
||||
~ title: opt!(map_res!(take_until_either_and_consume!("\r\n,"), from_utf8_slice))
|
||||
~ tag!(",")?
|
||||
,
|
||||
|| (duration, title)
|
||||
do_parse!(
|
||||
duration: float
|
||||
>> opt!(tag!(","))
|
||||
>> title: opt!(map_res!(take_until!("\r\n,"), from_utf8_slice))
|
||||
>> opt!(tag!(","))
|
||||
>>
|
||||
(duration, title)
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -435,30 +436,32 @@ named!(pub m3u_tag<String>,
|
|||
);
|
||||
|
||||
named!(pub version_tag<usize>,
|
||||
chain!(
|
||||
tag!("#EXT-X-VERSION:") ~ version: map_res!(digit, str::from_utf8),
|
||||
|| version.parse().unwrap_or_default()
|
||||
do_parse!(
|
||||
tag!("#EXT-X-VERSION:") >> version: map_res!(nom::character::streaming::digit1, str::from_utf8) >>
|
||||
(version.parse().unwrap_or_default())
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub start_tag<Start>,
|
||||
chain!(tag!("#EXT-X-START:") ~ attributes:key_value_pairs, || Start::from_hashmap(attributes))
|
||||
do_parse!(tag!("#EXT-X-START:") >> attributes:key_value_pairs >>
|
||||
(Start::from_hashmap(attributes))
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub ext_tag<ExtTag>,
|
||||
chain!(
|
||||
do_parse!(
|
||||
tag!("#EXT-")
|
||||
~ tag: map_res!(take_until_and_consume!(":"), from_utf8_slice)
|
||||
~ rest: map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice)
|
||||
,
|
||||
|| ExtTag { tag: tag, rest: rest }
|
||||
>> tag: map_res!(take_until!(":"), from_utf8_slice)
|
||||
>> rest: map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
>>
|
||||
(ExtTag { tag: tag, rest: rest })
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub comment_tag<String>,
|
||||
chain!(
|
||||
tag!("#") ~ text: map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice),
|
||||
|| text
|
||||
do_parse!(
|
||||
tag!("#") >> text: map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
>> (text)
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -468,7 +471,7 @@ named!(pub comment_tag<String>,
|
|||
|
||||
named!(pub key_value_pairs(&[u8]) -> HashMap<String, String>,
|
||||
map!(
|
||||
many0!(chain!(space? ~ k:key_value_pair,|| k))
|
||||
many0!(do_parse!(opt!(nom::character::streaming::space1) >> k:key_value_pair >> (k) ))
|
||||
,
|
||||
|pairs: Vec<(String, String)>| {
|
||||
pairs.into_iter().collect()
|
||||
|
@ -477,13 +480,14 @@ named!(pub key_value_pairs(&[u8]) -> HashMap<String, String>,
|
|||
);
|
||||
|
||||
named!(pub key_value_pair(&[u8]) -> (String, String),
|
||||
chain!(
|
||||
do_parse!(
|
||||
peek!(none_of!("\r\n"))
|
||||
~ left: map_res!(take_until_and_consume!("="), from_utf8_slice)
|
||||
~ right: alt!(quoted | unquoted)
|
||||
~ char!(',')?
|
||||
,
|
||||
|| (left, right)
|
||||
>> left: map_res!(take_until!("="), from_utf8_slice)
|
||||
>> right: alt!(quoted | unquoted)
|
||||
>> opt!(char!(','))
|
||||
>>
|
||||
|
||||
(left, right)
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -492,31 +496,30 @@ named!(pub quoted<String>,
|
|||
);
|
||||
|
||||
named!(pub unquoted<String>,
|
||||
map_res!(take_until_either!(",\r\n"), from_utf8_slice)
|
||||
map_res!(take_until!(",\r\n"), from_utf8_slice)
|
||||
);
|
||||
|
||||
named!(pub consume_line<String>,
|
||||
map_res!(take_until_either_and_consume!("\r\n"), from_utf8_slice)
|
||||
map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
);
|
||||
|
||||
named!(pub number<i32>,
|
||||
map_res!(map_res!(digit, str::from_utf8), str::FromStr::from_str)
|
||||
map_res!(map_res!(nom::character::streaming::digit1, str::from_utf8), str::FromStr::from_str)
|
||||
);
|
||||
|
||||
named!(pub byte_range_val<ByteRange>,
|
||||
chain!(
|
||||
do_parse!(
|
||||
n: number
|
||||
~ o: opt!(chain!(char!('@') ~ n:number,||n))
|
||||
,
|
||||
|| ByteRange { length: n, offset: o }
|
||||
>> o: opt!(do_parse!(char!('@') >> n:number >> (n))) >>
|
||||
(ByteRange { length: n, offset: o })
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub float<f32>,
|
||||
chain!(
|
||||
left: map_res!(digit, str::from_utf8)
|
||||
~ right_opt: opt!(chain!(char!('.') ~ d:map_res!(digit, str::from_utf8),|| d)),
|
||||
||
|
||||
do_parse!(
|
||||
left: map_res!(nom::character::streaming::digit1, str::from_utf8)
|
||||
>> right_opt: opt!(do_parse!(char!('.') >> d:map_res!(nom::character::streaming::digit1, str::from_utf8) >> (d) )) >>
|
||||
(
|
||||
match right_opt {
|
||||
Some(right) => {
|
||||
let mut num = String::from(left);
|
||||
|
@ -525,7 +528,7 @@ named!(pub float<f32>,
|
|||
num.parse().unwrap()
|
||||
},
|
||||
None => left.parse().unwrap(),
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in a new issue