Skip to content

Commit

Permalink
docs: error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
stoically committed Sep 10, 2020
1 parent 2041904 commit d59816e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ assert_eq!(nodes[0].children[0].value_as_string().unwrap(), "hi");
<div key={ let block = "in attribute value position"; } />
```

- **Helpful error reporting out of the box**

```rust,no-run
error: open tag has no corresponding close tag and is not self-closing
--> examples/html-to-string-macro/tests/lib.rs:5:24
|
5 | html_to_string! { <div> };
| ^^^
```

- **Customization**

A `ParserConfig` to customize parsing behavior is available, so if you have
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
//! # }).unwrap();
//! ```
//!
//! - **Helpful error reporting out of the box**
//!
//! ```ignore
//! error: open tag has no corresponding close tag and is not self-closing
//! --> examples/html-to-string-macro/tests/lib.rs:5:24
//! |
//! 5 | html_to_string! { <div> };
//! | ^^^
//! ```
//!
//! - **Customization**
//!
//! A [`ParserConfig`] to customize parsing behavior is available, so if you have
Expand Down
18 changes: 9 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ impl Parser {
if let Ok(_) = self.tag_close(&input.fork()) {
return Err(fork.error("close tag has no corresponding open tag"));
}
let (name, attributes, selfclosing) = self.tag_open(fork)?;
let (name, attributes, self_closing) = self.tag_open(fork)?;

let mut children = vec![];
if !selfclosing {
if !self_closing {
loop {
if !self.has_children(&name, fork)? {
break;
Expand All @@ -174,7 +174,7 @@ impl Parser {
if input.is_empty() {
return Err(Error::new(
tag_open_name.span(),
"open tag has no corresponding close tag",
"open tag has no corresponding close tag and is not self-closing",
));
}

Expand All @@ -196,9 +196,9 @@ impl Parser {
let name = self.node_name(input)?;

let mut attributes = TokenStream::new();
let selfclosing = loop {
if let Ok(selfclosing) = self.tag_open_end(input) {
break selfclosing;
let self_closing = loop {
if let Ok(self_closing) = self.tag_open_end(input) {
break self_closing;
}

if input.is_empty() {
Expand All @@ -212,14 +212,14 @@ impl Parser {
let parser = move |input: ParseStream| self.attributes(input);
let attributes = parser.parse2(attributes)?;

Ok((name, attributes, selfclosing))
Ok((name, attributes, self_closing))
}

fn tag_open_end(&self, input: ParseStream) -> Result<bool> {
let selfclosing = input.parse::<Option<Token![/]>>()?.is_some();
let self_closing = input.parse::<Option<Token![/]>>()?.is_some();
input.parse::<Token![>]>()?;

Ok(selfclosing)
Ok(self_closing)
}

fn tag_close(&self, input: ParseStream) -> Result<NodeName> {
Expand Down

0 comments on commit d59816e

Please sign in to comment.