sdp_rs/error/
mod.rs

1mod tokenizer_error;
2
3pub use tokenizer_error::TokenizerError;
4
5use std::{error::Error as StdError, fmt};
6
7/// The `Error` enum indicates that something went wrong.
8/// * `ParseError` signifies that tokenizing the input went through, but converting a token to an
9/// actual type failed.
10/// * `TokenizeError` signified that tokenizing the input failed.
11/// * Incomplete is not used, it is meant for a later release where support of streaming input will
12/// be added (for tokio codecs).
13#[derive(Debug, Eq, PartialEq, Clone)]
14pub enum Error {
15    ParseError(String),
16    TokenizeError(String),
17    Incomplete,
18}
19
20impl Error {
21    pub fn tokenizer<S, T>(tuple: (S, T)) -> Self
22    where
23        S: std::fmt::Display,
24        T: std::fmt::Display,
25    {
26        Self::TokenizeError(format!("failed to tokenize {}: {}", tuple.0, tuple.1))
27    }
28
29    pub fn parser<I>(element: &'static str, input: I) -> Self
30    where
31        I: std::fmt::Display,
32    {
33        Self::ParseError(format!("failed to parse {}: {}", element, input))
34    }
35
36    pub fn parser_with_error<I, E>(element: &'static str, input: I, error: E) -> Self
37    where
38        I: std::fmt::Display,
39        E: std::fmt::Display,
40    {
41        Self::ParseError(format!(
42            "failed to parse {} ( {} ): {}",
43            element, error, input
44        ))
45    }
46}
47
48impl From<TokenizerError> for Error {
49    fn from(from: TokenizerError) -> Self {
50        Self::TokenizeError(from.context)
51    }
52}
53
54impl From<nom::Err<TokenizerError>> for Error {
55    fn from(from: nom::Err<TokenizerError>) -> Self {
56        match from {
57            nom::Err::Incomplete(_) => Self::Incomplete,
58            nom::Err::Error(e) => e.into(),
59            nom::Err::Failure(e) => e.into(),
60        }
61    }
62}
63
64impl fmt::Display for Error {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        match self {
67            Self::TokenizeError(inner) => write!(f, "tokenizer error: {}", inner),
68            Self::ParseError(inner) => write!(f, "could not parse part: {}", inner),
69            Self::Incomplete => write!(f, "sdp error: incomplete input"),
70        }
71    }
72}
73
74impl StdError for Error {}
75
76impl From<std::num::ParseIntError> for Error {
77    fn from(error: std::num::ParseIntError) -> Self {
78        Self::ParseError(error.to_string())
79    }
80}
81
82impl From<std::net::AddrParseError> for Error {
83    fn from(error: std::net::AddrParseError) -> Self {
84        Self::ParseError(error.to_string())
85    }
86}