1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2 
3 // Based on https://github.com/dtolnay/syn/blob/2.0.37/src/path.rs.
4 
5 use syn::{
6     ext::IdentExt,
7     parse::{ParseStream, Result},
8     punctuated::Punctuated,
9     Ident, Path, PathArguments, PathSegment, Token,
10 };
11 
parse_path_segment(input: ParseStream<'_>) -> Result<PathSegment>12 fn parse_path_segment(input: ParseStream<'_>) -> Result<PathSegment> {
13     if input.peek(Token![super]) || input.peek(Token![self]) || input.peek(Token![crate]) {
14         let ident = input.call(Ident::parse_any)?;
15         return Ok(PathSegment::from(ident));
16     }
17 
18     let ident =
19         if input.peek(Token![Self]) { input.call(Ident::parse_any)? } else { input.parse()? };
20 
21     if input.peek(Token![::]) && input.peek3(Token![<]) {
22         Ok(PathSegment { ident, arguments: PathArguments::AngleBracketed(input.parse()?) })
23     } else {
24         Ok(PathSegment::from(ident))
25     }
26 }
27 
parse_path(input: ParseStream<'_>) -> Result<Path>28 pub(crate) fn parse_path(input: ParseStream<'_>) -> Result<Path> {
29     Ok(Path {
30         leading_colon: input.parse()?,
31         segments: {
32             let mut segments = Punctuated::new();
33             let value = parse_path_segment(input)?;
34             segments.push_value(value);
35             while input.peek(Token![::]) {
36                 let punct: Token![::] = input.parse()?;
37                 segments.push_punct(punct);
38                 let value = parse_path_segment(input)?;
39                 segments.push_value(value);
40             }
41             segments
42         },
43     })
44 }
45