1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-12 02:09:28 +00:00
hls_m3u8/src/error.rs

158 lines
4.3 KiB
Rust
Raw Normal View History

2019-09-13 14:06:52 +00:00
use std::fmt;
2018-02-11 06:10:52 +00:00
use thiserror::Error;
2020-02-10 11:47:01 +00:00
//use crate::types::ProtocolVersion;
2018-02-11 06:10:52 +00:00
2019-09-13 14:06:52 +00:00
/// This crate specific `Result` type.
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error, Clone, PartialEq)]
2020-02-10 11:47:01 +00:00
#[non_exhaustive]
enum ErrorKind {
2020-02-10 11:47:01 +00:00
#[error("a value is missing for the attribute {}", _0)]
2019-09-13 14:06:52 +00:00
MissingValue(String),
2020-02-10 11:47:01 +00:00
#[error("invalid input")]
2018-02-11 06:10:52 +00:00
InvalidInput,
2019-09-13 14:06:52 +00:00
#[error("{}", _0)]
ParseIntError(::std::num::ParseIntError),
2019-09-13 14:06:52 +00:00
#[error("{}", _0)]
ParseFloatError(::std::num::ParseFloatError),
2019-09-13 14:06:52 +00:00
2020-02-10 11:47:01 +00:00
#[error("expected `{}` at the start of {:?}", tag, input)]
2019-09-14 19:08:35 +00:00
MissingTag {
/// The required tag.
tag: String,
/// The unparsed input data.
input: String,
},
2019-09-13 14:06:52 +00:00
#[error("{}", _0)]
2019-09-13 14:06:52 +00:00
Custom(String),
2020-02-10 11:47:01 +00:00
#[error("unmatched group: {:?}", _0)]
2019-09-13 14:06:52 +00:00
UnmatchedGroup(String),
2020-02-10 11:47:01 +00:00
#[error("unknown protocol version {:?}", _0)]
2019-09-13 14:06:52 +00:00
UnknownProtocolVersion(String),
2020-02-10 11:47:01 +00:00
// #[error("required_version: {:?}, specified_version: {:?}", _0, _1)]
// VersionError(ProtocolVersion, ProtocolVersion),
#[error("missing attribute: {}", _0)]
2019-09-22 16:00:38 +00:00
MissingAttribute(String),
2020-02-10 11:47:01 +00:00
#[error("unexpected attribute: {:?}", _0)]
2019-10-03 14:23:27 +00:00
UnexpectedAttribute(String),
2020-02-10 11:47:01 +00:00
#[error("unexpected tag: {:?}", _0)]
2019-10-04 09:02:21 +00:00
UnexpectedTag(String),
#[error("{}", _0)]
ChronoParseError(chrono::ParseError),
2020-02-10 11:47:01 +00:00
#[error("builder error: {}", _0)]
Builder(String),
2020-02-10 11:47:01 +00:00
#[doc(hidden)]
#[error("{}", _0)]
Hex(hex::FromHexError),
2019-09-13 14:06:52 +00:00
}
2019-09-14 19:08:35 +00:00
/// The Error type of this library.
#[derive(Debug)]
2019-09-13 14:06:52 +00:00
pub struct Error {
inner: ErrorKind,
2019-09-13 14:06:52 +00:00
}
impl std::error::Error for Error {}
2019-09-13 14:06:52 +00:00
impl fmt::Display for Error {
2019-10-03 15:01:15 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.inner.fmt(f) }
2019-09-13 14:06:52 +00:00
}
2020-02-10 11:47:01 +00:00
#[allow(clippy::needless_pass_by_value)]
impl Error {
2020-01-26 12:12:19 +00:00
const fn new(inner: ErrorKind) -> Self { Self { inner } }
2019-09-13 14:06:52 +00:00
pub(crate) fn custom<T: fmt::Display>(value: T) -> Self {
Self::new(ErrorKind::Custom(value.to_string()))
}
2019-09-13 14:06:52 +00:00
pub(crate) fn missing_value<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::MissingValue(value.to_string()))
2019-09-13 14:06:52 +00:00
}
2019-10-03 14:23:27 +00:00
pub(crate) fn unexpected_attribute<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::UnexpectedAttribute(value.to_string()))
2019-10-03 14:23:27 +00:00
}
2019-10-04 09:02:21 +00:00
pub(crate) fn unexpected_tag<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::UnexpectedTag(value.to_string()))
2019-10-04 09:02:21 +00:00
}
2020-02-10 11:47:01 +00:00
pub(crate) const fn invalid_input() -> Self { Self::new(ErrorKind::InvalidInput) }
2019-09-13 14:06:52 +00:00
pub(crate) fn parse_int(value: ::std::num::ParseIntError) -> Self {
Self::new(ErrorKind::ParseIntError(value))
2019-09-13 14:06:52 +00:00
}
pub(crate) fn parse_float(value: ::std::num::ParseFloatError) -> Self {
Self::new(ErrorKind::ParseFloatError(value))
2019-09-13 14:06:52 +00:00
}
pub(crate) fn missing_tag<T, U>(tag: T, input: U) -> Self
where
T: ToString,
U: ToString,
{
Self::new(ErrorKind::MissingTag {
2019-09-13 14:06:52 +00:00
tag: tag.to_string(),
input: input.to_string(),
})
}
pub(crate) fn unmatched_group<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::UnmatchedGroup(value.to_string()))
2019-09-13 14:06:52 +00:00
}
pub(crate) fn unknown_protocol_version<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::UnknownProtocolVersion(value.to_string()))
2019-09-13 14:06:52 +00:00
}
2019-09-14 09:31:16 +00:00
pub(crate) fn builder<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::Builder(value.to_string()))
2019-09-15 10:51:51 +00:00
}
2019-09-22 16:00:38 +00:00
pub(crate) fn missing_attribute<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::MissingAttribute(value.to_string()))
2019-09-22 16:00:38 +00:00
}
2019-09-14 09:31:16 +00:00
// third party crates:
pub(crate) fn chrono(value: chrono::format::ParseError) -> Self {
Self::new(ErrorKind::ChronoParseError(value))
}
2019-09-15 09:25:41 +00:00
pub(crate) fn hex(value: hex::FromHexError) -> Self { Self::new(ErrorKind::Hex(value)) }
2019-09-22 18:33:40 +00:00
pub(crate) fn strum(value: strum::ParseError) -> Self {
Self::new(ErrorKind::Custom(value.to_string()))
2019-09-22 18:33:40 +00:00
}
}
#[doc(hidden)]
impl From<::std::num::ParseIntError> for Error {
fn from(value: ::std::num::ParseIntError) -> Self { Self::parse_int(value) }
}
#[doc(hidden)]
impl From<::std::num::ParseFloatError> for Error {
fn from(value: ::std::num::ParseFloatError) -> Self { Self::parse_float(value) }
}
2019-10-06 14:37:14 +00:00
#[doc(hidden)]
impl From<::strum::ParseError> for Error {
fn from(value: ::strum::ParseError) -> Self { Self::strum(value) }
2019-10-06 14:37:14 +00:00
}