word_dictionary/
errors.rs

1use std::{
2    error::Error,
3    fmt::{self, Display, Formatter},
4    io,
5};
6
7#[derive(Debug)]
8pub enum BrokenReason {
9    BadLeftString,
10    NoRightString,
11    BadRightString { right_string: String },
12    Duplicated { another_left_string: String },
13}
14
15#[derive(Debug)]
16pub enum ReadError {
17    IOError(io::Error),
18    Broken { line: usize, left_string: String, reason: BrokenReason },
19}
20
21impl From<io::Error> for ReadError {
22    #[inline]
23    fn from(error: io::Error) -> Self {
24        ReadError::IOError(error)
25    }
26}
27
28impl Display for ReadError {
29    #[inline]
30    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
31        match self {
32            ReadError::IOError(err) => Display::fmt(&err, f),
33            ReadError::Broken {
34                line,
35                left_string,
36                reason,
37            } => {
38                f.write_fmt(format_args!("broken at line {}, ", line))?;
39
40                match reason {
41                    BrokenReason::BadLeftString => f.write_fmt(format_args!(
42                        "the left string {:?} is not correct",
43                        left_string
44                    )),
45                    BrokenReason::NoRightString => f.write_fmt(format_args!(
46                        "expected a \"=\" after the left string {:?} to concatenate a right string",
47                        left_string
48                    )),
49                    BrokenReason::BadRightString {
50                        right_string,
51                    } => f.write_fmt(format_args!(
52                        "the right string {:?} is not correct",
53                        right_string
54                    )),
55                    BrokenReason::Duplicated {
56                        another_left_string,
57                    } => {
58                        if left_string == another_left_string {
59                            f.write_fmt(format_args!(
60                                "the left string {:#?} is duplicated",
61                                left_string
62                            ))
63                        } else {
64                            f.write_fmt(format_args!(
65                                "the left string {:#?} and {:#?} are duplicated",
66                                left_string, another_left_string
67                            ))
68                        }
69                    },
70                }
71            },
72        }
73    }
74}
75
76impl Error for ReadError {}
77
78#[derive(Debug)]
79pub enum WriteError {
80    IOError(io::Error),
81    BadLeftString,
82    BadRightString,
83    Duplicated,
84    Same,
85}
86
87impl From<io::Error> for WriteError {
88    #[inline]
89    fn from(error: io::Error) -> Self {
90        WriteError::IOError(error)
91    }
92}
93
94impl Display for WriteError {
95    #[inline]
96    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
97        match self {
98            WriteError::IOError(err) => Display::fmt(&err, f),
99            WriteError::BadLeftString => f.write_str("the left word is not correct"),
100            WriteError::BadRightString => f.write_str("the right word is not correct"),
101            WriteError::Duplicated => {
102                f.write_str("the pair of the left word and the right word is duplicated")
103            },
104            WriteError::Same => f.write_str("the left word is equal to the right word"),
105        }
106    }
107}
108
109impl Error for WriteError {}