diff --git a/Cargo.toml b/Cargo.toml index 4e2f85c..9a6c983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ codecov = {repository = "sile/hls_m3u8"} [dependencies] failure = "0.1.5" derive_builder = "0.7.2" -url = "2.1.0" chrono = "0.4.9" [dev-dependencies] diff --git a/src/error.rs b/src/error.rs index 7acb888..c2db7c2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,9 +12,7 @@ pub enum ErrorKind { #[fail(display = "ChronoParseError: {}", _0)] /// An error from the [Chrono](chrono) crate. ChronoParseError(String), - #[fail(display = "UrlParseError: {}", _0)] - /// An error from the [Url](url) crate. - UrlParseError(String), + #[fail(display = "UnknownError: {}", _0)] /// An unknown error occured. UnknownError(String), @@ -184,10 +182,6 @@ impl Error { Self::from(ErrorKind::BuilderError(value.to_string())) } - pub(crate) fn url(value: T) -> Self { - Self::from(ErrorKind::UrlParseError(value.to_string())) - } - pub(crate) fn chrono(value: T) -> Self { Self::from(ErrorKind::ChronoParseError(value.to_string())) } @@ -211,12 +205,6 @@ impl From<::std::io::Error> for Error { } } -impl From<::url::ParseError> for Error { - fn from(value: ::url::ParseError) -> Self { - Error::url(value) - } -} - impl From<::chrono::ParseError> for Error { fn from(value: ::chrono::ParseError) -> Self { Error::chrono(value) diff --git a/src/line.rs b/src/line.rs index 28b3a32..6d8ca95 100644 --- a/src/line.rs +++ b/src/line.rs @@ -2,8 +2,6 @@ use std::fmt; use std::ops::{Deref, DerefMut}; use std::str::FromStr; -use url::Url; - use crate::tags; use crate::Error; @@ -55,7 +53,7 @@ impl FromStr for Lines { continue; } } else { - Line::Uri(line.trim().parse()?) + Line::Uri(line.trim().to_string()) } } }; @@ -93,7 +91,7 @@ impl DerefMut for Lines { #[derive(Debug, PartialEq, Eq)] pub enum Line { Tag(Tag), - Uri(Url), + Uri(String), } #[allow(clippy::large_enum_variant)] diff --git a/src/media_segment.rs b/src/media_segment.rs index d637131..8460547 100644 --- a/src/media_segment.rs +++ b/src/media_segment.rs @@ -2,7 +2,6 @@ use std::fmt; use std::iter; use derive_builder::Builder; -use url::Url; use crate::tags::{ ExtInf, ExtXByteRange, ExtXDateRange, ExtXDiscontinuity, ExtXKey, ExtXMap, ExtXProgramDateTime, @@ -34,7 +33,7 @@ pub struct MediaSegment { /// Sets an [ExtInf] tag. inf_tag: ExtInf, /// Sets an Uri. - uri: Url, + uri: String, } impl MediaSegmentBuilder { @@ -81,7 +80,7 @@ impl MediaSegment { MediaSegmentBuilder::default() } /// Returns the URI of the media segment. - pub const fn uri(&self) -> &Url { + pub const fn uri(&self) -> &String { &self.uri } diff --git a/src/tags/master_playlist/session_key.rs b/src/tags/master_playlist/session_key.rs index 0d7d275..e76fb64 100644 --- a/src/tags/master_playlist/session_key.rs +++ b/src/tags/master_playlist/session_key.rs @@ -2,8 +2,6 @@ use std::fmt; use std::ops::{Deref, DerefMut}; use std::str::FromStr; -use url::Url; - use crate::types::{DecryptionKey, EncryptionMethod, ProtocolVersion}; use crate::utils::tag; use crate::Error; @@ -20,7 +18,7 @@ impl ExtXSessionKey { /// Makes a new [ExtXSessionKey] tag. /// # Panic /// This method will panic, if the [EncryptionMethod] is None. - pub fn new(method: EncryptionMethod, uri: Url) -> Self { + pub fn new(method: EncryptionMethod, uri: T) -> Self { if method == EncryptionMethod::None { panic!("The EncryptionMethod is not allowed to be None"); } @@ -36,7 +34,7 @@ impl ExtXSessionKey { /// /// let mut key = ExtXSessionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( @@ -93,7 +91,7 @@ mod test { fn test_display() { let mut key = ExtXSessionKey::new( EncryptionMethod::Aes128, - "https://www.example.com/hls-key/key.bin".parse().unwrap(), + "https://www.example.com/hls-key/key.bin", ); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, @@ -116,13 +114,13 @@ mod test { .unwrap(), ExtXSessionKey::new( EncryptionMethod::Aes128, - "https://priv.example.com/key.php?r=52".parse().unwrap() + "https://priv.example.com/key.php?r=52" ) ); let mut key = ExtXSessionKey::new( EncryptionMethod::Aes128, - "https://www.example.com/hls-key/key.bin".parse().unwrap(), + "https://www.example.com/hls-key/key.bin", ); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, diff --git a/src/tags/master_playlist/stream_inf.rs b/src/tags/master_playlist/stream_inf.rs index 808d4fc..74c0325 100644 --- a/src/tags/master_playlist/stream_inf.rs +++ b/src/tags/master_playlist/stream_inf.rs @@ -1,8 +1,6 @@ use std::fmt; use std::str::FromStr; -use url::Url; - use crate::attribute::AttributePairs; use crate::types::{ ClosedCaptions, DecimalFloatingPoint, DecimalResolution, HdcpLevel, ProtocolVersion, @@ -15,7 +13,7 @@ use crate::Error; /// [4.3.4.2. EXT-X-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.2 #[derive(Debug, Clone, PartialEq, Eq)] pub struct ExtXStreamInf { - uri: Url, + uri: String, bandwidth: u64, average_bandwidth: Option, codecs: Option, @@ -32,9 +30,9 @@ impl ExtXStreamInf { pub(crate) const PREFIX: &'static str = "#EXT-X-STREAM-INF:"; /// Makes a new `ExtXStreamInf` tag. - pub const fn new(uri: Url, bandwidth: u64) -> Self { + pub fn new(uri: T, bandwidth: u64) -> Self { ExtXStreamInf { - uri, + uri: uri.to_string(), bandwidth, average_bandwidth: None, codecs: None, @@ -49,7 +47,7 @@ impl ExtXStreamInf { } /// Returns the URI that identifies the associated media playlist. - pub const fn uri(&self) -> &Url { + pub const fn uri(&self) -> &String { &self.uri } @@ -193,7 +191,7 @@ impl FromStr for ExtXStreamInf { let bandwidth = bandwidth.ok_or(Error::missing_value("EXT-X-BANDWIDTH"))?; Ok(ExtXStreamInf { - uri: uri.parse()?, + uri: uri.to_string(), bandwidth, average_bandwidth, codecs, @@ -220,7 +218,7 @@ mod test { assert_eq!( stream_inf, - ExtXStreamInf::new("http://www.example.com".parse().unwrap(), 1000) + ExtXStreamInf::new("http://www.example.com", 1000) ); } @@ -228,14 +226,14 @@ mod test { fn test_requires_version() { assert_eq!( ProtocolVersion::V1, - ExtXStreamInf::new("http://www.example.com".parse().unwrap(), 1000).requires_version() + ExtXStreamInf::new("http://www.example.com", 1000).requires_version() ); } #[test] fn test_display() { assert_eq!( - ExtXStreamInf::new("http://www.example.com".parse().unwrap(), 1000).to_string(), + ExtXStreamInf::new("http://www.example.com/", 1000).to_string(), "#EXT-X-STREAM-INF:BANDWIDTH=1000\nhttp://www.example.com/".to_string() ); } diff --git a/src/tags/media_segment/key.rs b/src/tags/media_segment/key.rs index 64abf6d..9effe62 100644 --- a/src/tags/media_segment/key.rs +++ b/src/tags/media_segment/key.rs @@ -2,8 +2,6 @@ use std::fmt; use std::ops::{Deref, DerefMut}; use std::str::FromStr; -use url::Url; - use crate::types::{DecryptionKey, EncryptionMethod}; use crate::utils::tag; use crate::Error; @@ -22,14 +20,12 @@ impl ExtXKey { /// Makes a new `ExtXKey` tag. /// # Example /// ``` - /// use url::Url; - /// /// use hls_m3u8::tags::ExtXKey; /// use hls_m3u8::types::EncryptionMethod; /// /// let key = ExtXKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( @@ -37,7 +33,7 @@ impl ExtXKey { /// "#EXT-X-KEY:METHOD=AES-128,URI=\"https://www.example.com/\"" /// ); /// ``` - pub const fn new(method: EncryptionMethod, uri: Url) -> Self { + pub fn new(method: EncryptionMethod, uri: T) -> Self { Self(DecryptionKey::new(method, uri)) } @@ -128,7 +124,7 @@ mod test { key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, ]); - key.set_uri("https://www.example.com".parse().unwrap()); + key.set_uri(Some("https://www.example.com")); key.set_key_format_versions("1/2/3"); assert_eq!(key.to_string(), "#EXT-X-KEY:METHOD=NONE".to_string()); @@ -142,13 +138,13 @@ mod test { .unwrap(), ExtXKey::new( EncryptionMethod::Aes128, - "https://priv.example.com/key.php?r=52".parse().unwrap() + "https://priv.example.com/key.php?r=52" ) ); let mut key = ExtXKey::new( EncryptionMethod::Aes128, - "https://www.example.com/hls-key/key.bin".parse().unwrap(), + "https://www.example.com/hls-key/key.bin", ); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, diff --git a/src/types/decryption_key.rs b/src/types/decryption_key.rs index 0b425b9..8e2ec86 100644 --- a/src/types/decryption_key.rs +++ b/src/types/decryption_key.rs @@ -2,7 +2,6 @@ use std::fmt; use std::str::FromStr; use derive_builder::Builder; -use url::Url; use crate::attribute::AttributePairs; use crate::types::{EncryptionMethod, InitializationVector, ProtocolVersion}; @@ -14,7 +13,7 @@ use crate::Error; pub struct DecryptionKey { pub(crate) method: EncryptionMethod, #[builder(setter(into, strip_option), default)] - pub(crate) uri: Option, + pub(crate) uri: Option, #[builder(setter(into, strip_option), default)] pub(crate) iv: Option, #[builder(setter(into, strip_option), default)] @@ -27,13 +26,11 @@ impl DecryptionKey { /// Makes a new `DecryptionKey`. /// # Example /// ``` - /// use url::Url; - /// /// use hls_m3u8::types::{EncryptionMethod, DecryptionKey}; /// /// let key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( @@ -41,10 +38,10 @@ impl DecryptionKey { /// "METHOD=AES-128,URI=\"https://www.example.com/\"" /// ); /// ``` - pub const fn new(method: EncryptionMethod, uri: Url) -> Self { + pub fn new(method: EncryptionMethod, uri: T) -> Self { Self { method, - uri: Some(uri), + uri: Some(uri.to_string()), iv: None, key_format: None, key_format_versions: None, @@ -58,7 +55,7 @@ impl DecryptionKey { /// /// let key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( @@ -82,7 +79,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_method(EncryptionMethod::SampleAes); @@ -105,39 +102,41 @@ impl DecryptionKey { /// /// let key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( /// key.uri(), - /// &Some("https://www.example.com".parse().unwrap()) + /// &Some("https://www.example.com/".to_string()) /// ); /// ``` - pub const fn uri(&self) -> &Option { + pub const fn uri(&self) -> &Option { &self.uri } /// Sets the `URI` attribute. /// - /// This attribute is required, if the [EncryptionMethod] is not None. + /// # Note + /// This attribute is required, if the [EncryptionMethod] is not `None`. + /// /// # Example /// ``` /// use hls_m3u8::types::{DecryptionKey, EncryptionMethod}; /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// - /// key.set_uri("http://www.google.com".parse().unwrap()); + /// key.set_uri(Some("http://www.google.com/")); /// /// assert_eq!( /// key.to_string(), /// "METHOD=AES-128,URI=\"http://www.google.com/\"".to_string() /// ); /// ``` - pub fn set_uri(&mut self, value: Url) { - self.uri = Some(value); + pub fn set_uri(&mut self, value: Option) { + self.uri = value.map(|v| v.to_string()); } /// Returns the IV (Initialization Vector) attribute. @@ -149,7 +148,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_iv([ @@ -178,7 +177,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_iv([ @@ -207,7 +206,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_key_format("key_format_attribute"); @@ -230,7 +229,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_key_format("key_format_attribute"); @@ -257,7 +256,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_key_format_versions("1/2/3/4/5"); @@ -280,7 +279,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// key.set_key_format_versions("1/2/3/4/5"); @@ -301,7 +300,7 @@ impl DecryptionKey { /// /// let mut key = DecryptionKey::new( /// EncryptionMethod::Aes128, - /// "https://www.example.com".parse().unwrap() + /// "https://www.example.com/" /// ); /// /// assert_eq!( @@ -333,7 +332,7 @@ impl FromStr for DecryptionKey { for (key, value) in input.parse::()? { match key.as_str() { "METHOD" => method = Some((value.parse())?), - "URI" => uri = Some(unquote(value).parse()?), + "URI" => uri = Some(unquote(value)), "IV" => iv = Some((value.parse())?), "KEYFORMAT" => key_format = Some(unquote(value)), "KEYFORMATVERSIONS" => key_format_versions = Some(unquote(value)), @@ -391,7 +390,7 @@ mod test { fn test_builder() { let key = DecryptionKey::builder() .method(EncryptionMethod::Aes128) - .uri("https://www.example.com".parse::().unwrap()) + .uri("https://www.example.com/") .iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, ]) @@ -409,7 +408,7 @@ mod test { fn test_display() { let mut key = DecryptionKey::new( EncryptionMethod::Aes128, - "https://www.example.com/hls-key/key.bin".parse().unwrap(), + "https://www.example.com/hls-key/key.bin", ); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, @@ -432,13 +431,13 @@ mod test { .unwrap(), DecryptionKey::new( EncryptionMethod::Aes128, - "https://priv.example.com/key.php?r=52".parse().unwrap() + "https://priv.example.com/key.php?r=52" ) ); let mut key = DecryptionKey::new( EncryptionMethod::Aes128, - "https://www.example.com/hls-key/key.bin".parse().unwrap(), + "https://www.example.com/hls-key/key.bin", ); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, @@ -453,10 +452,7 @@ mod test { key ); - let mut key = DecryptionKey::new( - EncryptionMethod::Aes128, - "http://www.example.com".parse().unwrap(), - ); + let mut key = DecryptionKey::new(EncryptionMethod::Aes128, "http://www.example.com"); key.set_iv([ 16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82, ]); diff --git a/tests/playlist.rs b/tests/playlist.rs index b61068a..25ccabd 100644 --- a/tests/playlist.rs +++ b/tests/playlist.rs @@ -36,11 +36,11 @@ fn test_simple_playlist() { assert_eq!( media_playlist.segments()[0].uri(), - &"http://media.example.com/entire1.ts".parse().unwrap() + &"http://media.example.com/entire1.ts".to_string() ); assert_eq!( media_playlist.segments()[1].uri(), - &"http://media.example.com/entire2.ts".parse().unwrap() + &"http://media.example.com/entire2.ts".to_string() ); }