Skip to content

Commit 8ef9166

Browse files
author
tjk
committed
Refactor to support return Result by default everywhere
Add custom Error types and use them in Result types for error handling by default. Approximately akin to the way pulldown-cmark handles errors
1 parent 2330b4c commit 8ef9166

File tree

9 files changed

+435
-556
lines changed

9 files changed

+435
-556
lines changed

lib/src/html/default.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ mod tests {
4848
let config = HtmlConfig::default();
4949
let mut writer = DefaultHtmlWriter::new(FmtWriter(&mut output), &config);
5050

51-
writer.write_str("<p>");
51+
writer.write_str("<p>").unwrap();
5252
let _ = escape_html(&mut writer.get_writer(), "Hello & World");
53-
writer.write_str("</p>");
53+
writer.write_str("</p>").unwrap();
5454

5555
assert_eq!(output, "<p>Hello &amp; World</p>");
5656
}
@@ -67,9 +67,9 @@ mod tests {
6767
);
6868

6969
let mut writer = DefaultHtmlWriter::new(FmtWriter(&mut output), &config);
70-
writer.start_paragraph();
71-
writer.text("Test");
72-
writer.end_paragraph();
70+
writer.start_paragraph().unwrap();
71+
writer.text("Test").unwrap();
72+
writer.end_paragraph().unwrap();
7373

7474
assert_eq!(output, r#"<p class="test">Test</p>"#);
7575
}
@@ -101,7 +101,7 @@ mod tests {
101101
let config = HtmlConfig::default();
102102
let mut writer = DefaultHtmlWriter::new(TestWriter(String::new()), &config);
103103

104-
writer.write_str("Test");
104+
writer.write_str("Test").unwrap();
105105
assert_eq!(writer.get_writer().0, "Test");
106106
}
107107

@@ -114,9 +114,11 @@ mod tests {
114114

115115
assert!(!writer.get_state().currently_in_code_block);
116116
writer.get_state().currently_in_code_block = true;
117-
writer.start_code_block(pulldown_cmark::CodeBlockKind::Fenced("rust".into()));
117+
writer
118+
.start_code_block(pulldown_cmark::CodeBlockKind::Fenced("rust".into()))
119+
.unwrap();
118120
assert!(writer.get_state().currently_in_code_block);
119-
writer.end_code_block();
121+
writer.end_code_block().unwrap();
120122
writer.get_state().currently_in_code_block = false;
121123
assert!(!writer.get_state().currently_in_code_block);
122124
}

lib/src/html/error.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::error::Error;
2+
use std::fmt;
3+
use std::io;
4+
5+
/// Custom error type for HTML rendering operations
6+
#[derive(Debug)]
7+
pub enum HtmlError {
8+
/// IO-related errors
9+
Io(io::Error),
10+
/// Writing-related errors
11+
Write(fmt::Error),
12+
/// Theme-related errors (for syntax highlighting)
13+
Theme(String),
14+
/// Configuration validation errors
15+
Config(String),
16+
/// General rendering errors
17+
Render(String),
18+
}
19+
20+
impl fmt::Display for HtmlError {
21+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22+
match self {
23+
HtmlError::Io(err) => write!(f, "IO error: {}", err),
24+
HtmlError::Write(err) => write!(f, "Write error: {}", err),
25+
HtmlError::Theme(err) => write!(f, "Theme error: {}", err),
26+
HtmlError::Config(err) => write!(f, "Configuration error: {}", err),
27+
HtmlError::Render(err) => write!(f, "Rendering error: {}", err),
28+
}
29+
}
30+
}
31+
32+
impl Error for HtmlError {
33+
fn source(&self) -> Option<&(dyn Error + 'static)> {
34+
match self {
35+
HtmlError::Io(err) => Some(err),
36+
HtmlError::Write(err) => Some(err),
37+
_ => None,
38+
}
39+
}
40+
}
41+
42+
impl From<io::Error> for HtmlError {
43+
fn from(err: io::Error) -> Self {
44+
HtmlError::Io(err)
45+
}
46+
}
47+
48+
impl From<fmt::Error> for HtmlError {
49+
fn from(err: fmt::Error) -> Self {
50+
HtmlError::Write(err)
51+
}
52+
}

0 commit comments

Comments
 (0)