mant_engine/mandoc/
error.rs1use std::{
4 fmt,
5 path::{Path, PathBuf},
6};
7
8use libmandoc_rs::ParseError;
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum ManualErrorKind {
13 Read,
15 Decompression,
17 Limit,
19 UnsafePath,
21 Redirect,
23 Parse,
25}
26
27#[derive(Clone, Debug, Eq, PartialEq)]
29pub enum ManualError {
30 Read {
32 path: PathBuf,
34 message: String,
36 },
37 Decompression {
39 path: PathBuf,
41 message: String,
43 },
44 Limit {
46 path: PathBuf,
48 message: String,
50 },
51 UnsafePath {
53 path: PathBuf,
55 message: String,
57 },
58 Redirect {
60 path: PathBuf,
62 message: String,
64 },
65 Parse(ParseError),
67}
68
69impl ManualError {
70 pub(super) fn read(path: &Path, message: impl Into<String>) -> Self {
71 Self::Read {
72 path: path.to_path_buf(),
73 message: message.into(),
74 }
75 }
76
77 pub(super) fn decompression(path: &Path, message: impl Into<String>) -> Self {
78 Self::Decompression {
79 path: path.to_path_buf(),
80 message: message.into(),
81 }
82 }
83
84 pub(super) fn limit(path: &Path, message: impl Into<String>) -> Self {
85 Self::Limit {
86 path: path.to_path_buf(),
87 message: message.into(),
88 }
89 }
90
91 pub(super) fn unsafe_path(path: &Path, message: impl Into<String>) -> Self {
92 Self::UnsafePath {
93 path: path.to_path_buf(),
94 message: message.into(),
95 }
96 }
97
98 pub(super) fn redirect(path: &Path, message: impl Into<String>) -> Self {
99 Self::Redirect {
100 path: path.to_path_buf(),
101 message: message.into(),
102 }
103 }
104
105 #[must_use]
106 pub const fn kind(&self) -> ManualErrorKind {
108 match self {
109 Self::Read { .. } => ManualErrorKind::Read,
110 Self::Decompression { .. } => ManualErrorKind::Decompression,
111 Self::Limit { .. } => ManualErrorKind::Limit,
112 Self::UnsafePath { .. } => ManualErrorKind::UnsafePath,
113 Self::Redirect { .. } => ManualErrorKind::Redirect,
114 Self::Parse(_) => ManualErrorKind::Parse,
115 }
116 }
117
118 #[must_use]
119 pub fn path(&self) -> &Path {
121 match self {
122 Self::Read { path, .. }
123 | Self::Decompression { path, .. }
124 | Self::Limit { path, .. }
125 | Self::UnsafePath { path, .. }
126 | Self::Redirect { path, .. } => path,
127 Self::Parse(error) => &error.path,
128 }
129 }
130
131 #[must_use]
132 pub fn message(&self) -> &str {
134 match self {
135 Self::Read { message, .. }
136 | Self::Decompression { message, .. }
137 | Self::Limit { message, .. }
138 | Self::UnsafePath { message, .. }
139 | Self::Redirect { message, .. } => message,
140 Self::Parse(error) => &error.message,
141 }
142 }
143}
144
145impl From<ParseError> for ManualError {
146 fn from(error: ParseError) -> Self {
147 Self::Parse(error)
148 }
149}
150
151impl fmt::Display for ManualError {
152 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
153 write!(formatter, "{}: {}", self.path().display(), self.message())
154 }
155}
156
157impl std::error::Error for ManualError {
158 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
159 match self {
160 Self::Parse(error) => Some(error),
161 Self::Read { .. }
162 | Self::Decompression { .. }
163 | Self::Limit { .. }
164 | Self::UnsafePath { .. }
165 | Self::Redirect { .. } => None,
166 }
167 }
168}