1use crate::prelude::*;
2use core::error::Error as StdError;
3use core::fmt;
4
5#[derive(Debug, Eq, PartialEq, Clone)]
6pub struct TokenizerError {
7 pub context: String,
8}
9
10impl TokenizerError {
11 pub fn new(msg: impl Into<String>) -> Self {
12 Self {
13 context: msg.into(),
14 }
15 }
16}
17
18impl fmt::Display for TokenizerError {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 write!(f, "tokenizer error: {}", self.context)
21 }
22}
23
24impl StdError for TokenizerError {}
25
26impl<S: Into<String>, T: fmt::Display> From<(S, T)> for TokenizerError {
27 fn from(tuple: (S, T)) -> Self {
28 Self {
29 context: format!("failed to tokenize {}: {}", tuple.0.into(), tuple.1),
30 }
31 }
32}
33
34#[derive(Debug, Eq, PartialEq, Clone)]
35pub enum Error {
36 MissingHeader(String),
37 InvalidParam(String),
38 ParseError(String),
39 TokenizeError(String),
40 Utf8Error(String),
41 Unexpected(String),
42}
43
44impl Error {
45 pub fn missing_header(header: &'static str) -> Self {
46 Self::MissingHeader(header.into())
47 }
48
49 pub fn tokenizer<S: Into<String>, T: fmt::Display>(tuple: (S, T)) -> Self {
50 Self::TokenizeError(format!(
51 "failed to tokenize {}: {}",
52 tuple.0.into(),
53 tuple.1
54 ))
55 }
56}
57
58impl fmt::Display for Error {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 match self {
61 Self::MissingHeader(inner) => write!(f, "rsip error: missing header: {}", inner),
62 Self::InvalidParam(inner) => write!(f, "rsip error: invalid header param: {}", inner),
63 Self::ParseError(inner) => write!(f, "rsip error: could not parse part: {}", inner),
64 Self::TokenizeError(inner) => write!(f, "Tokenizer error: {}", inner),
65 Self::Unexpected(inner) => write!(f, "rsip quite unexpected error: {}", inner),
66 Self::Utf8Error(inner) => write!(f, "rsip error: invalid utf8 ({})", inner),
67 }
68 }
69}
70
71impl StdError for Error {}
72
73impl From<TokenizerError> for Error {
74 fn from(e: TokenizerError) -> Self {
75 Self::TokenizeError(e.context)
76 }
77}
78
79impl From<core::str::Utf8Error> for Error {
80 fn from(e: core::str::Utf8Error) -> Self {
81 Self::Utf8Error(e.to_string())
82 }
83}
84
85impl From<core::num::ParseIntError> for Error {
86 fn from(e: core::num::ParseIntError) -> Self {
87 Self::ParseError(e.to_string())
88 }
89}
90
91impl From<core::net::AddrParseError> for Error {
92 fn from(e: core::net::AddrParseError) -> Self {
93 Self::ParseError(e.to_string())
94 }
95}