Skip to content

Commit

Permalink
Adds EntityId::new() (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Craig Disselkoen <[email protected]>
  • Loading branch information
aaronjeline and cdisselkoen authored Jan 16, 2024
1 parent 381f77a commit 192f168
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions cedar-policy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `AsRef<str>` 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

Expand Down
17 changes: 14 additions & 3 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<str>) -> 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<Self, Self::Err> {
Expand Down

0 comments on commit 192f168

Please sign in to comment.