munyo/error/
munyo_error.rs

1use std::{fmt::Display, path::PathBuf};
2
3use thiserror::Error;
4
5use super::{parse_error::ParseError, parse_error2::ParseError2};
6
7/// Error type of Munyo
8#[derive(Error, Debug)]
9pub enum Error {
10	/// Failed to read file
11    #[error("failed to read `{0}`, {1}")]
12    ReadFile(PathBuf, anyhow::Error),
13	/// Parse error
14    #[error("`{0}`:{1}")]
15    Parse(PathItem, ParseError),
16	/// Error occurred in the deserialization
17    #[error("`{0}`:{1}")]
18    Deserialize(PathItem, ParseError2),
19	/// Error occurred in the serialization
20    #[error("{0}")]
21    Serialize(anyhow::Error),
22	/// Error occurred in the custom serde::Serialize implementation.
23    #[error("{0}")]
24    SerializeCustom(anyhow::Error),
25	/// Error occurred in the various occasions
26    #[error("{0}")]
27    Message(anyhow::Error),
28}
29
30#[derive(Debug, Clone, Default, PartialEq)]
31pub struct PathItem {
32    pub path: Option<PathBuf>,
33}
34
35impl PathItem {
36    pub fn new(path: Option<PathBuf>) -> Self {
37        Self { path }
38    }
39}
40
41impl Display for PathItem {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match &self.path {
44            Some(t) => write!(f, "{}", t.to_string_lossy()),
45            None => Ok(()),
46        }
47    }
48}
49
50impl serde::de::Error for Error {
51    fn custom<T>(msg: T) -> Self
52    where
53        T: Display,
54    {
55        Self::Message(anyhow::Error::msg(msg.to_string()))
56    }
57}
58
59impl serde::ser::Error for Error {
60    fn custom<T>(msg: T) -> Self
61    where
62        T: Display,
63    {
64        Self::SerializeCustom(anyhow::Error::msg(msg.to_string()))
65    }
66}
67
68impl From<async_channel::TryRecvError> for Error {
69    fn from(e: async_channel::TryRecvError) -> Self {
70        Self::Message(e.into())
71    }
72}
73
74impl From<async_channel::RecvError> for Error {
75    fn from(e: async_channel::RecvError) -> Self {
76        Self::Message(e.into())
77    }
78}
79
80impl From<std::io::Error> for Error{
81    fn from(value: std::io::Error) -> Self {
82        Self::Message(value.into())
83    }
84}
85
86impl From<&str> for Error{
87    fn from(value: &str) -> Self {
88        Self::Message(anyhow::Error::msg(value.to_string()))
89    }
90}