1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::{fmt::Display, path::PathBuf};

use thiserror::Error;

use super::parse_error::ParseError;

/// Error type of Munyo
#[derive(Error, Debug)]
pub enum Error {
	/// Failed to read file
    #[error("failed to read `{0}`, {1}")]
    ReadFile(PathBuf, anyhow::Error),
	/// Parse error
    #[error("`{0}`:{1}")]
    Parse(PathItem, ParseError),
	/// Error occurred in the deserialization
    #[error("`{0}`:{1}")]
    Deserialize(PathItem, ParseError),
	/// Error occurred in the serialization
    #[error("{0}")]
    Serialize(anyhow::Error),
	/// Error occurred in the custom serde::Serialize implementation.
    #[error("{0}")]
    SerializeCustom(anyhow::Error),
	/// Error occurred in the various occasions
    #[error("{0}")]
    Message(anyhow::Error),
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct PathItem {
    pub path: Option<PathBuf>,
}

impl PathItem {
    pub fn new(path: Option<PathBuf>) -> Self {
        Self { path }
    }
}

impl Display for PathItem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.path {
            Some(t) => write!(f, "{}", t.to_string_lossy()),
            None => Ok(()),
        }
    }
}

impl serde::de::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self::Message(anyhow::Error::msg(msg.to_string()))
    }
}

impl serde::ser::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self::SerializeCustom(anyhow::Error::msg(msg.to_string()))
    }
}

impl From<async_channel::TryRecvError> for Error {
    fn from(e: async_channel::TryRecvError) -> Self {
        Self::Message(e.into())
    }
}

impl From<async_channel::RecvError> for Error {
    fn from(e: async_channel::RecvError) -> Self {
        Self::Message(e.into())
    }
}

impl From<std::io::Error> for Error{
    fn from(value: std::io::Error) -> Self {
        Self::Message(value.into())
    }
}