firebase_rs_sdk/performance/
error.rs1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum PerformanceErrorCode {
5 InvalidArgument,
6 Internal,
7}
8
9impl PerformanceErrorCode {
10 pub fn as_str(&self) -> &'static str {
11 match self {
12 PerformanceErrorCode::InvalidArgument => "performance/invalid-argument",
13 PerformanceErrorCode::Internal => "performance/internal",
14 }
15 }
16}
17
18#[derive(Clone, Debug)]
19pub struct PerformanceError {
20 pub code: PerformanceErrorCode,
21 message: String,
22}
23
24impl PerformanceError {
25 pub fn new(code: PerformanceErrorCode, 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 PerformanceError {
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 PerformanceError {}
44
45pub type PerformanceResult<T> = Result<T, PerformanceError>;
46
47pub fn invalid_argument(message: impl Into<String>) -> PerformanceError {
48 PerformanceError::new(PerformanceErrorCode::InvalidArgument, message)
49}
50
51pub fn internal_error(message: impl Into<String>) -> PerformanceError {
52 PerformanceError::new(PerformanceErrorCode::Internal, message)
53}