firebase_rs_sdk/data_connect/
error.rs

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