Skip to main content

melodium_share/
error.rs

1use melodium_common::descriptor::{Identifier, Status};
2use melodium_engine::LogicError;
3use std::error::Error;
4use std::fmt::{Display, Formatter};
5
6#[derive(Debug, Clone)]
7pub struct SharingError {
8    pub id: u32,
9    pub kind: SharingErrorKind,
10}
11
12#[derive(Debug, Clone)]
13pub enum SharingErrorKind {
14    CompiledModel { identifier: Identifier },
15    CompiledTreatment { identifier: Identifier },
16    NoModelDesignAvailable { identifier: Identifier },
17    NoTreatmentDesignAvailable { identifier: Identifier },
18    InvalidIdentifier { wrong_identifier: crate::Identifier },
19    MissingBaseIdentifier { model_identifier: Identifier },
20    DataSerializationFailure {},
21    Logic { error: LogicError },
22}
23
24impl Display for SharingErrorKind {
25    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
26        match self {
27            SharingErrorKind::CompiledModel { identifier } => write!(
28                f,
29                "Model '{identifier}' is compiled and cannot be restitued"
30            ),
31            SharingErrorKind::CompiledTreatment { identifier } => write!(
32                f,
33                "Treatment '{identifier}' is compiled and cannot be restitued"
34            ),
35            SharingErrorKind::NoModelDesignAvailable { identifier } => {
36                write!(f, "No design available for model '{identifier}'")
37            }
38            SharingErrorKind::NoTreatmentDesignAvailable { identifier } => {
39                write!(f, "No design available for treatment '{identifier}'")
40            }
41            SharingErrorKind::InvalidIdentifier { wrong_identifier } => {
42                write!(f, "Identifier '{wrong_identifier}' is invalid")
43            }
44            SharingErrorKind::MissingBaseIdentifier { model_identifier } => {
45                write!(f, "Base identifier is missing for '{model_identifier}'")
46            }
47            SharingErrorKind::DataSerializationFailure {} => {
48                write!(f, "Serialized data not managed")
49            }
50            SharingErrorKind::Logic { error } => write!(f, "{}", error),
51        }
52    }
53}
54
55impl SharingError {
56    pub fn compiled_model(id: u32, identifier: Identifier) -> Self {
57        Self {
58            id,
59            kind: SharingErrorKind::CompiledModel { identifier },
60        }
61    }
62
63    pub fn compiled_treatment(id: u32, identifier: Identifier) -> Self {
64        Self {
65            id,
66            kind: SharingErrorKind::CompiledTreatment { identifier },
67        }
68    }
69
70    pub fn no_model_design_available(id: u32, identifier: Identifier) -> Self {
71        Self {
72            id,
73            kind: SharingErrorKind::NoModelDesignAvailable { identifier },
74        }
75    }
76
77    pub fn no_treatment_design_available(id: u32, identifier: Identifier) -> Self {
78        Self {
79            id,
80            kind: SharingErrorKind::NoTreatmentDesignAvailable { identifier },
81        }
82    }
83
84    pub fn invalid_identifier(id: u32, wrong_identifier: crate::Identifier) -> Self {
85        Self {
86            id,
87            kind: SharingErrorKind::InvalidIdentifier { wrong_identifier },
88        }
89    }
90
91    pub fn missing_base_identifier(id: u32, model_identifier: Identifier) -> Self {
92        Self {
93            id,
94            kind: SharingErrorKind::MissingBaseIdentifier { model_identifier },
95        }
96    }
97
98    pub fn data_serialization_error(id: u32) -> Self {
99        Self {
100            id,
101            kind: SharingErrorKind::DataSerializationFailure {},
102        }
103    }
104
105    pub fn logic(id: u32, error: LogicError) -> Self {
106        Self {
107            id,
108            kind: SharingErrorKind::Logic { error },
109        }
110    }
111}
112
113impl Display for SharingError {
114    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
115        write!(f, "R{:04}: {}", self.id, self.kind)
116    }
117}
118
119impl From<LogicError> for SharingError {
120    fn from(le: LogicError) -> Self {
121        SharingError::logic(0, le)
122    }
123}
124
125impl Error for SharingError {
126    fn source(&self) -> Option<&(dyn Error + 'static)> {
127        // Generic error, underlying cause isn't tracked.
128        None
129    }
130}
131
132pub type SharingErrors = Vec<SharingError>;
133pub type SharingResult<T> = Status<T, SharingError, SharingError>;
134
135impl From<SharingError> for SharingErrors {
136    fn from(value: SharingError) -> Self {
137        vec![value]
138    }
139}