mirror of
https://github.com/rutgersc/m3u8-rs.git
synced 2025-01-03 11:48:41 +00:00
Upgraded macros to Nom 5
Changes: IResult::Done - IResult::Ok chain! -> do_parse! slightly different syntax with `?` -> opt! digit -> digit1 space? -> space0 multispace? -> multispace0 many0!() -> many0!(complete!()) take_until_either! -> is_not! take_until_and_consume! -> take_until! + take!(1) take_until_either_and_consume! -> is_not! + take!(1)
This commit is contained in:
parent
fc9f45dd18
commit
b9d377bfa4
1 changed files with 48 additions and 26 deletions
62
src/lib.rs
62
src/lib.rs
|
@ -92,7 +92,10 @@ extern crate nom;
|
|||
|
||||
pub mod playlist;
|
||||
|
||||
use nom::*;
|
||||
use nom::character::complete::{digit1, multispace0, space0 };
|
||||
use nom::{IResult};
|
||||
use nom::{ delimited,none_of,peek,is_not,complete,terminated,tag,
|
||||
alt,do_parse,opt,named,map,map_res,eof,many0,take,take_until,char};
|
||||
|
||||
use std::str;
|
||||
use std::f32;
|
||||
|
@ -244,8 +247,9 @@ 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()))
|
||||
} )
|
||||
|
@ -258,11 +262,14 @@ named!(pub is_master_playlist_tag_line(&[u8]) -> Option<(bool, String)>,
|
|||
|
||||
pub fn parse_master_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MasterPlaylistTag>> {
|
||||
do_parse!(input,
|
||||
tags: many0!(do_parse!(m:master_playlist_tag >> opt!(character::complete::multispace1) >> (m) )) >> opt!(eof!()) >>
|
||||
( { tags.reverse(); tags } )
|
||||
tags: many0!(complete!(do_parse!( m : master_playlist_tag >> multispace0 >> (m) )))
|
||||
>> opt!(eof!())
|
||||
>>
|
||||
( {let mut tags_rev: Vec<MasterPlaylistTag> = tags; tags_rev.reverse(); tags_rev } )
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/// Contains all the tags required to parse a master playlist.
|
||||
#[derive(Debug)]
|
||||
pub enum MasterPlaylistTag {
|
||||
|
@ -330,8 +337,9 @@ named!(pub session_key_tag<SessionKey>,
|
|||
|
||||
pub fn parse_media_playlist_tags(input: &[u8]) -> IResult<&[u8], Vec<MediaPlaylistTag>> {
|
||||
do_parse!(input,
|
||||
tags: many0!(do_parse!(m:media_playlist_tag >> opt!(character::complete::multispace1) >> (m) )) >> opt!(eof!()) >>
|
||||
( { tags.reverse(); tags } )
|
||||
tags: many0!(complete!(do_parse!(m:media_playlist_tag >> multispace0 >> (m) ))) >> opt!(eof!())
|
||||
>>
|
||||
( {let mut tags_rev: Vec<MediaPlaylistTag> = tags; tags_rev.reverse(); tags_rev } )
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -371,7 +379,11 @@ pub fn media_playlist_tag(input: &[u8]) -> IResult<&[u8], MediaPlaylistTag> {
|
|||
|
||||
named!(pub playlist_type<MediaPlaylistType>,
|
||||
map_res!(
|
||||
map_res!(take_until!("\r\n"), str::from_utf8),
|
||||
do_parse!(
|
||||
p: map_res!(is_not!("\r\n"), str::from_utf8)
|
||||
>> take!(1)
|
||||
>> (p)
|
||||
),
|
||||
MediaPlaylistType::from_str
|
||||
)
|
||||
);
|
||||
|
@ -416,7 +428,8 @@ named!(pub duration_title_tag<(f32, Option<String>)>,
|
|||
do_parse!(
|
||||
duration: float
|
||||
>> opt!(tag!(","))
|
||||
>> title: opt!(map_res!(take_until!("\r\n,"), from_utf8_slice))
|
||||
>> title: opt!(map_res!(is_not!("\r\n,"), from_utf8_slice))
|
||||
>> take!(1)
|
||||
>> opt!(tag!(","))
|
||||
>>
|
||||
(duration, title)
|
||||
|
@ -437,7 +450,7 @@ named!(pub m3u_tag<String>,
|
|||
|
||||
named!(pub version_tag<usize>,
|
||||
do_parse!(
|
||||
tag!("#EXT-X-VERSION:") >> version: map_res!(nom::character::streaming::digit1, str::from_utf8) >>
|
||||
tag!("#EXT-X-VERSION:") >> version: map_res!(digit1, str::from_utf8) >>
|
||||
(version.parse().unwrap_or_default())
|
||||
)
|
||||
);
|
||||
|
@ -452,7 +465,9 @@ named!(pub ext_tag<ExtTag>,
|
|||
do_parse!(
|
||||
tag!("#EXT-")
|
||||
>> tag: map_res!(take_until!(":"), from_utf8_slice)
|
||||
>> rest: map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
>> take!(1)
|
||||
>> rest: map_res!(is_not!("\r\n"), from_utf8_slice)
|
||||
>> take!(1)
|
||||
>>
|
||||
(ExtTag { tag: tag, rest: rest })
|
||||
)
|
||||
|
@ -460,7 +475,8 @@ named!(pub ext_tag<ExtTag>,
|
|||
|
||||
named!(pub comment_tag<String>,
|
||||
do_parse!(
|
||||
tag!("#") >> text: map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
tag!("#") >> text: map_res!(is_not!("\r\n"), from_utf8_slice)
|
||||
>> take!(1)
|
||||
>> (text)
|
||||
)
|
||||
);
|
||||
|
@ -471,7 +487,7 @@ named!(pub comment_tag<String>,
|
|||
|
||||
named!(pub key_value_pairs(&[u8]) -> HashMap<String, String>,
|
||||
map!(
|
||||
many0!(do_parse!(opt!(nom::character::streaming::space1) >> k:key_value_pair >> (k) ))
|
||||
many0!(do_parse!(space0 >> k:key_value_pair >> (k) ))
|
||||
,
|
||||
|pairs: Vec<(String, String)>| {
|
||||
pairs.into_iter().collect()
|
||||
|
@ -483,10 +499,10 @@ named!(pub key_value_pair(&[u8]) -> (String, String),
|
|||
do_parse!(
|
||||
peek!(none_of!("\r\n"))
|
||||
>> left: map_res!(take_until!("="), from_utf8_slice)
|
||||
>> take!(1)
|
||||
>> right: alt!(quoted | unquoted)
|
||||
>> opt!(char!(','))
|
||||
>>
|
||||
|
||||
(left, right)
|
||||
)
|
||||
);
|
||||
|
@ -496,29 +512,35 @@ named!(pub quoted<String>,
|
|||
);
|
||||
|
||||
named!(pub unquoted<String>,
|
||||
map_res!(take_until!(",\r\n"), from_utf8_slice)
|
||||
map_res!(is_not!(",\r\n"), from_utf8_slice)
|
||||
);
|
||||
|
||||
named!(pub consume_line<String>,
|
||||
map_res!(take_until!("\r\n"), from_utf8_slice)
|
||||
do_parse!(
|
||||
l: map_res!(is_not!("\r\n"), from_utf8_slice)
|
||||
>> take!(1)
|
||||
>>
|
||||
(l)
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub number<i32>,
|
||||
map_res!(map_res!(nom::character::streaming::digit1, str::from_utf8), str::FromStr::from_str)
|
||||
map_res!(map_res!(digit1, str::from_utf8), str::FromStr::from_str)
|
||||
);
|
||||
|
||||
named!(pub byte_range_val<ByteRange>,
|
||||
do_parse!(
|
||||
n: number
|
||||
>> o: opt!(do_parse!(char!('@') >> n:number >> (n))) >>
|
||||
>> o: opt!(do_parse!(char!('@') >> n:number >> (n) )) >>
|
||||
(ByteRange { length: n, offset: o })
|
||||
)
|
||||
);
|
||||
|
||||
named!(pub float<f32>,
|
||||
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) )) >>
|
||||
left: map_res!(digit1, str::from_utf8)
|
||||
>> right_opt: opt!(do_parse!(char!('.') >> d:map_res!(digit1, str::from_utf8) >> (d) ))
|
||||
>>
|
||||
(
|
||||
match right_opt {
|
||||
Some(right) => {
|
||||
|
|
Loading…
Reference in a new issue