Skip to content

Commit 540957c

Browse files
author
Aaron Eline
authored
Added PolicyId::new() (#587)
1 parent 5bc10fd commit 540957c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

cedar-policy/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- `PolicyId::new()` added to `PolicyId` (#587, resolving #551)
1213
- `AsRef<str>` implementation for `PolicyId`. (#504, resolving #503)
1314
- New API `template_links` for `Policy` to retrieve the linked values for a
1415
template-linked policy. (#515, resolving #489)

cedar-policy/src/api.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,18 +2691,25 @@ impl TemplateResourceConstraint {
26912691

26922692
/// Unique ids assigned to policies and templates.
26932693
///
2694-
/// A `PolicyId` can can be constructed using [`PolicyId::from_str`] or by
2694+
/// A [`PolicyId`] can can be constructed using [`PolicyId::from_str`] or by
26952695
/// calling `parse()` on a string. This currently always returns `Ok()`.
26962696
///
26972697
/// ```
26982698
/// # use cedar_policy::PolicyId;
2699-
/// let id : PolicyId = "my-id".parse().unwrap();
2699+
/// let id = PolicyId::new("my-id");
27002700
/// # assert_eq!(id.as_ref(), "my-id");
27012701
/// ```
27022702
#[repr(transparent)]
27032703
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize, RefCast)]
27042704
pub struct PolicyId(ast::PolicyID);
27052705

2706+
impl PolicyId {
2707+
/// Construct a [`PolicyId`] from a source string
2708+
pub fn new(id: impl AsRef<str>) -> Self {
2709+
Self(ast::PolicyID::from_string(id.as_ref()))
2710+
}
2711+
}
2712+
27062713
impl FromStr for PolicyId {
27072714
type Err = ParseErrors;
27082715

0 commit comments

Comments
 (0)