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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
// Copyright (C) 2016-2023 Élisabeth HENRY.
//
// This file is part of Crowbook.
//
// Crowbook is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Caribon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received ba copy of the GNU Lesser General Public License
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
use std::borrow::Cow;
use std::error;
use std::fmt;
use std::result;
use std::string::FromUtf8Error;
#[derive(Debug, PartialEq, Clone)]
/// Source of an error.
///
/// Contains (if it's possible) the file and ideally the line that the user should
/// look at to correct their error.
pub struct Source {
/// File name of the source
#[doc(hidden)]
pub file: Option<String>,
/// Line number of the source
#[doc(hidden)]
pub line: Option<u32>,
}
impl Source {
/// Create an empty source, with both fields set to None
pub fn empty() -> Source {
Source {
file: None,
line: None,
}
}
/// Create a new source pointing to file
pub fn new<S: Into<String>>(s: S) -> Source {
Source {
file: Some(s.into()),
line: None,
}
}
/// Sets line number of a source.
pub fn set_line(&mut self, line: u32) -> &mut Self {
self.line = Some(line);
self
}
/// Unsets a line number of a source
#[doc(hidden)]
pub fn unset_line(&mut self) -> &mut Self {
self.line = None;
self
}
}
impl fmt::Display for Source {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref file) = self.file {
write!(f, "{file}")?;
if let Some(line) = self.line {
write!(f, ":{line}")?;
}
} else {
write!(f, "<UNKNOWN FILE>")?;
}
Ok(())
}
}
impl<'a> From<&'a Source> for Source {
fn from(s: &'a Source) -> Source {
s.clone()
}
}
#[derive(Debug, PartialEq)]
/// Crowbook Error type.
///
/// This type tries (when it can) to track where the error came from, to
/// pinpoint the file (at least) and, if possible, the line the user needs
/// to look at.
pub struct Error {
/// Origin (file, line) of the error, if there is one
source: Source,
inner: Inner,
}
impl Error {
/// Creates a new default error.
pub fn default<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::Default(msg.into()),
}
}
/// Creates a new grammar check error.
///
/// Used when there is a problem connecting to languagetool
pub fn grammar_check<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::GrammarCheck(msg.into()),
}
}
/// Creates a new parser error.
///
/// Error when parsing markdown file.
pub fn parser<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::Parser(msg.into()),
}
}
/// Creates a new syntect error.
///
/// Error when parsing (and higlighting) syntax code
pub fn syntect<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::Syntect(msg.into()),
}
}
/// Creates a new config parser error.
///
/// Error when parsing the book configuration file.
pub fn config_parser<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::ConfigParser(msg.into()),
}
}
/// Creates a new "file not found" error
///
/// # Arguments
/// * source: the source of the error.
/// * msg: description of why the file was needed.
/// * file: file name that wasn't found.
pub fn file_not_found<
S1: Into<Cow<'static, str>>,
S2: Into<Cow<'static, str>>,
O: Into<Source>,
>(
source: O,
msg: S1,
file: S2,
) -> Error {
Error {
source: source.into(),
inner: Inner::FileNotFound(msg.into(), file.into()),
}
}
/// Creates a new render error.
///
/// Error when rendering the book to a given format.
pub fn render<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::Render(msg.into()),
}
}
/// Creates a new template error.
///
/// Error when compiling a mustache template.
pub fn template<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::Template(msg.into()),
}
}
/// Creates a new invalid option error.
///
/// Error when trying to set an option.
pub fn invalid_option<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::InvalidOption(msg.into()),
}
}
/// Creates a new zipper error.
///
/// Error when moving/copying files to temporary dir, e.g. using `zip` commmand.
pub fn zipper<S: Into<Cow<'static, str>>>(msg: S) -> Error {
Error {
source: Source::empty(),
inner: Inner::Zipper(msg.into()),
}
}
/// Creates a new book option error
///
/// Used when converting an error to invalid type.
pub fn book_option<S: Into<Cow<'static, str>>, O: Into<Source>>(source: O, msg: S) -> Error {
Error {
source: source.into(),
inner: Inner::BookOption(msg.into()),
}
}
/// Change the source of an error.
pub fn with_source<O: Into<Source>>(mut self, source: O) -> Error {
self.source = source.into();
self
}
/// Returns true if self is a default option error, false else.
pub fn is_default(&self) -> bool {
matches!(self.inner, Inner::Default(..))
}
/// Returns true if self is a parser error, false else.
pub fn is_parser(&self) -> bool {
matches!(self.inner, Inner::Parser(..))
}
/// Returns true if self is a config parser error, false else.
pub fn is_config_parser(&self) -> bool {
matches!(self.inner, Inner::ConfigParser(..))
}
/// Returns true if self is a file not found error, false else.
pub fn is_file_not_found(&self) -> bool {
matches!(self.inner, Inner::FileNotFound(..))
}
/// Returns true if self is a render error, false else.
pub fn is_render(&self) -> bool {
matches!(self.inner, Inner::Render(..))
}
/// Returns true if self is a zipper error, false else.
pub fn is_zipper(&self) -> bool {
matches!(self.inner, Inner::Zipper(..))
}
/// Returns true if self is a book option error, false else.
pub fn is_book_option(&self) -> bool {
matches!(self.inner, Inner::BookOption(..))
}
/// Returns true if self is an invalid option error, false else.
pub fn is_invalid_option(&self) -> bool {
matches!(self.inner, Inner::InvalidOption(..))
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match self.inner {
Inner::Default(ref s)
| Inner::Parser(ref s)
| Inner::Zipper(ref s)
| Inner::BookOption(ref s)
| Inner::ConfigParser(ref s)
| Inner::InvalidOption(ref s)
| Inner::Render(ref s)
| Inner::Template(ref s)
| Inner::Syntect(ref s)
| Inner::GrammarCheck(ref s) => s.as_ref(),
Inner::FileNotFound(..) => "File not found",
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let source = &self.source;
if let Some(ref file) = source.file {
write!(f, "{file}")?;
if let Some(line) = source.line {
write!(f, ":{line}")?;
}
write!(f, ": ")?;
}
match self.inner {
Inner::Default(ref s) => write!(f, "{s}"),
Inner::GrammarCheck(ref s) => {
write!(
f,
"{}",
lformat!("Error while trying to check grammar: {error}", error = s)
)
}
Inner::Parser(ref s) => {
write!(
f,
"{}",
lformat!("Error parsing markdown: {error}", error = s)
)
}
Inner::ConfigParser(ref s) => {
f.write_str(&lformat!("Error parsing configuration file: "))?;
f.write_str(s)
}
Inner::FileNotFound(ref description, ref file) => {
write!(
f,
"{}",
lformat!(
"Could not find file '{file}' for {description}",
file = file,
description = description
)
)
}
Inner::Template(ref s) => {
write!(
f,
"{}",
lformat!("Error compiling template: {template}", template = s)
)
}
Inner::Render(ref s) => {
f.write_str(&lformat!("Error during rendering: "))?;
f.write_str(s)
}
Inner::Zipper(ref s) => {
f.write_str(&lformat!("Error during temporary files editing: "))?;
f.write_str(s)
}
Inner::BookOption(ref s) => {
f.write_str(&lformat!("Error converting BookOption: "))?;
f.write_str(s)
}
Inner::InvalidOption(ref s) => {
f.write_str(&lformat!("Error accessing book option: "))?;
f.write_str(s)
}
Inner::Syntect(ref s) => {
f.write_str(&lformat!("Error higligting syntax: "))?;
f.write_str(s)
}
}?;
Ok(())
}
}
/// Crowbook's Result type, used by many methods that can fail
pub type Result<T> = result::Result<T, Error>;
/// Implement our Error from mustache::Error
impl From<mustache::Error> for Error {
fn from(err: mustache::Error) -> Error {
Error::template(Source::empty(), format!("{err}"))
}
}
impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Error {
Error::render(
Source::empty(),
lformat!("UTF-8 error: {error}", error = err),
)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Error {
Error::render(
Source::empty(),
lformat!("UTF-8 error: {error}", error = err),
)
}
}
impl From<fmt::Error> for Error {
fn from(err: fmt::Error) -> Error {
Error::default(
Source::empty(),
lformat!("format error: {error}", error = err),
)
}
}
impl From<syntect::Error> for Error {
fn from(err: syntect::Error) -> Error {
Error::syntect(
Source::empty(),
lformat!("syntect error: {error}", error = err),
)
}
}
#[derive(Debug, PartialEq)]
enum Inner {
/// Default variant
Default(Cow<'static, str>),
/// An error in Parsing markdown file
Parser(Cow<'static, str>),
/// An error in parsing a book configuration file
ConfigParser(Cow<'static, str>),
/// An error when a file is not found
FileNotFound(Cow<'static, str>, Cow<'static, str>), // description, file
/// An error in a renderer
Render(Cow<'static, str>),
/// An error during "zipping" processus
Zipper(Cow<'static, str>),
/// An error relative to BookOption convertion (usually a type error)
BookOption(Cow<'static, str>),
/// An invalid option
InvalidOption(Cow<'static, str>),
/// Error when compiling template
Template(Cow<'static, str>),
/// Error when connecting to LanguageTool
GrammarCheck(Cow<'static, str>),
/// Error when parsing code syntax
Syntect(Cow<'static, str>),
}