Skip to content
Draft
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
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cedar-policy-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ unicode-security = "0.1.0"
regex = { version = "1.11", features = ["unicode"]}
linked-hash-map = { version = "0.5.6", features = ["serde_impl"] }
linked_hash_set = "0.1.5"
konst = "0.4"
const_panic = "0.2"

# wasm dependencies
serde-wasm-bindgen = { version = "0.6", optional = true }
Expand Down
2 changes: 2 additions & 0 deletions cedar-policy-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mod name;
pub use name::*;
mod ops;
pub use ops::*;
mod path;
pub use path::*;
mod pattern;
pub use pattern::*;
mod partial_value;
Expand Down
19 changes: 11 additions & 8 deletions cedar-policy-core/src/ast/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ use std::sync::Arc;
use thiserror::Error;

#[cfg(feature = "tolerant-ast")]
static ERROR_NAME: std::sync::LazyLock<Name> =
std::sync::LazyLock::new(|| Name(InternalName::from(Id::new_unchecked("EntityTypeError"))));
static ERROR_NAME: Name = Name(InternalName::new_from_path(Id::new_unchecked_from_static("EntityTypeError"), Path::empty(), None));

#[cfg(feature = "tolerant-ast")]
static ERROR_EID_SMOL_STR: std::sync::LazyLock<SmolStr> =
std::sync::LazyLock::new(|| SmolStr::from("Eid::ErrorEid"));
static ERROR_EID_SMOL_STR: SmolStr = SmolStr::new_static("Eid::ErrorEid");

#[cfg(feature = "tolerant-ast")]
static EID_ERROR_STR: &str = "Eid::Error";
Expand All @@ -50,7 +48,7 @@ static ENTITY_TYPE_ERROR_STR: &str = "EntityType::Error";
static ENTITY_UID_ERROR_STR: &str = "EntityUID::Error";

/// The entity type that Actions must have
pub static ACTION_ENTITY_TYPE: &str = "Action";
pub static ACTION_ENTITY_TYPE: Id = Id::new_unchecked_from_static("Action");

#[derive(PartialEq, Eq, Debug, Clone, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -94,7 +92,7 @@ impl EntityType {
pub fn is_action(&self) -> bool {
match self {
EntityType::EntityType(name) => {
name.as_ref().basename() == &Id::new_unchecked(ACTION_ENTITY_TYPE)
name.as_ref().basename() == &ACTION_ENTITY_TYPE
}
#[cfg(feature = "tolerant-ast")]
EntityType::ErrorEntityType => false,
Expand Down Expand Up @@ -145,11 +143,16 @@ impl EntityType {
pub fn from_normalized_str(src: &str) -> Result<Self, ParseErrors> {
Name::from_normalized_str(src).map(Into::into)
}

/// Convert a [`Name`] to an [`EntityType`]
pub const fn from_name(name: Name) -> Self {
Self::EntityType(name)
}
}

impl From<Name> for EntityType {
fn from(n: Name) -> Self {
Self::EntityType(n)
Self::from_name(n)
}
}

Expand Down Expand Up @@ -905,7 +908,7 @@ mod test {

#[test]
fn action_type_is_valid_id() {
assert!(Id::from_normalized_str(ACTION_ENTITY_TYPE).is_ok());
assert!(Id::from_normalized_str(ACTION_ENTITY_TYPE.as_ref()).is_ok());
}

#[cfg(feature = "tolerant-ast")]
Expand Down
11 changes: 11 additions & 0 deletions cedar-policy-core/src/ast/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl Id {
Id(s.into())
}

/// Similar to `new_unchecked`, but for static strings which can be `const`
/// constructed
pub(crate) const fn new_unchecked_from_static(s: &'static str) -> Id {
Id(SmolStr::new_static(s))
}

/// Get the underlying string
pub fn into_smolstr(self) -> SmolStr {
self.0
Expand All @@ -61,6 +67,11 @@ impl Id {
pub fn is_reserved(&self) -> bool {
self.as_ref() == RESERVED_ID
}

/// Check if the `Id` is static
pub const fn is_static(&self) -> bool {
!self.0.is_heap_allocated()
}
}

impl AsRef<str> for Id {
Expand Down
Loading
Loading