mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-11-25 08:31:00 +00:00
improvements to error
This commit is contained in:
parent
9cc162ece7
commit
101878a083
1 changed files with 16 additions and 45 deletions
61
src/error.rs
61
src/error.rs
|
@ -1,33 +1,27 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
//use crate::types::ProtocolVersion;
|
||||||
use crate::types::ProtocolVersion;
|
|
||||||
|
|
||||||
/// This crate specific `Result` type.
|
/// This crate specific `Result` type.
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
/// The [`ErrorKind`].
|
|
||||||
#[derive(Debug, Error, Clone, PartialEq)]
|
#[derive(Debug, Error, Clone, PartialEq)]
|
||||||
|
#[non_exhaustive]
|
||||||
enum ErrorKind {
|
enum ErrorKind {
|
||||||
/// A required value is missing.
|
#[error("a value is missing for the attribute {}", _0)]
|
||||||
#[error("A value is missing for the attribute {}", _0)]
|
|
||||||
MissingValue(String),
|
MissingValue(String),
|
||||||
|
|
||||||
/// Error for anything.
|
#[error("invalid input")]
|
||||||
#[error("Invalid Input")]
|
|
||||||
InvalidInput,
|
InvalidInput,
|
||||||
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
/// Failed to parse a String to int.
|
|
||||||
ParseIntError(::std::num::ParseIntError),
|
ParseIntError(::std::num::ParseIntError),
|
||||||
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
/// Failed to parse a String to float.
|
|
||||||
ParseFloatError(::std::num::ParseFloatError),
|
ParseFloatError(::std::num::ParseFloatError),
|
||||||
|
|
||||||
/// A tag is missing, that is required at the start of the input.
|
#[error("expected `{}` at the start of {:?}", tag, input)]
|
||||||
#[error("Expected `{}` at the start of {:?}", tag, input)]
|
|
||||||
MissingTag {
|
MissingTag {
|
||||||
/// The required tag.
|
/// The required tag.
|
||||||
tag: String,
|
tag: String,
|
||||||
|
@ -36,56 +30,34 @@ enum ErrorKind {
|
||||||
},
|
},
|
||||||
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
/// A custom error.
|
|
||||||
Custom(String),
|
Custom(String),
|
||||||
|
|
||||||
/// Unmatched Group
|
#[error("unmatched group: {:?}", _0)]
|
||||||
#[error("Unmatched Group: {:?}", _0)]
|
|
||||||
UnmatchedGroup(String),
|
UnmatchedGroup(String),
|
||||||
|
|
||||||
/// Unknown m3u8 version. This library supports up to ProtocolVersion 7.
|
#[error("unknown protocol version {:?}", _0)]
|
||||||
#[error("Unknown protocol version {:?}", _0)]
|
|
||||||
UnknownProtocolVersion(String),
|
UnknownProtocolVersion(String),
|
||||||
|
|
||||||
/// Some io error
|
// #[error("required_version: {:?}, specified_version: {:?}", _0, _1)]
|
||||||
#[error("{}", _0)]
|
// VersionError(ProtocolVersion, ProtocolVersion),
|
||||||
Io(String),
|
#[error("missing attribute: {}", _0)]
|
||||||
|
|
||||||
/// This error occurs, if there is a ProtocolVersion mismatch.
|
|
||||||
#[error("required_version: {:?}, specified_version: {:?}", _0, _1)]
|
|
||||||
VersionError(ProtocolVersion, ProtocolVersion),
|
|
||||||
|
|
||||||
/// An attribute is missing.
|
|
||||||
#[error("Missing Attribute: {}", _0)]
|
|
||||||
MissingAttribute(String),
|
MissingAttribute(String),
|
||||||
|
|
||||||
/// An unexpected value.
|
#[error("unexpected attribute: {:?}", _0)]
|
||||||
#[error("Unexpected Attribute: {:?}", _0)]
|
|
||||||
UnexpectedAttribute(String),
|
UnexpectedAttribute(String),
|
||||||
|
|
||||||
/// An unexpected tag.
|
#[error("unexpected tag: {:?}", _0)]
|
||||||
#[error("Unexpected Tag: {:?}", _0)]
|
|
||||||
UnexpectedTag(String),
|
UnexpectedTag(String),
|
||||||
|
|
||||||
/// An error from the [`chrono`] crate.
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
ChronoParseError(chrono::ParseError),
|
ChronoParseError(chrono::ParseError),
|
||||||
|
|
||||||
/// An error from a Builder.
|
#[error("builder error: {}", _0)]
|
||||||
#[error("BuilderError: {}", _0)]
|
|
||||||
Builder(String),
|
Builder(String),
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
Hex(hex::FromHexError),
|
Hex(hex::FromHexError),
|
||||||
|
|
||||||
/// Hints that destructuring should not be exhaustive.
|
|
||||||
///
|
|
||||||
/// This enum may grow additional variants, so this makes sure clients
|
|
||||||
/// don't count on exhaustive matching. (Otherwise, adding a new variant
|
|
||||||
/// could break existing code.)
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[error("Invalid error")]
|
|
||||||
__Nonexhaustive,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The Error type of this library.
|
/// The Error type of this library.
|
||||||
|
@ -100,6 +72,7 @@ impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.inner.fmt(f) }
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.inner.fmt(f) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::needless_pass_by_value)]
|
||||||
impl Error {
|
impl Error {
|
||||||
const fn new(inner: ErrorKind) -> Self { Self { inner } }
|
const fn new(inner: ErrorKind) -> Self { Self { inner } }
|
||||||
|
|
||||||
|
@ -119,7 +92,7 @@ impl Error {
|
||||||
Self::new(ErrorKind::UnexpectedTag(value.to_string()))
|
Self::new(ErrorKind::UnexpectedTag(value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn invalid_input() -> Self { Self::new(ErrorKind::InvalidInput) }
|
pub(crate) const fn invalid_input() -> Self { Self::new(ErrorKind::InvalidInput) }
|
||||||
|
|
||||||
pub(crate) fn parse_int(value: ::std::num::ParseIntError) -> Self {
|
pub(crate) fn parse_int(value: ::std::num::ParseIntError) -> Self {
|
||||||
Self::new(ErrorKind::ParseIntError(value))
|
Self::new(ErrorKind::ParseIntError(value))
|
||||||
|
@ -148,8 +121,6 @@ impl Error {
|
||||||
Self::new(ErrorKind::UnknownProtocolVersion(value.to_string()))
|
Self::new(ErrorKind::UnknownProtocolVersion(value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn io<T: ToString>(value: T) -> Self { Self::new(ErrorKind::Io(value.to_string())) }
|
|
||||||
|
|
||||||
pub(crate) fn builder<T: ToString>(value: T) -> Self {
|
pub(crate) fn builder<T: ToString>(value: T) -> Self {
|
||||||
Self::new(ErrorKind::Builder(value.to_string()))
|
Self::new(ErrorKind::Builder(value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue