1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-19 16:58:23 +00:00
hls_m3u8/src/line.rs

136 lines
5.3 KiB
Rust
Raw Normal View History

2020-02-10 12:20:39 +00:00
use core::convert::TryFrom;
2020-02-14 12:05:18 +00:00
use core::iter::FusedIterator;
2018-02-14 15:50:57 +00:00
2020-02-14 12:05:18 +00:00
use derive_more::Display;
2019-09-13 14:06:52 +00:00
use crate::tags;
use crate::types::PlaylistType;
2019-09-13 14:06:52 +00:00
use crate::Error;
2020-02-06 16:02:44 +00:00
#[derive(Debug, Clone)]
2020-02-02 13:33:57 +00:00
pub(crate) struct Lines<'a> {
2020-02-10 12:20:39 +00:00
lines: ::core::iter::FilterMap<::core::str::Lines<'a>, fn(&'a str) -> Option<&'a str>>,
2019-09-14 09:57:56 +00:00
}
2018-02-11 06:10:52 +00:00
2020-02-02 13:33:57 +00:00
impl<'a> Iterator for Lines<'a> {
2020-02-06 11:27:48 +00:00
type Item = crate::Result<Line<'a>>;
2019-09-14 09:57:56 +00:00
2020-02-02 13:33:57 +00:00
fn next(&mut self) -> Option<Self::Item> {
2020-02-10 12:20:39 +00:00
let line = self.lines.next()?;
if line.starts_with(tags::VariantStream::PREFIX_EXTXSTREAMINF) {
let uri = self.lines.next()?;
Some(
tags::VariantStream::try_from(format!("{}\n{}", line, uri).as_str())
2020-08-11 09:13:14 +00:00
.map(tags::VariantStream::into_owned)
2020-02-10 12:20:39 +00:00
.map(|v| Line::Tag(Tag::VariantStream(v))),
)
} else if line.starts_with("#EXT") {
Some(Tag::try_from(line).map(Line::Tag))
} else if line.starts_with('#') {
Some(Ok(Line::Comment(line)))
} else {
Some(Ok(Line::Uri(line)))
2018-02-11 06:10:52 +00:00
}
}
}
2019-09-13 14:06:52 +00:00
2020-02-14 12:05:18 +00:00
impl<'a> FusedIterator for Lines<'a> {}
2020-02-02 13:33:57 +00:00
impl<'a> From<&'a str> for Lines<'a> {
fn from(buffer: &'a str) -> Self {
Self {
2020-02-14 12:05:18 +00:00
lines: buffer
.lines()
.filter_map(|line| Some(line.trim()).filter(|v| !v.is_empty())),
2020-02-02 13:33:57 +00:00
}
}
2018-02-11 06:10:52 +00:00
}
#[derive(Debug, Clone, PartialEq)]
2020-02-06 11:27:48 +00:00
pub(crate) enum Line<'a> {
Tag(Tag<'a>),
2020-02-10 12:20:39 +00:00
Comment(&'a str),
2020-02-06 11:27:48 +00:00
Uri(&'a str),
2018-02-11 06:10:52 +00:00
}
2019-03-31 10:00:02 +00:00
#[allow(clippy::large_enum_variant)]
2020-02-14 12:05:18 +00:00
#[derive(Debug, Clone, PartialEq, Display)]
#[display(fmt = "{}")]
2020-02-06 11:27:48 +00:00
pub(crate) enum Tag<'a> {
2018-02-14 19:51:44 +00:00
ExtXVersion(tags::ExtXVersion),
ExtInf(tags::ExtInf<'a>),
2018-02-14 19:51:44 +00:00
ExtXByteRange(tags::ExtXByteRange),
ExtXDiscontinuity(tags::ExtXDiscontinuity),
ExtXKey(tags::ExtXKey<'a>),
ExtXMap(tags::ExtXMap<'a>),
ExtXProgramDateTime(tags::ExtXProgramDateTime<'a>),
ExtXDateRange(tags::ExtXDateRange<'a>),
2018-02-14 19:51:44 +00:00
ExtXTargetDuration(tags::ExtXTargetDuration),
ExtXMediaSequence(tags::ExtXMediaSequence),
ExtXDiscontinuitySequence(tags::ExtXDiscontinuitySequence),
ExtXEndList(tags::ExtXEndList),
PlaylistType(PlaylistType),
2018-02-14 19:51:44 +00:00
ExtXIFramesOnly(tags::ExtXIFramesOnly),
ExtXMedia(tags::ExtXMedia<'a>),
ExtXSessionData(tags::ExtXSessionData<'a>),
ExtXSessionKey(tags::ExtXSessionKey<'a>),
2018-02-14 19:51:44 +00:00
ExtXIndependentSegments(tags::ExtXIndependentSegments),
ExtXStart(tags::ExtXStart),
VariantStream(tags::VariantStream<'a>),
2020-02-06 11:27:48 +00:00
Unknown(&'a str),
2018-02-14 15:50:57 +00:00
}
2019-09-13 14:06:52 +00:00
2020-02-06 11:27:48 +00:00
impl<'a> TryFrom<&'a str> for Tag<'a> {
type Error = Error;
2019-09-13 14:06:52 +00:00
2020-02-06 11:27:48 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
if input.starts_with(tags::ExtXVersion::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXVersion)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtInf::PREFIX) {
TryFrom::try_from(input).map(Self::ExtInf)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXByteRange::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXByteRange)
2020-08-11 08:36:44 +00:00
} else if input.starts_with(tags::ExtXDiscontinuitySequence::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXDiscontinuitySequence)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXDiscontinuity::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXDiscontinuity)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXKey::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXKey)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXMap::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXMap)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXProgramDateTime::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXProgramDateTime)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXTargetDuration::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXTargetDuration)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXDateRange::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXDateRange)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXMediaSequence::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXMediaSequence)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXEndList::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXEndList)
} else if input.starts_with(PlaylistType::PREFIX) {
TryFrom::try_from(input).map(Self::PlaylistType)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXIFramesOnly::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXIFramesOnly)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXMedia::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXMedia)
2020-02-10 12:20:39 +00:00
} else if input.starts_with(tags::VariantStream::PREFIX_EXTXIFRAME)
|| input.starts_with(tags::VariantStream::PREFIX_EXTXSTREAMINF)
{
TryFrom::try_from(input).map(Self::VariantStream)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXSessionData::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXSessionData)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXSessionKey::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXSessionKey)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXIndependentSegments::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXIndependentSegments)
2019-10-03 14:23:27 +00:00
} else if input.starts_with(tags::ExtXStart::PREFIX) {
TryFrom::try_from(input).map(Self::ExtXStart)
2018-02-14 15:50:57 +00:00
} else {
2020-02-06 11:27:48 +00:00
Ok(Self::Unknown(input))
2018-02-14 15:50:57 +00:00
}
}
}