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;
|
2020-03-25 11:17:03 +00:00
|
|
|
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(
|
2020-04-22 07:54:48 +00:00
|
|
|
tags::VariantStream::try_from(format!("{}\n{}", line, uri).as_str())
|
|
|
|
.map(|v| v.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
|
|
|
}
|
|
|
|
|
2019-10-05 11:23:41 +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),
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtInf(tags::ExtInf<'a>),
|
2018-02-14 19:51:44 +00:00
|
|
|
ExtXByteRange(tags::ExtXByteRange),
|
|
|
|
ExtXDiscontinuity(tags::ExtXDiscontinuity),
|
2020-04-22 07:54:48 +00:00
|
|
|
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),
|
2020-03-25 11:17:03 +00:00
|
|
|
PlaylistType(PlaylistType),
|
2018-02-14 19:51:44 +00:00
|
|
|
ExtXIFramesOnly(tags::ExtXIFramesOnly),
|
2020-04-22 07:54:48 +00:00
|
|
|
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),
|
2020-04-22 07:54:48 +00:00
|
|
|
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) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXVersion)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtInf::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtInf)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXByteRange::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXByteRange)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXDiscontinuity::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXDiscontinuity)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXKey::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXKey)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXMap::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXMap)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXProgramDateTime::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXProgramDateTime)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXTargetDuration::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXTargetDuration)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXDateRange::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXDateRange)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXMediaSequence::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXMediaSequence)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXDiscontinuitySequence::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXDiscontinuitySequence)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXEndList::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXEndList)
|
2020-03-25 11:17:03 +00:00
|
|
|
} else if input.starts_with(PlaylistType::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::PlaylistType)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXIFramesOnly::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXIFramesOnly)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXMedia::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::VariantStream)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXSessionData::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXSessionData)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXSessionKey::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXSessionKey)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXIndependentSegments::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
TryFrom::try_from(input).map(Self::ExtXIndependentSegments)
|
2019-10-03 14:23:27 +00:00
|
|
|
} else if input.starts_with(tags::ExtXStart::PREFIX) {
|
2020-04-22 07:54:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|