diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index ad086a315..e766bb672 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -1342,10 +1342,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } fn visit_path(&mut self, buf: &mut Buffer, path: &[&str]) -> DisplayWrap { - for (i, part) in path.iter().enumerate() { - if i > 0 { - buf.write("::"); + let mut it = path.iter(); + match it.next() { + Some(&part) if part != "::" => { + buf.write(part); } + _ => {} + } + for part in it { + buf.write("::"); buf.write(part); } DisplayWrap::Unwrapped @@ -1357,10 +1362,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { path: &[&str], args: &[Expr], ) -> Result { - for (i, part) in path.iter().enumerate() { - if i > 0 { - buf.write("::"); + let mut it = path.iter(); + match it.next() { + Some(&part) if part != "::" => { + buf.write(part); } + _ => {} + } + for part in it { + buf.write("::"); buf.write(part); } buf.write("("); diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 7224d0285..3e52c8511 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -1,7 +1,7 @@ use nom::branch::alt; use nom::bytes::complete::{escaped, is_not, tag, take_until}; use nom::character::complete::{anychar, char, digit1}; -use nom::combinator::{complete, map, opt}; +use nom::combinator::{complete, map, opt, value}; use nom::error::ParseError; use nom::multi::{many0, many1, separated_list0, separated_list1}; use nom::sequence::{delimited, pair, tuple}; @@ -312,10 +312,12 @@ fn expr_var_call(i: &[u8]) -> IResult<&[u8], Expr> { } fn path(i: &[u8]) -> IResult<&[u8], Vec<&str>> { + let root = opt(value("::", ws(tag("::")))); let tail = separated_list1(ws(tag("::")), identifier); - let (i, (start, _, rest)) = tuple((identifier, ws(tag("::")), tail))(i)?; - - let mut path = vec![start]; + let (i, (root, start, _, rest)) = tuple((root, identifier, ws(tag("::")), tail))(i)?; + let mut path = Vec::new(); + path.extend(root); + path.push(start); path.extend(rest); Ok((i, path)) }