1use rustapi_core::ApiError;
4use std::fmt;
5
6#[derive(Debug)]
8pub enum ToonError {
9 Encode(String),
11 Decode(String),
13 InvalidContentType,
15 EmptyBody,
17}
18
19impl fmt::Display for ToonError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 Self::Encode(msg) => write!(f, "TOON encoding error: {}", msg),
23 Self::Decode(msg) => write!(f, "TOON decoding error: {}", msg),
24 Self::InvalidContentType => write!(
25 f,
26 "Invalid content type: expected application/toon or text/toon"
27 ),
28 Self::EmptyBody => write!(f, "Empty request body"),
29 }
30 }
31}
32
33impl std::error::Error for ToonError {}
34
35impl From<toon_format::ToonError> for ToonError {
36 fn from(err: toon_format::ToonError) -> Self {
37 match &err {
38 toon_format::ToonError::SerializationError(_) => ToonError::Encode(err.to_string()),
39 _ => ToonError::Decode(err.to_string()),
40 }
41 }
42}
43
44impl From<ToonError> for ApiError {
45 fn from(err: ToonError) -> Self {
46 match err {
47 ToonError::Encode(msg) => ApiError::internal(format!("Failed to encode TOON: {}", msg)),
48 ToonError::Decode(msg) => ApiError::bad_request(format!("Invalid TOON: {}", msg)),
49 ToonError::InvalidContentType => ApiError::bad_request(
50 "Invalid content type: expected application/toon or text/toon",
51 ),
52 ToonError::EmptyBody => ApiError::bad_request("Empty request body"),
53 }
54 }
55}