diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-01-07 13:11:40 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-01-07 13:11:40 +0200 |
commit | f5acd7cc8f41e2451dc198a34eaf183272517d90 (patch) | |
tree | 2549bec9de80346a90c89376df78ce7e8969ff4b | |
parent | 59d24d84c408884effac41b9f05993320183e6c1 (diff) | |
download | riki-f5acd7cc8f41e2451dc198a34eaf183272517d90.tar.gz |
refactor: simplify error dependencies
Sponsored-by: author
-rw-r--r-- | src/error.rs | 7 | ||||
-rw-r--r-- | src/page.rs | 13 | ||||
-rw-r--r-- | src/parser.rs | 33 |
3 files changed, 37 insertions, 16 deletions
diff --git a/src/error.rs b/src/error.rs index 6da90b1..d38c1e9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -38,10 +38,3 @@ pub enum RikiError { #[error(transparent)] Site(#[from] crate::site::SiteError), } - -impl RikiError { - pub fn wikitext_syntax(line: usize, col: usize, tokens: &[crate::token::TokenKind]) -> Self { - let tokens = tokens.to_vec(); - crate::parser::ParserError::WikitextSyntax(line, col, tokens).into() - } -} diff --git a/src/page.rs b/src/page.rs index db0dc96..2c07023 100644 --- a/src/page.rs +++ b/src/page.rs @@ -2,7 +2,7 @@ use crate::directive::{Processed, Toc}; use crate::html::HtmlError; use crate::html::{parse, Content, Element, ElementTag, HtmlPage}; use crate::name::Name; -use crate::parser::WikitextParser; +use crate::parser::{ParserError, WikitextParser}; use crate::site::Site; use crate::util::get_mtime; use crate::wikitext::{Snippet, WikitextError}; @@ -28,6 +28,9 @@ pub enum PageError { Html(#[from] Box<HtmlError>), #[error(transparent)] + Parser(#[from] Box<ParserError>), + + #[error(transparent)] Riki(#[from] Box<crate::error::RikiError>), } @@ -43,6 +46,12 @@ impl From<HtmlError> for PageError { } } +impl From<ParserError> for PageError { + fn from(e: ParserError) -> Self { + Self::Parser(Box::new(e)) + } +} + pub struct Page { meta: PageMeta, unprocessed: UnprocessedPage, @@ -121,7 +130,7 @@ impl UnprocessedPage { fn snippets(parser: &mut WikitextParser) -> Result<Vec<Snippet>, PageError> { let mut snippets = vec![]; - while let Some(snippet) = parser.parse().map_err(|e| PageError::Riki(Box::new(e)))? { + while let Some(snippet) = parser.parse()? { snippets.push(snippet); } Ok(snippets) diff --git a/src/parser.rs b/src/parser.rs index e13363e..83be505 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,5 @@ -use crate::error::RikiError; use crate::token::{TokenKind, TokenParser, TokenPatterns}; -use crate::wikitext::{ParsedDirective, Snippet, WikiLink}; +use crate::wikitext::{ParsedDirective, Snippet, WikiLink, WikitextError}; use line_col::LineColLookup; use log::{debug, trace}; use std::collections::HashMap; @@ -9,6 +8,22 @@ use std::collections::HashMap; pub enum ParserError { #[error("failed to parse wikitext, line {0}, column {1}: {2:?}")] WikitextSyntax(usize, usize, Vec<crate::token::TokenKind>), + + #[error(transparent)] + Wikitext(#[from] Box<WikitextError>), +} + +impl ParserError { + pub fn wikitext_syntax(line: usize, col: usize, tokens: &[crate::token::TokenKind]) -> Self { + let tokens = tokens.to_vec(); + crate::parser::ParserError::WikitextSyntax(line, col, tokens) + } +} + +impl From<WikitextError> for ParserError { + fn from(e: WikitextError) -> Self { + Self::Wikitext(Box::new(e)) + } } #[derive(Debug)] @@ -50,7 +65,7 @@ impl WikitextParser { self.tokens.is_empty() } - pub fn parse(&mut self) -> Result<Option<Snippet>, RikiError> { + pub fn parse(&mut self) -> Result<Option<Snippet>, ParserError> { if self.is_empty() { return Ok(None); } @@ -116,7 +131,9 @@ impl WikitextParser { self.drain(3); break; } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => { + return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])) + } } } if target.is_none() { @@ -169,7 +186,9 @@ impl WikitextParser { args.insert(value.to_string(), "".to_string()); self.drain(1); } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => { + return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])) + } } } Snippet::Directive(ParsedDirective::new(&name, args)?) @@ -205,7 +224,7 @@ impl WikitextParser { break; } _ => { - return Err(RikiError::wikitext_syntax( + return Err(ParserError::wikitext_syntax( line, col, &self.tokens[..std::cmp::min(5, self.tokens.len())], @@ -285,7 +304,7 @@ impl WikitextParser { self.drain(1); Snippet::Markdown("]]".into()) } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])), }; Ok(Some(snippet)) } |