diff options
Diffstat (limited to 'parser')
| -rw-r--r-- | parser/src/conversion.rs | 6 | ||||
| -rw-r--r-- | parser/src/conversion/block.rs | 11 | ||||
| -rw-r--r-- | parser/src/conversion/tests.rs | 6 | ||||
| -rw-r--r-- | parser/src/lib.rs | 2 | ||||
| -rw-r--r-- | parser/src/pair_ext_parse.rs | 8 | ||||
| -rw-r--r-- | parser/src/simplify.rs | 13 |
6 files changed, 23 insertions, 23 deletions
diff --git a/parser/src/conversion.rs b/parser/src/conversion.rs index de5f091..5e855ef 100644 --- a/parser/src/conversion.rs +++ b/parser/src/conversion.rs @@ -40,7 +40,7 @@ fn get_level<'tl>(toplevel: &'tl mut Vec<c::StructuralSubElement>, section_idxs: pub fn convert_document(pairs: Pairs<Rule>) -> Result<e::Document, Error> { use self::block::TitleOrSsubel::*; - + let mut toplevel: Vec<c::StructuralSubElement> = vec![]; // The kinds of section titles encountered. // `section_idx[x]` has the kind `kinds[x]`, but `kinds` can be longer @@ -49,7 +49,7 @@ pub fn convert_document(pairs: Pairs<Rule>) -> Result<e::Document, Error> { // `None`s indicate skipped section levels: // toplevel[section_idxs.flatten()[0]].children[section_idxs.flatten()[1]]... let mut section_idxs: Vec<Option<usize>> = vec![]; - + for pair in pairs { if let Some(ssubel) = block::convert_ssubel(pair)? { match ssubel { Title(title, kind) => { @@ -83,7 +83,7 @@ pub fn convert_document(pairs: Pairs<Rule>) -> Result<e::Document, Error> { pub fn whitespace_normalize_name(name: &str) -> String { // Python's string.split() defines whitespace differently than Rust does. let split_iter = name.split( - |ch: char| ch.is_whitespace() || (ch >= '\x1C' && ch <= '\x1F') + |ch: char| ch.is_whitespace() || ('\x1C'..='\x1F').contains(&ch) ).filter(|split| !split.is_empty()); let mut ret = String::new(); for split in split_iter { diff --git a/parser/src/conversion/block.rs b/parser/src/conversion/block.rs index 626bc20..a68dd17 100644 --- a/parser/src/conversion/block.rs +++ b/parser/src/conversion/block.rs @@ -37,8 +37,9 @@ pub(super) fn convert_ssubel(pair: Pair<Rule>) -> Result<Option<TitleOrSsubel>, fn convert_substructure(pair: Pair<Rule>) -> Result<c::SubStructure, Error> { + #[allow(clippy::match_single_binding)] Ok(match pair.as_rule() { - // todo: Topic, Sidebar, Transition + // TODO: Topic, Sidebar, Transition // no section here, as it’s constructed from titles _ => convert_body_elem(pair)?.into(), }) @@ -50,7 +51,7 @@ fn convert_body_elem(pair: Pair<Rule>) -> Result<c::BodyElement, Error> { Rule::paragraph => convert_paragraph(pair)?.into(), Rule::target => convert_target(pair)?.into(), Rule::substitution_def => convert_substitution_def(pair)?.into(), - Rule::admonition_gen => convert_admonition_gen(pair)?.into(), + Rule::admonition_gen => convert_admonition_gen(pair)?, Rule::image => convert_image::<e::Image>(pair)?.into(), Rule::bullet_list => convert_bullet_list(pair)?.into(), Rule::literal_block => convert_literal_block(pair).into(), @@ -84,7 +85,7 @@ fn convert_title(pair: Pair<Rule>) -> Result<(e::Title, TitleKind), Error> { let mut elem = e::Title::with_children(title_inlines.expect("No text in title")); if let Some(title) = title { //TODO: slugify properly - let slug = title.to_lowercase().replace("\n", "").replace(" ", "-"); + let slug = title.to_lowercase().replace('\n', "").replace(' ', "-"); elem.names_mut().push(at::NameToken(slug)); } let title_kind = match kind { @@ -163,7 +164,7 @@ fn convert_image<I>(pair: Pair<Rule>) -> Result<I, Error> where I: Element + Ext } fn parse_scale(pair: &Pair<Rule>) -> Result<u8, Error> { - let input = if pair.as_str().chars().rev().next() == Some('%') { &pair.as_str()[..pair.as_str().len()-1] } else { pair.as_str() }; + let input = if pair.as_str().ends_with('%') { &pair.as_str()[..pair.as_str().len()-1] } else { pair.as_str() }; use pest::error::{Error,ErrorVariant}; Ok(input.parse().map_err(|e: std::num::ParseIntError| { let var: ErrorVariant<Rule> = ErrorVariant::CustomError { message: e.to_string() }; @@ -215,7 +216,7 @@ fn convert_literal_lines(pair: Pair<Rule>) -> e::LiteralBlock { Rule::literal_line_blank => "\n", _ => unreachable!(), }.into()).collect(); - return e::LiteralBlock::with_children(children); + e::LiteralBlock::with_children(children) } fn convert_code_directive(pair: Pair<Rule>) -> e::LiteralBlock { diff --git a/parser/src/conversion/tests.rs b/parser/src/conversion/tests.rs index 89b0a1c..e042d01 100644 --- a/parser/src/conversion/tests.rs +++ b/parser/src/conversion/tests.rs @@ -41,11 +41,11 @@ fn convert_skipped_section() { let doctree = parse(SECTIONS).unwrap(); let lvl0 = doctree.children(); assert_eq!(lvl0.len(), 3, "Should be a paragraph and 2 sections: {:?}", lvl0); - + assert_eq!(lvl0[0], e::Paragraph::with_children(vec![ "Intro before first section title".to_owned().into() ]).into(), "The intro text should fit"); - + let lvl1a = ssubel_to_section(&lvl0[1]).children(); assert_eq!(lvl1a.len(), 2, "The 1st lvl1 section should have (a title and) a single lvl2 section as child: {:?}", lvl1a); //TODO: test title lvl1a[0] @@ -55,7 +55,7 @@ fn convert_skipped_section() { let lvl3a = ssubel_to_section(&lvl2[1]).children(); assert_eq!(lvl3a.len(), 1, "The 1st lvl3 section should just a title: {:?}", lvl3a); //TODO: test title lvl3a[0] - + let lvl1b = ssubel_to_section(&lvl0[2]).children(); assert_eq!(lvl1b.len(), 2, "The 2nd lvl1 section should have (a title and) a single lvl2 section as child: {:?}", lvl1b); //TODO: test title lvl1b[0] diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 23e97c7..4f1b8dd 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -22,7 +22,7 @@ pub fn parse_only(source: &str) -> Result<Document, Error> { convert_document(pairs) } -/// Parse into a document tree and resolve sections and references. +/// Parse into a document tree and resolve sections and references. pub fn parse(source: &str) -> Result<Document, Error> { parse_only(source).map(resolve_references) } diff --git a/parser/src/pair_ext_parse.rs b/parser/src/pair_ext_parse.rs index a04b3dd..fb5ba6b 100644 --- a/parser/src/pair_ext_parse.rs +++ b/parser/src/pair_ext_parse.rs @@ -6,16 +6,16 @@ use pest::error::{Error,ErrorVariant}; pub trait PairExt<R> where R: pest::RuleType { - fn parse<T, E>(&self) -> Result<T, Error<R>> where T: FromStr<Err = E>, E: ToString; + fn parse<T, E>(&self) -> Result<T, Box<Error<R>>> where T: FromStr<Err = E>, E: ToString; } impl<'l, R> PairExt<R> for Pair<'l, R> where R: pest::RuleType { - fn parse<T, E>(&self) -> Result<T, Error<R>> where T: FromStr<Err = E>, E: ToString { + fn parse<T, E>(&self) -> Result<T, Box<Error<R>>> where T: FromStr<Err = E>, E: ToString { self.as_str().parse().map_err(|e| to_parse_error(self.as_span(), &e)) } } -pub(crate) fn to_parse_error<E, R>(span: Span, e: &E) -> Error<R> where E: ToString, R: pest::RuleType { +pub(crate) fn to_parse_error<E, R>(span: Span, e: &E) -> Box<Error<R>> where E: ToString, R: pest::RuleType { let var: ErrorVariant<R> = ErrorVariant::CustomError { message: e.to_string() }; - Error::new_from_span(var, span) + Box::new(Error::new_from_span(var, span)) } diff --git a/parser/src/simplify.rs b/parser/src/simplify.rs index 4c254af..ab04964 100644 --- a/parser/src/simplify.rs +++ b/parser/src/simplify.rs @@ -33,6 +33,7 @@ use document_tree::{ #[derive(Debug)] +#[allow(dead_code)] enum NamedTargetType { NumberedFootnote(usize), LabeledFootnote(usize), @@ -43,11 +44,9 @@ enum NamedTargetType { SectionTitle, } impl NamedTargetType { + #[allow(dead_code)] fn is_implicit_target(&self) -> bool { - match self { - NamedTargetType::SectionTitle => true, - _ => false, - } + matches!(self, NamedTargetType::SectionTitle) } } @@ -55,7 +54,7 @@ impl NamedTargetType { struct Substitution { content: Vec<c::TextOrInlineElement>, /// If true and the sibling before the reference is a text node, - /// the text node gets right-trimmed. + /// the text node gets right-trimmed. ltrim: bool, /// Same as `ltrim` with the sibling after the reference. rtrim: bool, @@ -79,7 +78,7 @@ impl TargetsCollected { _ => unimplemented!(), } } - + fn substitution<'t>(self: &'t TargetsCollected, refname: &[NameToken]) -> Option<&'t Substitution> { // TODO: Check if the substitution would expand circularly if refname.len() != 1 { @@ -378,7 +377,7 @@ impl ResolvableRefs for c::TextOrInlineElement { // The corresponding SystemMessage node should go in a generated // section with class "system-messages" at the end of the document. use document_tree::Problematic; - let mut replacement: Box<Problematic> = Box::new(Default::default()); + let mut replacement: Box<Problematic> = Box::default(); replacement.children_mut().push( c::TextOrInlineElement::String(Box::new(format!("|{}|", e.extra().refname[0].0))) ); |
