Skip to content

Commit

Permalink
Replace unwrap with actual error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kelko committed Sep 29, 2022
1 parent 94440a2 commit cc531cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ use tl::{HTMLTag, NodeHandle, Parser, VDom};
#[cfg(test)]
mod tests;

#[derive(Debug, Snafu)]
pub enum StreamingEditorError {
#[snafu(display("Nothing Imported from tl"))]
NothingImported { backtrace: Backtrace },
#[snafu(display("Node not resolved by Parser"))]
InvalidParserState { backtrace: Backtrace },
}

const HTML_VOID_ELEMENTS: [&str; 16] = [
"area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link",
"meta", "param", "source", "track", "wbr",
Expand Down Expand Up @@ -110,7 +118,7 @@ impl HtmlContent {
}
}

pub(crate) fn import(dom: VDom) -> Result<Node<HtmlContent>, ()> {
pub(crate) fn import(dom: VDom) -> Result<Node<HtmlContent>, StreamingEditorError> {
let parser = dom.parser();
let mut converted = dom
.children()
Expand All @@ -133,11 +141,14 @@ impl HtmlContent {
if let Some(root_result) = converted.pop() {
root_result
} else {
Err(())
NothingImportedSnafu {}.fail()
}
}

fn convert_tag(tag: &HTMLTag, parser: &Parser) -> Result<Node<HtmlContent>, ()> {
fn convert_tag(
tag: &HTMLTag,
parser: &Parser,
) -> Result<Node<HtmlContent>, StreamingEditorError> {
let name = String::from(tag.name().as_utf8_str());
let mut attributes = BTreeMap::new();

Expand All @@ -161,7 +172,10 @@ impl HtmlContent {
Ok(converted)
}

fn convert_node(node_handle: &NodeHandle, parser: &Parser) -> Result<Node<HtmlContent>, ()> {
fn convert_node(
node_handle: &NodeHandle,
parser: &Parser,
) -> Result<Node<HtmlContent>, StreamingEditorError> {
if let Some(node) = node_handle.get(parser) {
return match node {
tl::Node::Tag(tag) => Self::convert_tag(tag, parser),
Expand All @@ -170,14 +184,16 @@ impl HtmlContent {
};
}

Err(())
InvalidParserStateSnafu {}.fail()
}

fn convert_text(text: impl Into<String>) -> Result<Node<HtmlContent>, ()> {
fn convert_text(text: impl Into<String>) -> Result<Node<HtmlContent>, StreamingEditorError> {
Ok(Node::new(HtmlContent::Text(text.into())))
}

fn convert_comment(comment: impl Into<String>) -> Result<Node<HtmlContent>, ()> {
fn convert_comment(
comment: impl Into<String>,
) -> Result<Node<HtmlContent>, StreamingEditorError> {
let comment = comment.into();
let comment = comment.trim_start_matches("<!--");
let comment = comment.trim_end_matches("-->");
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub enum StreamingEditorError {
#[snafu(backtrace)]
source: crate::pipeline::PipelineError,
},
#[snafu(display("Failed to convert parsed HTML into memory model"))]
LoadingParsedHtmlFailed {
#[snafu(backtrace)]
source: crate::html::StreamingEditorError,
},
}

pub struct HtmlStreamingEditor<'a> {
Expand All @@ -74,7 +79,7 @@ impl<'a> HtmlStreamingEditor<'a> {

let dom = tl::parse(&string_content, tl::ParserOptions::default())
.context(ParsingInputFailedSnafu)?;
let root_element = HtmlContent::import(dom).unwrap();
let root_element = HtmlContent::import(dom).context(LoadingParsedHtmlFailedSnafu)?;
let result = pipeline
.run_on(vec![rctree::Node::clone(&root_element)])
.context(RunningPipelineFailedSnafu)?;
Expand Down

0 comments on commit cc531cb

Please sign in to comment.