Run parser through cargo fmt

Now that we don't use the nom macros anymore this works properly.
This commit is contained in:
Sebastian Dröge 2021-11-17 15:59:31 +02:00
parent 0ed0ce51f8
commit a44c2a1a72

View file

@ -76,16 +76,18 @@
pub mod playlist; pub mod playlist;
use nom::bytes::complete::{tag, take, is_not, is_a, take_while1, take_until};
use nom::character::is_digit;
use nom::character::complete::{digit1, multispace0, space0, line_ending, not_line_ending, char, none_of};
use nom::sequence::{delimited, preceded, tuple, pair, terminated};
use nom::combinator::{map, map_res, opt, peek, eof, complete};
use nom::multi::{fold_many0, many0};
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::{is_a, is_not, tag, take, take_until, take_while1};
use nom::character::complete::{
char, digit1, line_ending, multispace0, none_of, not_line_ending, space0,
};
use nom::character::is_digit;
use nom::combinator::{complete, eof, map, map_res, opt, peek};
use nom::multi::{fold_many0, many0};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::IResult;
use crate::playlist::*; use crate::playlist::*;
use nom::IResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::f32; use std::f32;
use std::result::Result; use std::result::Result;
@ -225,14 +227,12 @@ pub fn is_master_playlist_tag_line(i: &[u8]) -> IResult<&[u8], Option<(bool, Str
map(terminated(tag("#EXT-X-MEDIA"), is_not("-")), |t| (true, t)), // terminated() to prevent matching with #EXT-X-MEDIA-SEQUENCE for which we have a separate pattern below map(terminated(tag("#EXT-X-MEDIA"), is_not("-")), |t| (true, t)), // terminated() to prevent matching with #EXT-X-MEDIA-SEQUENCE for which we have a separate pattern below
map(tag("#EXT-X-SESSION-KEY"), |t| (true, t)), map(tag("#EXT-X-SESSION-KEY"), |t| (true, t)),
map(tag("#EXT-X-SESSION-DATA"), |t| (true, t)), map(tag("#EXT-X-SESSION-DATA"), |t| (true, t)),
map(tag("#EXT-X-TARGETDURATION"), |t| (false, t)), map(tag("#EXT-X-TARGETDURATION"), |t| (false, t)),
map(tag("#EXT-X-MEDIA-SEQUENCE"), |t| (false, t)), map(tag("#EXT-X-MEDIA-SEQUENCE"), |t| (false, t)),
map(tag("#EXT-X-DISCONTINUITY-SEQUENCE"), |t| (false, t)), map(tag("#EXT-X-DISCONTINUITY-SEQUENCE"), |t| (false, t)),
map(tag("#EXT-X-ENDLIST"), |t| (false, t)), map(tag("#EXT-X-ENDLIST"), |t| (false, t)),
map(tag("#EXT-X-PLAYLIST-TYPE"), |t| (false, t)), map(tag("#EXT-X-PLAYLIST-TYPE"), |t| (false, t)),
map(tag("#EXT-X-I-FRAMES-ONLY"), |t| (false, t)), map(tag("#EXT-X-I-FRAMES-ONLY"), |t| (false, t)),
map(tag("#EXTINF"), |t| (false, t)), map(tag("#EXTINF"), |t| (false, t)),
map(tag("#EXT-X-BYTERANGE"), |t| (false, t)), map(tag("#EXT-X-BYTERANGE"), |t| (false, t)),
map(tag("#EXT-X-DISCONTINUITY"), |t| (false, t)), map(tag("#EXT-X-DISCONTINUITY"), |t| (false, t)),
@ -243,7 +243,7 @@ pub fn is_master_playlist_tag_line(i: &[u8]) -> IResult<&[u8], Option<(bool, Str
))), ))),
consume_line, consume_line,
)), )),
|(_, tag, _)| tag.map(|(a,b)| (a, from_utf8_slice(b).unwrap())), |(_, tag, _)| tag.map(|(a, b)| (a, from_utf8_slice(b).unwrap())),
)(i) )(i)
} }
@ -254,14 +254,10 @@ pub fn is_master_playlist_tag_line(i: &[u8]) -> IResult<&[u8], Option<(bool, Str
pub fn parse_master_playlist_tags(i: &[u8]) -> IResult<&[u8], Vec<MasterPlaylistTag>> { pub fn parse_master_playlist_tags(i: &[u8]) -> IResult<&[u8], Vec<MasterPlaylistTag>> {
map( map(
tuple(( tuple((
many0( many0(complete(map(
complete(
map(
pair(master_playlist_tag, multispace0), pair(master_playlist_tag, multispace0),
|(tag, _)| tag, |(tag, _)| tag,
) ))),
)
),
opt(eof), opt(eof),
)), )),
|(tags, _)| { |(tags, _)| {
@ -301,10 +297,9 @@ pub fn master_playlist_tag(i: &[u8]) -> IResult<&[u8], MasterPlaylistTag> {
map(session_data_tag, MasterPlaylistTag::SessionData), map(session_data_tag, MasterPlaylistTag::SessionData),
map(session_key_tag, MasterPlaylistTag::SessionKey), map(session_key_tag, MasterPlaylistTag::SessionKey),
map(start_tag, MasterPlaylistTag::Start), map(start_tag, MasterPlaylistTag::Start),
map( map(tag("#EXT-X-INDEPENDENT-SEGMENTS"), |_| {
tag("#EXT-X-INDEPENDENT-SEGMENTS"), MasterPlaylistTag::IndependentSegments
|_| MasterPlaylistTag::IndependentSegments, }),
),
map(ext_tag, MasterPlaylistTag::Unknown), map(ext_tag, MasterPlaylistTag::Unknown),
map(comment_tag, MasterPlaylistTag::Comment), map(comment_tag, MasterPlaylistTag::Comment),
map(consume_line, MasterPlaylistTag::Uri), map(consume_line, MasterPlaylistTag::Uri),
@ -354,52 +349,35 @@ pub fn master_playlist_from_tags(mut tags: Vec<MasterPlaylistTag>) -> MasterPlay
pub fn variant_stream_tag(i: &[u8]) -> IResult<&[u8], VariantStream> { pub fn variant_stream_tag(i: &[u8]) -> IResult<&[u8], VariantStream> {
map( map(
pair( pair(tag("#EXT-X-STREAM-INF:"), key_value_pairs),
tag("#EXT-X-STREAM-INF:"),
key_value_pairs,
),
|(_, attributes)| VariantStream::from_hashmap(attributes, false), |(_, attributes)| VariantStream::from_hashmap(attributes, false),
)(i) )(i)
} }
pub fn variant_i_frame_stream_tag(i: &[u8]) -> IResult<&[u8], VariantStream> { pub fn variant_i_frame_stream_tag(i: &[u8]) -> IResult<&[u8], VariantStream> {
map( map(
pair( pair(tag("#EXT-X-I-FRAME-STREAM-INF:"), key_value_pairs),
tag("#EXT-X-I-FRAME-STREAM-INF:"),
key_value_pairs,
),
|(_, attributes)| VariantStream::from_hashmap(attributes, true), |(_, attributes)| VariantStream::from_hashmap(attributes, true),
)(i) )(i)
} }
pub fn alternative_media_tag(i: &[u8]) -> IResult<&[u8], AlternativeMedia> { pub fn alternative_media_tag(i: &[u8]) -> IResult<&[u8], AlternativeMedia> {
map( map(pair(tag("#EXT-X-MEDIA:"), key_value_pairs), |(_, media)| {
pair( AlternativeMedia::from_hashmap(media)
tag("#EXT-X-MEDIA:"), })(i)
key_value_pairs,
),
|(_, media)| AlternativeMedia::from_hashmap(media),
)(i)
} }
pub fn session_data_tag(i: &[u8]) -> IResult<&[u8], SessionData> { pub fn session_data_tag(i: &[u8]) -> IResult<&[u8], SessionData> {
map_res( map_res(
pair( pair(tag("#EXT-X-SESSION-DATA:"), key_value_pairs),
tag("#EXT-X-SESSION-DATA:"),
key_value_pairs,
),
|(_, session_data)| SessionData::from_hashmap(session_data), |(_, session_data)| SessionData::from_hashmap(session_data),
)(i) )(i)
} }
pub fn session_key_tag(i: &[u8]) -> IResult<&[u8], SessionKey> { pub fn session_key_tag(i: &[u8]) -> IResult<&[u8], SessionKey> {
map( map(pair(tag("#EXT-X-SESSION-KEY:"), key), |(_, key)| {
pair( SessionKey(key)
tag("#EXT-X-SESSION-KEY:"), })(i)
key,
),
|(_, key)| SessionKey(key),
)(i)
} }
// ----------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------
@ -409,14 +387,10 @@ pub fn session_key_tag(i: &[u8]) -> IResult<&[u8], SessionKey> {
pub fn parse_media_playlist_tags(i: &[u8]) -> IResult<&[u8], Vec<MediaPlaylistTag>> { pub fn parse_media_playlist_tags(i: &[u8]) -> IResult<&[u8], Vec<MediaPlaylistTag>> {
map( map(
tuple(( tuple((
many0( many0(complete(map(
complete(
map(
pair(media_playlist_tag, multispace0), pair(media_playlist_tag, multispace0),
|(tag, _)| tag, |(tag, _)| tag,
) ))),
)
),
opt(eof), opt(eof),
)), )),
|(tags, _)| { |(tags, _)| {
@ -452,46 +426,29 @@ pub fn media_playlist_tag(i: &[u8]) -> IResult<&[u8], MediaPlaylistTag> {
map(m3u_tag, MediaPlaylistTag::M3U), map(m3u_tag, MediaPlaylistTag::M3U),
map(version_tag, MediaPlaylistTag::Version), map(version_tag, MediaPlaylistTag::Version),
map( map(
pair( pair(tag("#EXT-X-TARGETDURATION:"), float),
tag("#EXT-X-TARGETDURATION:"),
float,
),
|(_, duration)| MediaPlaylistTag::TargetDuration(duration), |(_, duration)| MediaPlaylistTag::TargetDuration(duration),
), ),
map( map(
pair( pair(tag("#EXT-X-MEDIA-SEQUENCE:"), number),
tag("#EXT-X-MEDIA-SEQUENCE:"),
number,
),
|(_, sequence)| MediaPlaylistTag::MediaSequence(sequence), |(_, sequence)| MediaPlaylistTag::MediaSequence(sequence),
), ),
map( map(
pair( pair(tag("#EXT-X-DISCONTINUITY-SEQUENCE:"), number),
tag("#EXT-X-DISCONTINUITY-SEQUENCE:"),
number,
),
|(_, sequence)| MediaPlaylistTag::DiscontinuitySequence(sequence), |(_, sequence)| MediaPlaylistTag::DiscontinuitySequence(sequence),
), ),
map( map(
pair( pair(tag("#EXT-X-PLAYLIST-TYPE:"), playlist_type),
tag("#EXT-X-PLAYLIST-TYPE:"),
playlist_type,
),
|(_, typ)| MediaPlaylistTag::PlaylistType(typ), |(_, typ)| MediaPlaylistTag::PlaylistType(typ),
), ),
map( map(tag("#EXT-X-I-FRAMES-ONLY"), |_| {
tag("#EXT-X-I-FRAMES-ONLY"), MediaPlaylistTag::IFramesOnly
|_| MediaPlaylistTag::IFramesOnly, }),
),
map(start_tag, MediaPlaylistTag::Start), map(start_tag, MediaPlaylistTag::Start),
map( map(tag("#EXT-X-INDEPENDENT-SEGMENTS"), |_| {
tag("#EXT-X-INDEPENDENT-SEGMENTS"), MediaPlaylistTag::IndependentSegments
|_| MediaPlaylistTag::IndependentSegments, }),
), map(tag("#EXT-X-ENDLIST"), |_| MediaPlaylistTag::EndList),
map(
tag("#EXT-X-ENDLIST"),
|_| MediaPlaylistTag::EndList,
),
map(media_segment_tag, MediaPlaylistTag::Segment), map(media_segment_tag, MediaPlaylistTag::Segment),
))(i) ))(i)
} }
@ -576,10 +533,7 @@ pub fn media_playlist_from_tags(mut tags: Vec<MediaPlaylistTag>) -> MediaPlaylis
pub fn playlist_type(i: &[u8]) -> IResult<&[u8], MediaPlaylistType> { pub fn playlist_type(i: &[u8]) -> IResult<&[u8], MediaPlaylistType> {
map_res( map_res(
tuple(( tuple((map_res(is_not("\r\n"), str::from_utf8), take(1usize))),
map_res(is_not("\r\n"), str::from_utf8),
take(1usize),
)),
|(typ, _)| MediaPlaylistType::from_str(typ), |(typ, _)| MediaPlaylistType::from_str(typ),
)(i) )(i)
} }
@ -606,49 +560,26 @@ pub enum SegmentTag {
pub fn media_segment_tag(i: &[u8]) -> IResult<&[u8], SegmentTag> { pub fn media_segment_tag(i: &[u8]) -> IResult<&[u8], SegmentTag> {
alt(( alt((
map( map(
pair( pair(tag("#EXTINF:"), duration_title_tag),
tag("#EXTINF:"),
duration_title_tag,
),
|(_, (duration, title))| SegmentTag::Extinf(duration, title), |(_, (duration, title))| SegmentTag::Extinf(duration, title),
), ),
map( map(
pair( pair(tag("#EXT-X-BYTERANGE:"), byte_range_val),
tag("#EXT-X-BYTERANGE:"),
byte_range_val,
),
|(_, range)| SegmentTag::ByteRange(range), |(_, range)| SegmentTag::ByteRange(range),
), ),
map(tag("#EXT-X-DISCONTINUITY"), |_| SegmentTag::Discontinuity),
map(pair(tag("#EXT-X-KEY:"), key), |(_, key)| {
SegmentTag::Key(key)
}),
map(pair(tag("#EXT-X-MAP:"), extmap), |(_, map)| {
SegmentTag::Map(map)
}),
map( map(
tag("#EXT-X-DISCONTINUITY"), pair(tag("#EXT-X-PROGRAM-DATE-TIME:"), consume_line),
|_| SegmentTag::Discontinuity
),
map(
pair(
tag("#EXT-X-KEY:"),
key,
),
|(_, key)| SegmentTag::Key(key),
),
map(
pair(
tag("#EXT-X-MAP:"),
extmap,
),
|(_, map)| SegmentTag::Map(map),
),
map(
pair(
tag("#EXT-X-PROGRAM-DATE-TIME:"),
consume_line,
),
|(_, line)| SegmentTag::ProgramDateTime(line), |(_, line)| SegmentTag::ProgramDateTime(line),
), ),
map( map(
pair( pair(tag("#EXT-X-DATE-RANGE:"), consume_line),
tag("#EXT-X-DATE-RANGE:"),
consume_line,
),
|(_, line)| SegmentTag::DateRange(line), |(_, line)| SegmentTag::DateRange(line),
), ),
map(ext_tag, SegmentTag::Unknown), map(ext_tag, SegmentTag::Unknown),
@ -662,9 +593,7 @@ pub fn duration_title_tag(i: &[u8]) -> IResult<&[u8], (f32, Option<String>)> {
tuple(( tuple((
float, float,
opt(char(',')), opt(char(',')),
opt( opt(map_res(is_not("\r\n,"), from_utf8_slice)),
map_res(is_not("\r\n,"), from_utf8_slice),
),
take(1usize), take(1usize),
opt(char(',')), opt(char(',')),
)), )),
@ -673,30 +602,22 @@ pub fn duration_title_tag(i: &[u8]) -> IResult<&[u8], (f32, Option<String>)> {
} }
pub fn key(i: &[u8]) -> IResult<&[u8], Key> { pub fn key(i: &[u8]) -> IResult<&[u8], Key> {
map( map(key_value_pairs, Key::from_hashmap)(i)
key_value_pairs,
Key::from_hashmap,
)(i)
} }
pub fn extmap(i: &[u8]) -> IResult<&[u8], Map> { pub fn extmap(i: &[u8]) -> IResult<&[u8], Map> {
map_res( map_res(key_value_pairs, |attrs| -> Result<Map, &str> {
key_value_pairs,
|attrs| -> Result<Map, &str> {
let uri = attrs.get("URI").cloned().unwrap_or_default(); let uri = attrs.get("URI").cloned().unwrap_or_default();
let byte_range = attrs.get("BYTERANGE").map(|range| let byte_range = attrs
match byte_range_val(range.as_bytes()) { .get("BYTERANGE")
.map(|range| match byte_range_val(range.as_bytes()) {
IResult::Ok((_, range)) => Ok(range), IResult::Ok((_, range)) => Ok(range),
IResult::Err(_) => Err("invalid byte range"), IResult::Err(_) => Err("invalid byte range"),
}
).transpose()?;
Ok(Map {
uri,
byte_range,
}) })
} .transpose()?;
)(i)
Ok(Map { uri, byte_range })
})(i)
} }
// ----------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------
@ -704,33 +625,20 @@ pub fn extmap(i: &[u8]) -> IResult<&[u8], Map> {
// ----------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------
pub fn m3u_tag(i: &[u8]) -> IResult<&[u8], String> { pub fn m3u_tag(i: &[u8]) -> IResult<&[u8], String> {
map_res( map_res(tag("#EXTM3U"), from_utf8_slice)(i)
tag("#EXTM3U"),
from_utf8_slice,
)(i)
} }
pub fn version_tag(i: &[u8]) -> IResult<&[u8], usize> { pub fn version_tag(i: &[u8]) -> IResult<&[u8], usize> {
map( map(
pair( pair(tag("#EXT-X-VERSION:"), map_res(digit1, str::from_utf8)),
tag("#EXT-X-VERSION:"), |(_, version)| version.parse().unwrap_or_default(),
map_res(digit1, str::from_utf8),
),
|(_, version)| {
version.parse().unwrap_or_default()
}
)(i) )(i)
} }
pub fn start_tag(i: &[u8]) -> IResult<&[u8], Start> { pub fn start_tag(i: &[u8]) -> IResult<&[u8], Start> {
map( map(
pair( pair(tag("#EXT-X-START:"), key_value_pairs),
tag("#EXT-X-START:"), |(_, attributes)| Start::from_hashmap(attributes),
key_value_pairs,
),
|(_, attributes)| {
Start::from_hashmap(attributes)
}
)(i) )(i)
} }
@ -743,19 +651,14 @@ pub fn ext_tag(i: &[u8]) -> IResult<&[u8], ExtTag> {
opt(map_res(is_not("\r\n"), from_utf8_slice)), opt(map_res(is_not("\r\n"), from_utf8_slice)),
take(1usize), take(1usize),
)), )),
|(_, tag, _, rest, _)| { |(_, tag, _, rest, _)| ExtTag { tag, rest },
ExtTag { tag, rest }
}
)(i) )(i)
} }
pub fn comment_tag(i: &[u8]) -> IResult<&[u8], String> { pub fn comment_tag(i: &[u8]) -> IResult<&[u8], String> {
map( map(
pair( pair(
preceded( preceded(char('#'), map_res(is_not("\r\n"), from_utf8_slice)),
char('#'),
map_res(is_not("\r\n"), from_utf8_slice),
),
take(1usize), take(1usize),
), ),
|(text, _)| text, |(text, _)| text,
@ -773,7 +676,7 @@ pub fn key_value_pairs(i: &[u8]) -> IResult<&[u8], HashMap<String, String>> {
|mut acc: HashMap<_, _>, (left, right)| { |mut acc: HashMap<_, _>, (left, right)| {
acc.insert(left, right); acc.insert(left, right);
acc acc
} },
)(i) )(i)
} }
@ -786,43 +689,31 @@ pub fn key_value_pair(i: &[u8]) -> IResult<&[u8], (String, String)> {
alt((quoted, unquoted)), alt((quoted, unquoted)),
opt(char(',')), opt(char(',')),
)), )),
|(_, left, _, right, _)| { |(_, left, _, right, _)| (left, right),
(left, right)
}
)(i) )(i)
} }
pub fn quoted(i: &[u8]) -> IResult<&[u8], String> { pub fn quoted(i: &[u8]) -> IResult<&[u8], String> {
delimited( delimited(
char('\"'), char('\"'),
map_res( map_res(is_not("\""), from_utf8_slice),
is_not("\""), char('\"'),
from_utf8_slice
),
char('\"')
)(i) )(i)
} }
pub fn unquoted(i: &[u8]) -> IResult<&[u8], String> { pub fn unquoted(i: &[u8]) -> IResult<&[u8], String> {
map_res( map_res(is_not(",\r\n"), from_utf8_slice)(i)
is_not(",\r\n"),
from_utf8_slice
)(i)
} }
pub fn consume_line(i: &[u8]) -> IResult<&[u8], String> { pub fn consume_line(i: &[u8]) -> IResult<&[u8], String> {
map( map(
pair( pair(map_res(not_line_ending, from_utf8_slice), opt(line_ending)),
map_res(not_line_ending, from_utf8_slice),
opt(line_ending),
),
|(line, _)| line, |(line, _)| line,
)(i) )(i)
} }
pub fn number(i: &[u8]) -> IResult<&[u8], i32> { pub fn number(i: &[u8]) -> IResult<&[u8], i32> {
map_res(take_while1(is_digit), map_res(take_while1(is_digit), |s| {
|s| {
// Can't fail because we validated it above already // Can't fail because we validated it above already
let s = str::from_utf8(s).unwrap(); let s = str::from_utf8(s).unwrap();
str::parse::<i32>(s) str::parse::<i32>(s)
@ -830,26 +721,19 @@ pub fn number(i: &[u8]) -> IResult<&[u8], i32> {
} }
pub fn byte_range_val(i: &[u8]) -> IResult<&[u8], ByteRange> { pub fn byte_range_val(i: &[u8]) -> IResult<&[u8], ByteRange> {
map( map(pair(number, opt(preceded(char('@'), number))), |(n, o)| {
pair( ByteRange {
number, length: n,
opt( offset: o,
preceded(char('@'), number)
),
),
|(n, o)| {
ByteRange { length: n, offset: o }
} }
)(i) })(i)
} }
pub fn float(i: &[u8]) -> IResult<&[u8], f32> { pub fn float(i: &[u8]) -> IResult<&[u8], f32> {
map_res( map_res(
pair( pair(
take_while1(is_digit), take_while1(is_digit),
opt( opt(preceded(char('.'), take_while1(is_digit))),
preceded(char('.'), take_while1(is_digit))
),
), ),
|(left, right): (&[u8], Option<&[u8]>)| match right { |(left, right): (&[u8], Option<&[u8]>)| match right {
Some(right) => { Some(right) => {
@ -863,7 +747,7 @@ pub fn float(i: &[u8]) -> IResult<&[u8], f32> {
let left = str::from_utf8(left).unwrap(); let left = str::from_utf8(left).unwrap();
left.parse() left.parse()
} }
} },
)(i) )(i)
} }