twiml_rust/
error.rs

1//! Error types for TwiML
2
3use std::fmt;
4
5/// Result type alias for TwiML operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for TwiML operations
9#[derive(Debug)]
10pub enum Error {
11    /// TwiML validation failed
12    Validation(String),
13
14    /// Invalid parameter
15    InvalidParameter {
16        /// Parameter name
17        param: String,
18        /// Reason for invalidity
19        reason: String,
20    },
21
22    /// Generic error
23    Other(String),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::Validation(msg) => write!(f, "TwiML validation failed: {}", msg),
30            Error::InvalidParameter { param, reason } => {
31                write!(f, "Invalid parameter '{}': {}", param, reason)
32            }
33            Error::Other(msg) => write!(f, "{}", msg),
34        }
35    }
36}
37
38impl std::error::Error for Error {}
39
40impl Error {
41    /// Create a new validation error
42    pub fn validation(message: impl Into<String>) -> Self {
43        Self::Validation(message.into())
44    }
45
46    /// Create a new invalid parameter error
47    pub fn invalid_parameter(param: impl Into<String>, reason: impl Into<String>) -> Self {
48        Self::InvalidParameter {
49            param: param.into(),
50            reason: reason.into(),
51        }
52    }
53
54    /// Create a new generic error
55    pub fn other(message: impl Into<String>) -> Self {
56        Self::Other(message.into())
57    }
58}