Skip to content

Conversation

TroyKomodo
Copy link

Description of changes

Allow for construction of IDs, and entity type names at compile time.

Issue #, if available

#1828

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A breaking change requiring a major version bump to cedar-policy (e.g., changes to the signature of an existing API).
  • A backwards-compatible change requiring a minor version bump to cedar-policy (e.g., addition of a new API).
  • A bug fix or other functionality change requiring a patch to cedar-policy.
  • A change "invisible" to users (e.g., documentation, changes to "internal" crates like cedar-policy-core, cedar-validator, etc.)
  • A change (breaking or otherwise) that only impacts unreleased or experimental code.

I confirm that this PR (choose one, and delete the other options):

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).
  • Does not update the CHANGELOG because my change does not significantly impact released code.

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar formal model or DRT infrastructure.
  • Requires updates, and I have made / will make these updates myself. (Please include in your description a timeline or link to the relevant PR in cedar-spec, and how you have tested that your updates are correct.)
  • Requires updates, but I do not plan to make them in the near future. (Make sure that your changes are hidden behind a feature flag to mark them as experimental.)
  • I'm not sure how my change impacts cedar-spec. (Post your PR anyways, and we'll discuss in the comments.)

I confirm that docs.cedarpolicy.com (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar language specification.
  • Requires updates, and I have made / will make these updates myself. (Please include in your description a timeline or link to the relevant PR in cedar-docs. PRs should be targeted at a staging-X.Y branch, not main.)
  • I'm not sure how my change impacts the documentation. (Post your PR anyways, and we'll discuss in the comments.)

@TroyKomodo TroyKomodo marked this pull request as draft September 29, 2025 23:16

/// This is how we can create a `Name` at compile time
#[macro_export]
macro_rules! make_name {
Copy link
Author

Choose a reason for hiding this comment

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

@john-h-kastner-aws this is sort of how I think we should allow for compile time name creation.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a reasonable approach to me. It should be trivial to have a wrapper around all this in the cedar-policy crate to construct the public Name type, right?

Copy link
Author

Choose a reason for hiding this comment

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

Yes thats correct


#[test]
fn compile_time_name() {
const _: Name = make_name!("foo").expect("should be OK");
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, this expect is also evaluated at compile time, correct?

Copy link
Author

@TroyKomodo TroyKomodo Oct 8, 2025

Choose a reason for hiding this comment

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

yes thats correct, this is not a std::result::Result, so this is calling a different method i defined called expect which is const.


/// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is kind of nice change on its own. Wouldn't mind splitting it out into a separate small PR

}

/// Check if the `Id` is static
pub const fn is_static(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to use the name is_heap_allocated instead since this function will return true for an inline smolstr?

}
}

const fn validate_part(part: &str) -> Option<NameValidationError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

My main concern for merging a final implementation will be the correctness of this function. Ideally some of the logic should be shared (at least the list of reserved identifiers), and then unit tests ofc, but we can also do something relatively lightweight with the proptest crate

Copy link
Author

Choose a reason for hiding this comment

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

I agree, it would be ideal if we could share the logic. Not sure how to go about that tho.


/// This is how we can create a `Name` at compile time
#[macro_export]
macro_rules! make_name {
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a reasonable approach to me. It should be trivial to have a wrapper around all this in the cedar-policy crate to construct the public Name type, right?

@TroyKomodo TroyKomodo mentioned this pull request Oct 8, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants