Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably update the example to use new

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I want to leave the .parse() example because it's slightly weird to newcomers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the parse example to the FromStr doc string? Most users should be using new to construct this

///
/// ```
/// # 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