firebase_rs_sdk/installations/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum InstallationsErrorCode {
5    InvalidArgument,
6    Internal,
7    RequestFailed,
8}
9
10impl InstallationsErrorCode {
11    pub fn as_str(&self) -> &'static str {
12        match self {
13            InstallationsErrorCode::InvalidArgument => "installations/invalid-argument",
14            InstallationsErrorCode::Internal => "installations/internal",
15            InstallationsErrorCode::RequestFailed => "installations/request-failed",
16        }
17    }
18}
19
20#[derive(Clone, Debug)]
21pub struct InstallationsError {
22    pub code: InstallationsErrorCode,
23    message: String,
24}
25
26impl InstallationsError {
27    pub fn new(code: InstallationsErrorCode, message: impl Into<String>) -> Self {
28        Self {
29            code,
30            message: message.into(),
31        }
32    }
33
34    pub fn code_str(&self) -> &'static str {
35        self.code.as_str()
36    }
37}
38
39impl Display for InstallationsError {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        write!(f, "{} ({})", self.message, self.code_str())
42    }
43}
44
45impl std::error::Error for InstallationsError {}
46
47pub type InstallationsResult<T> = Result<T, InstallationsError>;
48
49pub fn invalid_argument(message: impl Into<String>) -> InstallationsError {
50    InstallationsError::new(InstallationsErrorCode::InvalidArgument, message)
51}
52
53pub fn internal_error(message: impl Into<String>) -> InstallationsError {
54    InstallationsError::new(InstallationsErrorCode::Internal, message)
55}
56
57pub fn request_failed(message: impl Into<String>) -> InstallationsError {
58    InstallationsError::new(InstallationsErrorCode::RequestFailed, message)
59}