Skip to content

Commit

Permalink
Merge pull request #1496 from Fancyflame/master
Browse files Browse the repository at this point in the history
Add `require_ident` to `Path`
  • Loading branch information
dtolnay authored Sep 10, 2023
2 parents 81945ea + 97f8eb8 commit 07a89dd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ impl Path {
None
}
}

/// Error if this does not contain only one segment.
///
/// A path is considered an ident if:
///
/// - the path has no leading colon,
/// - the number of path segments is 1, and
/// - the first path segment has no angle bracketed or parenthesized
/// path arguments.
#[cfg(feature = "parsing")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
pub fn require_ident(&self) -> Result<&Ident> {
self.get_ident().ok_or_else(|| {
crate::error::new2(
self.segments.first().unwrap().ident.span(),
self.segments.last().unwrap().ident.span(),
"expected this path to be an identifier",
)
})
}
}

ast_struct! {
Expand Down

0 comments on commit 07a89dd

Please sign in to comment.