34 lines
807 B
Rust
34 lines
807 B
Rust
use serde::ser::SerializeStruct;
|
|
use serde::{Serialize, Serializer};
|
|
|
|
pub enum HandlerError {
|
|
BadUidError,
|
|
}
|
|
|
|
impl HandlerError {
|
|
fn to_code(&self) -> u32 {
|
|
match self {
|
|
HandlerError::BadUidError => 400
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Serialize for HandlerError {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
let mut s = serializer.serialize_struct("error", 2)?;
|
|
s.serialize_field("code", &self.to_code())?;
|
|
s.serialize_field("description", &String::from(self))?;
|
|
s.end()
|
|
}
|
|
}
|
|
|
|
impl From<&HandlerError> for String {
|
|
fn from(err: &HandlerError) -> Self {
|
|
match err {
|
|
HandlerError::BadUidError => String::from("the uid is in a bad format"),
|
|
}
|
|
}
|
|
}
|