Skip to content
Open
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
21 changes: 13 additions & 8 deletions cedar-policy-core/src/ast/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ 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: std::sync::LazyLock<Name> = std::sync::LazyLock::new(|| {
Name(InternalName::from(Id::new_unchecked_const(
"EntityTypeError",
)))
});

#[cfg(feature = "tolerant-ast")]
static EID_ERROR_STR: &str = "Eid::Error";
Expand Down Expand Up @@ -90,7 +93,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() == &Id::new_unchecked_const(ACTION_ENTITY_TYPE)
}
#[cfg(feature = "tolerant-ast")]
EntityType::ErrorEntityType => false,
Expand Down Expand Up @@ -921,15 +924,17 @@ mod test {
assert!(!error_type.is_action());
assert_eq!(error_type.qualify_with(None), EntityType::ErrorEntityType);
assert_eq!(
error_type.qualify_with(Some(&Name(InternalName::from(Id::new_unchecked(
error_type.qualify_with(Some(&Name(InternalName::from(Id::new_unchecked_const(
"EntityTypeError"
))))),
EntityType::ErrorEntityType
);

assert_eq!(
error_type.name(),
&Name(InternalName::from(Id::new_unchecked("EntityTypeError")))
&Name(InternalName::from(Id::new_unchecked_const(
"EntityTypeError"
)))
);
assert_eq!(error_type.loc(), None)
}
Expand All @@ -946,9 +951,9 @@ mod test {

#[test]
fn entity_type_serialization() {
let entity_type = EntityType::EntityType(Name(InternalName::from(Id::new_unchecked(
"some_entity_type",
))));
let entity_type = EntityType::EntityType(Name(InternalName::from(
Id::new_unchecked_const("some_entity_type"),
)));
let serialized = serde_json::to_string(&entity_type).unwrap();

assert_eq!(serialized, r#""some_entity_type""#);
Expand Down
12 changes: 12 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,13 @@ impl Id {
Id(s.into())
}

/// Create a new `Id` from a static string
///
/// This never allocates.
pub(crate) const fn new_unchecked_const(s: &'static str) -> Self {
Id(SmolStr::new_static(s))
}

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

/// Return if the `Id` is heap-allocated
pub const fn is_heap_allocated(&self) -> bool {
self.0.is_heap_allocated()
}
}

impl AsRef<str> for Id {
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/ast/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl InternalName {
/// Get the [`InternalName`] representing the reserved `__cedar` namespace
pub fn __cedar() -> Self {
// using `Id::new_unchecked()` for performance reasons -- this function is called many times by validator code
Self::unqualified_name(Id::new_unchecked("__cedar"), None)
Self::unqualified_name(Id::new_unchecked_const("__cedar"), None)
}

/// Create an [`InternalName`] with no path (no namespaces).
Expand Down
Loading