twars_url2md/
error.rs

1use std::fmt;
2
3/// Custom error type for URL processing
4#[derive(Debug)]
5pub enum Error {
6    /// Error occurred while fetching URL
7    Fetch(String),
8    /// Error occurred while processing HTML
9    Html(String),
10    /// Error occurred while converting to Markdown
11    Markdown(String),
12    /// Error occurred while writing output
13    Output(String),
14    /// Other errors
15    Other(String),
16}
17
18impl std::error::Error for Error {}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Error::Fetch(msg) => write!(f, "Failed to fetch URL: {}", msg),
24            Error::Html(msg) => write!(f, "Failed to process HTML: {}", msg),
25            Error::Markdown(msg) => write!(f, "Failed to convert to Markdown: {}", msg),
26            Error::Output(msg) => write!(f, "Failed to write output: {}", msg),
27            Error::Other(msg) => write!(f, "{}", msg),
28        }
29    }
30}
31
32impl From<reqwest::Error> for Error {
33    fn from(err: reqwest::Error) -> Self {
34        Error::Fetch(err.to_string())
35    }
36}
37
38impl From<std::io::Error> for Error {
39    fn from(err: std::io::Error) -> Self {
40        Error::Output(err.to_string())
41    }
42}
43
44impl From<anyhow::Error> for Error {
45    fn from(err: anyhow::Error) -> Self {
46        Error::Other(err.to_string())
47    }
48}