diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-02 11:30:02 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-31 10:27:15 +0200 |
commit | dcfa5e1c694bdcb237801eb09398d70f98a39a97 (patch) | |
tree | 0c311e1d857b90f61b638f7c8782c9b4842e3364 /askama_parser | |
parent | faf2e34cbf2e2049f2f473005e3c28dcc0d0a949 (diff) | |
download | askama-dcfa5e1c694bdcb237801eb09398d70f98a39a97.tar.gz askama-dcfa5e1c694bdcb237801eb09398d70f98a39a97.tar.bz2 askama-dcfa5e1c694bdcb237801eb09398d70f98a39a97.zip |
parser: simplify top-level parser match
Diffstat (limited to 'askama_parser')
-rw-r--r-- | askama_parser/src/lib.rs | 53 |
1 files changed, 24 insertions, 29 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 9559ee0..ab3eb83 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -63,40 +63,35 @@ pub struct Ast<'a> { impl<'a> Ast<'a> { pub fn from_str(src: &'a str, syntax: &Syntax<'_>) -> Result<Self, ParseError> { - match Node::parse(src, &State::new(syntax)) { - Ok((left, nodes)) => { - if !left.is_empty() { - Err(ParseError(format!("unable to parse template:\n\n{left:?}"))) - } else { - Ok(Self { nodes }) - } - } - - Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => { - let nom::error::Error { input, .. } = err; - let offset = src.len() - input.len(); - let (source_before, source_after) = src.split_at(offset); + let err = match Node::parse(src, &State::new(syntax)) { + Ok((left, nodes)) => match left.is_empty() { + true => return Ok(Self { nodes }), + false => return Err(ParseError(format!("unable to parse template:\n\n{left:?}"))), + }, + Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => err, + Err(nom::Err::Incomplete(_)) => return Err(ParseError("parsing incomplete".into())), + }; - let source_after = match source_after.char_indices().enumerate().take(41).last() { - Some((40, (i, _))) => format!("{:?}...", &source_after[..i]), - _ => format!("{source_after:?}"), - }; + let nom::error::Error { input, .. } = err; + let offset = src.len() - input.len(); + let (source_before, source_after) = src.split_at(offset); - let (row, last_line) = source_before.lines().enumerate().last().unwrap(); - let column = last_line.chars().count(); + let source_after = match source_after.char_indices().enumerate().take(41).last() { + Some((40, (i, _))) => format!("{:?}...", &source_after[..i]), + _ => format!("{source_after:?}"), + }; - let msg = format!( - "problems parsing template source at row {}, column {} near:\n{}", - row + 1, - column, - source_after, - ); + let (row, last_line) = source_before.lines().enumerate().last().unwrap(); + let column = last_line.chars().count(); - Err(ParseError(msg)) - } + let msg = format!( + "problems parsing template source at row {}, column {} near:\n{}", + row + 1, + column, + source_after, + ); - Err(nom::Err::Incomplete(_)) => Err(ParseError("parsing incomplete".into())), - } + Err(ParseError(msg)) } } |