1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/tags/media_segment/program_date_time.rs

248 lines
7 KiB
Rust
Raw Normal View History

#[cfg(not(feature = "chrono"))]
use std::borrow::Cow;
use std::convert::TryFrom;
2019-09-06 10:55:00 +00:00
use std::fmt;
use std::marker::PhantomData;
2019-09-06 10:55:00 +00:00
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2019-10-06 14:39:18 +00:00
use chrono::{DateTime, FixedOffset, SecondsFormat};
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2020-02-02 14:23:47 +00:00
use derive_more::{Deref, DerefMut};
2019-09-15 10:51:51 +00:00
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-13 14:06:52 +00:00
2020-03-25 10:56:43 +00:00
/// Associates the first sample of a [`MediaSegment`] with an absolute date
/// and/or time.
2020-02-02 12:38:11 +00:00
///
2020-03-25 10:56:43 +00:00
/// ## Features
///
/// By enabling the `chrono` feature the `date_time`-field will change from
/// `String` to `DateTime<FixedOffset>` and the traits
/// - `Deref<Target=DateTime<FixedOffset>>`,
/// - `DerefMut<Target=DateTime<FixedOffset>>`
/// - and `Copy`
///
/// will be derived.
2019-09-06 10:55:00 +00:00
///
2020-02-21 19:44:09 +00:00
/// [`MediaSegment`]: crate::MediaSegment
2020-03-20 11:05:16 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "chrono", derive(Deref, DerefMut, Copy))]
2020-03-25 10:56:43 +00:00
#[non_exhaustive]
pub struct ExtXProgramDateTime<'a> {
2020-03-20 11:05:16 +00:00
/// The date-time of the first sample of the associated media segment.
#[cfg(feature = "chrono")]
#[cfg_attr(feature = "chrono", deref_mut, deref)]
pub date_time: DateTime<FixedOffset>,
/// The date-time of the first sample of the associated media segment.
#[cfg(not(feature = "chrono"))]
pub date_time: Cow<'a, str>,
_p: PhantomData<&'a str>,
2020-03-20 11:05:16 +00:00
}
2019-09-06 10:55:00 +00:00
impl<'a> ExtXProgramDateTime<'a> {
2019-09-06 10:55:00 +00:00
pub(crate) const PREFIX: &'static str = "#EXT-X-PROGRAM-DATE-TIME:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXProgramDateTime`] tag.
2019-09-22 16:00:38 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-22 16:00:38 +00:00
/// ```
2019-10-06 14:39:18 +00:00
/// # use hls_m3u8::tags::ExtXProgramDateTime;
2019-09-22 16:00:38 +00:00
/// use chrono::{FixedOffset, TimeZone};
///
/// const HOURS_IN_SECS: i32 = 3600; // 1 hour = 3600 seconds
///
/// let program_date_time = ExtXProgramDateTime::new(
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 2, 19)
2019-10-03 15:01:15 +00:00
/// .and_hms_milli(14, 54, 23, 31),
2019-09-22 16:00:38 +00:00
/// );
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
pub const fn new(date_time: DateTime<FixedOffset>) -> Self {
Self {
date_time,
_p: PhantomData,
}
}
2019-09-06 10:55:00 +00:00
2020-03-20 11:05:16 +00:00
/// Makes a new [`ExtXProgramDateTime`] tag.
2019-10-06 14:39:18 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-06 14:39:18 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXProgramDateTime;
2020-03-20 11:05:16 +00:00
/// let program_date_time = ExtXProgramDateTime::new("2010-02-19T14:54:23.031+08:00");
2019-10-06 14:39:18 +00:00
/// ```
2020-03-20 11:05:16 +00:00
#[cfg(not(feature = "chrono"))]
pub fn new<T: Into<Cow<'a, str>>>(date_time: T) -> Self {
2020-03-20 11:05:16 +00:00
Self {
date_time: date_time.into(),
_p: PhantomData,
}
}
/// Makes the struct independent of its lifetime, by taking ownership of all
/// internal [`Cow`]s.
///
/// # Note
///
/// This is a relatively expensive operation.
#[must_use]
pub fn into_owned(self) -> ExtXProgramDateTime<'static> {
ExtXProgramDateTime {
#[cfg(not(feature = "chrono"))]
date_time: Cow::Owned(self.date_time.into_owned()),
#[cfg(feature = "chrono")]
date_time: self.date_time,
_p: PhantomData,
2020-03-20 11:05:16 +00:00
}
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-10-06 14:39:18 +00:00
/// This tag requires [`ProtocolVersion::V1`].
impl<'a> RequiredVersion for ExtXProgramDateTime<'a> {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl<'a> fmt::Display for ExtXProgramDateTime<'a> {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020-03-20 11:05:16 +00:00
let date_time = {
#[cfg(feature = "chrono")]
{
self.date_time.to_rfc3339_opts(SecondsFormat::Millis, true)
}
#[cfg(not(feature = "chrono"))]
{
&self.date_time
}
};
2019-09-15 10:51:51 +00:00
write!(f, "{}{}", Self::PREFIX, date_time)
2019-09-06 10:55:00 +00:00
}
}
impl<'a> TryFrom<&'a str> for ExtXProgramDateTime<'a> {
type Error = Error;
2019-09-08 10:23:33 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
2019-09-13 14:06:52 +00:00
let input = tag(input, Self::PREFIX)?;
2020-03-25 10:56:43 +00:00
Ok(Self::new({
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
{
DateTime::parse_from_rfc3339(input).map_err(Error::chrono)?
}
#[cfg(not(feature = "chrono"))]
{
input
}
2020-03-25 10:56:43 +00:00
}))
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2019-10-06 14:39:18 +00:00
use chrono::{Datelike, TimeZone};
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2020-02-02 14:23:47 +00:00
use core::ops::DerefMut;
use pretty_assertions::assert_eq;
2019-09-22 08:57:28 +00:00
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2019-09-22 08:57:28 +00:00
const HOURS_IN_SECS: i32 = 3600; // 1 hour = 3600 seconds
2019-09-06 10:55:00 +00:00
#[test]
2019-09-15 10:51:51 +00:00
fn test_display() {
assert_eq!(
2020-03-20 11:05:16 +00:00
ExtXProgramDateTime::new({
#[cfg(feature = "chrono")]
{
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31)
}
#[cfg(not(feature = "chrono"))]
{
"2010-02-19T14:54:23.031+08:00"
}
})
2019-09-22 16:00:38 +00:00
.to_string(),
2019-09-15 10:51:51 +00:00
"#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00".to_string()
);
}
#[test]
fn test_parser() {
2019-09-22 08:57:28 +00:00
assert_eq!(
2020-03-20 11:05:16 +00:00
ExtXProgramDateTime::new({
#[cfg(feature = "chrono")]
{
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31)
}
#[cfg(not(feature = "chrono"))]
{
"2010-02-19T14:54:23.031+08:00"
}
}),
ExtXProgramDateTime::try_from("#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00")
2019-09-22 08:57:28 +00:00
.unwrap()
);
2019-09-15 10:51:51 +00:00
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
2019-09-22 16:00:38 +00:00
assert_eq!(
2020-03-20 11:05:16 +00:00
ExtXProgramDateTime::new({
#[cfg(feature = "chrono")]
{
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31)
}
#[cfg(not(feature = "chrono"))]
{
"2010-02-19T14:54:23.031+08:00"
}
})
2019-09-22 16:00:38 +00:00
.required_version(),
ProtocolVersion::V1
2019-09-22 08:57:28 +00:00
);
2019-09-06 10:55:00 +00:00
}
2019-10-06 14:39:18 +00:00
#[test]
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2019-10-06 14:39:18 +00:00
fn test_deref() {
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
)
.year(),
2010
);
}
#[test]
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2019-10-06 14:39:18 +00:00
fn test_deref_mut() {
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
)
.deref_mut(),
&mut FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
);
}
2019-09-06 10:55:00 +00:00
}