From 192f168eb586d8981a77310df13889242227c1e5 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 16 Jan 2024 11:46:42 -0500 Subject: [PATCH] Adds `EntityId::new()` (#583) Co-authored-by: Craig Disselkoen --- cedar-policy/CHANGELOG.md | 1 + cedar-policy/src/api.rs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cedar-policy/CHANGELOG.md b/cedar-policy/CHANGELOG.md index 1a5821373..9d3ebe0ee 100644 --- a/cedar-policy/CHANGELOG.md +++ b/cedar-policy/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `AsRef` implementation for `PolicyId`. (#504, resolving #503) - New API `template_links` for `Policy` to retrieve the linked values for a template-linked policy. (#515, resolving #489) +- Added `EntityId::new()` constructor (#583, resolving #553) ### Changed diff --git a/cedar-policy/src/api.rs b/cedar-policy/src/api.rs index ffa27032c..5f0d22218 100644 --- a/cedar-policy/src/api.rs +++ b/cedar-policy/src/api.rs @@ -1701,9 +1701,10 @@ impl<'a> Diagnostic for ValidationWarning<'a> { /// Identifier portion of the [`EntityUid`] type. /// -/// An `EntityId` can can be constructed using [`EntityId::from_str`] or by -/// calling `parse()` on a string. This implementation is `Infallible`, so the -/// parsed `EntityId` can be extracted safely. +/// All strings are valid [`EntityId`]s, and can be +/// constructed either using [`EntityId::new`] +/// or by using the implementation of [`FromStr`]. This implementation is [`Infallible`], so the +/// parsed [`EntityId`] can be extracted safely. /// /// ``` /// # use cedar_policy::EntityId; @@ -1714,6 +1715,16 @@ impl<'a> Diagnostic for ValidationWarning<'a> { #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, RefCast)] pub struct EntityId(ast::Eid); +impl EntityId { + /// Construct an [`EntityId`] from a source string + pub fn new(src: impl AsRef) -> Self { + match src.as_ref().parse() { + Ok(eid) => eid, + Err(infallible) => match infallible {}, + } + } +} + impl FromStr for EntityId { type Err = Infallible; fn from_str(eid_str: &str) -> Result {