1use std::fmt;
2use std::path::{Path, PathBuf};
3
4#[derive(Debug)]
6pub struct Error {
7 pub(crate) mesg: String,
8 pub(crate) line: Option<u64>,
9 pub(crate) path: Option<PathBuf>,
10}
11
12impl Error {
13 pub(crate) fn parse(msg: String) -> Error {
15 Error {
16 mesg: msg,
17 line: None,
18 path: None,
19 }
20 }
21
22 pub fn mesg(&self) -> &str {
24 self.mesg.as_str()
25 }
26
27 pub fn line(&self) -> Option<u64> {
29 self.line
30 }
31
32 pub fn path(&self) -> Option<&Path> {
34 self.path.as_deref()
35 }
36}
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 if let Some(ref path) = self.path {
41 if let Some(line) = self.line {
42 write!(f, "{}, {}:{}: ", self.mesg, path.display(), line)
43 } else {
44 write!(f, "{}, {}", self.mesg, path.display())
45 }
46 } else if let Some(line) = self.line {
47 write!(f, "{}, line {}", self.mesg, line)
48 } else {
49 write!(f, "{}", self.mesg)
50 }
51 }
52}
53
54impl From<ucd_parse::Error> for Error {
55 fn from(error: ucd_parse::Error) -> Self {
56 Error {
57 mesg: format!("Parse error: {}", error),
58 line: error.line(),
59 path: None,
60 }
61 }
62}
63
64impl From<&str> for Error {
65 fn from(msg: &str) -> Self {
66 Error::parse(msg.to_string())
67 }
68}
69
70impl From<std::io::Error> for Error {
71 fn from(error: std::io::Error) -> Self {
72 Error {
73 mesg: format!("IO Error: {}", error),
74 line: None,
75 path: None,
76 }
77 }
78}