1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-20 01:08:04 +00:00
hls_m3u8/src/error.rs

236 lines
6 KiB
Rust
Raw Permalink Normal View History

2019-09-13 14:06:52 +00:00
use std::fmt;
2018-02-11 06:10:52 +00:00
2020-02-14 12:01:42 +00:00
#[cfg(feature = "backtrace")]
use backtrace::Backtrace;
use thiserror::Error;
2020-02-14 12:01:42 +00:00
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-14 12:01:42 +00:00
#[error("a value is missing for the attribute {value}")]
MissingValue { value: String },
2019-09-13 14:06:52 +00:00
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
2020-02-14 12:01:42 +00:00
#[error("{source}: {input:?}")]
ParseIntError {
input: String,
source: ::std::num::ParseIntError,
},
2019-09-13 14:06:52 +00:00
2020-02-14 12:01:42 +00:00
#[error("{source}: {input:?}")]
ParseFloatError {
input: String,
source: ::std::num::ParseFloatError,
},
2019-09-13 14:06:52 +00:00
2020-02-14 12:01:42 +00:00
#[error("expected `{tag}` at the start of {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
2020-02-14 12:01:42 +00:00
#[error("{0}")]
2019-09-13 14:06:52 +00:00
Custom(String),
2020-02-14 12:01:42 +00:00
#[error("unmatched group: {0:?}")]
2019-09-13 14:06:52 +00:00
UnmatchedGroup(String),
2020-02-14 12:01:42 +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),
2020-02-14 12:01:42 +00:00
#[error("missing attribute: {attribute:?}")]
MissingAttribute { attribute: String },
2019-09-22 16:00:38 +00:00
2020-02-14 12:01:42 +00:00
#[error("unexpected attribute: {attribute:?}")]
UnexpectedAttribute { attribute: String },
2019-10-03 14:23:27 +00:00
2020-02-14 12:01:42 +00:00
#[error("unexpected tag: {tag:?}")]
UnexpectedTag { tag: String },
2019-10-04 09:02:21 +00:00
2020-02-14 12:01:42 +00:00
#[error("{source}")]
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2020-02-14 12:01:42 +00:00
Chrono { source: chrono::ParseError },
2020-02-14 12:01:42 +00:00
#[error("builder error: {message}")]
Builder { message: String },
2020-02-14 12:01:42 +00:00
#[error("{source}")]
Hex { source: 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,
2020-02-14 12:01:42 +00:00
#[cfg(feature = "backtrace")]
backtrace: Backtrace,
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool { self.inner == other.inner }
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 {
2020-04-09 06:43:13 +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-02-14 12:01:42 +00:00
fn new(inner: ErrorKind) -> Self {
Self {
inner,
#[cfg(feature = "backtrace")]
backtrace: Backtrace::new(),
}
}
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 {
2020-02-14 12:01:42 +00:00
Self::new(ErrorKind::MissingValue {
value: value.to_string(),
})
2019-09-13 14:06:52 +00:00
}
2020-03-25 13:11:11 +00:00
pub(crate) fn missing_field<T: fmt::Display, D: fmt::Display>(strct: D, field: T) -> Self {
Self::new(ErrorKind::Custom(format!(
"the field `{}` is missing for `{}`",
field, strct
)))
}
2019-10-03 14:23:27 +00:00
pub(crate) fn unexpected_attribute<T: ToString>(value: T) -> Self {
2020-02-14 12:01:42 +00:00
Self::new(ErrorKind::UnexpectedAttribute {
attribute: 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 {
2020-02-14 12:01:42 +00:00
Self::new(ErrorKind::UnexpectedTag {
tag: value.to_string(),
})
2019-10-04 09:02:21 +00:00
}
2020-02-14 12:01:42 +00:00
pub(crate) fn invalid_input() -> Self { Self::new(ErrorKind::InvalidInput) }
2019-09-13 14:06:52 +00:00
2020-02-14 12:01:42 +00:00
pub(crate) fn parse_int<T: fmt::Display>(input: T, source: ::std::num::ParseIntError) -> Self {
Self::new(ErrorKind::ParseIntError {
input: input.to_string(),
source,
})
2019-09-13 14:06:52 +00:00
}
2020-02-14 12:01:42 +00:00
pub(crate) fn parse_float<T: fmt::Display>(
input: T,
source: ::std::num::ParseFloatError,
) -> Self {
Self::new(ErrorKind::ParseFloatError {
input: input.to_string(),
source,
})
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 {
2020-02-14 12:01:42 +00:00
Self::new(ErrorKind::Builder {
message: 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 {
2020-02-14 12:01:42 +00:00
Self::new(ErrorKind::MissingAttribute {
attribute: value.to_string(),
})
2019-09-22 16:00:38 +00:00
}
2019-09-14 09:31:16 +00:00
2020-08-11 08:36:44 +00:00
pub(crate) fn unexpected_data(value: &str) -> Self {
Self::custom(format!("Unexpected data in the line: {:?}", value))
}
// third party crates:
2020-03-20 11:05:16 +00:00
#[cfg(feature = "chrono")]
2020-02-14 12:01:42 +00:00
pub(crate) fn chrono(source: chrono::format::ParseError) -> Self {
Self::new(ErrorKind::Chrono { source })
}
2019-09-15 09:25:41 +00:00
2020-02-14 12:01:42 +00:00
pub(crate) fn hex(source: hex::FromHexError) -> Self {
//
Self::new(ErrorKind::Hex { source })
}
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)]
2020-02-14 12:01:42 +00:00
impl From<::strum::ParseError> for Error {
fn from(value: ::strum::ParseError) -> Self { Self::strum(value) }
}
2020-02-14 12:01:42 +00:00
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_parse_float_error() {
assert_eq!(
Error::parse_float(
"1.x234",
"1.x234"
.parse::<f32>()
.expect_err("this should not parse as a float!")
)
.to_string(),
"invalid float literal: \"1.x234\"".to_string()
);
}
2019-10-06 14:37:14 +00:00
2020-02-14 12:01:42 +00:00
#[test]
fn test_parse_int_error() {
assert_eq!(
Error::parse_int(
"1x",
"1x".parse::<usize>()
.expect_err("this should not parse as an usize!")
)
.to_string(),
"invalid digit found in string: \"1x\"".to_string()
);
}
2019-10-06 14:37:14 +00:00
}