html_generator/
error.rs

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
//! Error types for HTML generation and processing.
//!
//! This module defines custom error types used throughout the HTML generation library.
//! It provides a centralized location for all error definitions, making it easier to
//! manage and handle errors consistently across the codebase.

use std::io::Error as IoError;
use thiserror::Error;

/// Enum to represent various errors that can occur during HTML generation, processing, or optimization.
#[derive(Error, Debug)]
pub enum HtmlError {
    /// Error that occurs when a regular expression fails to compile.
    ///
    /// This variant contains the underlying error from the `regex` crate.
    #[error("Failed to compile regex: {0}")]
    RegexCompilationError(#[from] regex::Error),

    /// Error indicating failure in extracting front matter from the input content.
    ///
    /// This variant is used when there is an issue parsing the front matter of a document.
    /// The associated string provides details about the error.
    #[error("Failed to extract front matter: {0}")]
    FrontMatterExtractionError(String),

    /// Error indicating a failure in formatting an HTML header.
    ///
    /// This variant is used when the header cannot be formatted correctly. The associated string provides more details.
    #[error("Failed to format header: {0}")]
    HeaderFormattingError(String),

    /// Error for IO-related issues.
    ///
    /// This variant wraps standard IO errors and is used when an IO operation fails (e.g., reading or writing files).
    #[error("IO error: {0}")]
    IoError(#[from] IoError),

    /// Error that occurs when parsing a selector fails.
    ///
    /// This variant is used when a CSS or HTML selector cannot be parsed.
    /// The first string is the selector, and the second string provides additional context.
    #[error("Failed to parse selector '{0}': {1}")]
    SelectorParseError(String, String),

    /// Error indicating failure to minify HTML content.
    ///
    /// This variant is used when there is an issue during the HTML minification process. The associated string provides details.
    #[error("Failed to minify HTML: {0}")]
    MinificationError(String),

    /// Error that occurs during the conversion of Markdown to HTML.
    ///
    /// This variant is used when the Markdown conversion process encounters an issue. The associated string provides more information.
    #[error("Markdown conversion error: {0}")]
    MarkdownConversionError(String),

    /// Error that occurs during SEO optimization.
    ///
    /// This variant is used when an SEO-related process fails, such as generating meta tags or structured data.
    /// The associated string provides more context.
    #[error("SEO optimization error: {0}")]
    SeoOptimizationError(String),

    /// Error that occurs when handling accessibility-related operations.
    ///
    /// This variant is used for errors that occur during accessibility checks or modifications (e.g., adding ARIA attributes).
    /// The associated string provides more details.
    #[error("Accessibility error: {0}")]
    AccessibilityError(String),

    /// Error indicating that a required HTML element is missing.
    ///
    /// This variant is used when a necessary HTML element (like a title tag) is not found.
    #[error("Missing required HTML element: {0}")]
    MissingHtmlElement(String),

    /// Error that occurs when structured data is invalid.
    ///
    /// This variant is used when JSON-LD or other structured data does not meet the expected format or requirements.
    #[error("Invalid structured data: {0}")]
    InvalidStructuredData(String),

    /// Error indicating an invalid input.
    ///
    /// This variant is used when the input content is invalid or does not meet the expected criteria.
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Error indicating an invalid front matter format.
    ///
    /// This variant is used when the front matter of a document does not follow the expected format.
    #[error("Invalid front matter format: {0}")]
    InvalidFrontMatterFormat(String),

    /// Error indicating an input that is too large.
    ///
    /// This variant is used when the input content exceeds a certain size limit.
    #[error("Input too large: size {0} bytes")]
    InputTooLarge(usize),

    /// Error indicating an invalid header format.
    ///
    /// This variant is used when an HTML header does not conform to the expected format.
    #[error("Invalid header format: {0}")]
    InvalidHeaderFormat(String),

    /// Error that occurs when converting from UTF-8 fails.
    ///
    /// This variant wraps errors that occur when converting a byte sequence to a UTF-8 string.
    #[error("UTF-8 conversion error: {0}")]
    Utf8ConversionError(#[from] std::string::FromUtf8Error),

    /// Error indicating a failure during parsing.
    ///
    /// This variant is used for general parsing errors where the specific source of the issue isn't covered by other variants.
    #[error("Parsing error: {0}")]
    ParsingError(String),

    /// Error indicating a validation failure.
    ///
    /// This variant is used when a validation step fails, such as schema validation or data integrity checks.
    #[error("Validation error: {0}")]
    ValidationError(String),

    /// A catch-all error for unexpected failures.
    ///
    /// This variant is used for errors that do not fit into other categories.
    #[error("Unexpected error: {0}")]
    UnexpectedError(String),

    /// An SEO-related error.
    #[error("SEO error: {0}")]
    SeoError(String),
}

/// Type alias for a result using the `HtmlError` error type.
///
/// This type alias makes it more convenient to work with Results throughout the library,
/// reducing boilerplate and improving readability.
pub type Result<T> = std::result::Result<T, HtmlError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_html_error_display() {
        let error = HtmlError::FrontMatterExtractionError(
            "Invalid format".to_string(),
        );
        assert_eq!(
            error.to_string(),
            "Failed to extract front matter: Invalid format"
        );
    }

    #[test]
    fn test_html_error_from_io_error() {
        let io_error = IoError::new(
            std::io::ErrorKind::NotFound,
            "File not found",
        );
        let html_error: HtmlError = io_error.into();

        // Use IoErrorKind to verify the correct error kind
        if let HtmlError::IoError(e) = html_error {
            assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
        } else {
            panic!("Expected HtmlError::IoError");
        }
    }

    #[test]
    fn test_html_error_from_utf8_error() {
        let utf8_error =
            String::from_utf8(vec![0, 159, 146, 150]).unwrap_err();
        let html_error: HtmlError = utf8_error.into();
        assert!(matches!(
            html_error,
            HtmlError::Utf8ConversionError(_)
        ));
    }
}