Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring in api.rs #830

Merged
merged 6 commits into from
May 3, 2024
Merged

Refactoring in api.rs #830

merged 6 commits into from
May 3, 2024

Conversation

khieta
Copy link
Contributor

@khieta khieta commented May 2, 2024

Description of changes

This PR performs some refactoring to reduce the size of our api.rs file. For the most part, the changes are just copy-and-paste, but I did need to add some new pub(crate) functions to handle the new layer of indirection. I’ll add review comments to any new functions added.

  • Moved EntityUid and PolicyId to a separate api/id.rs file.
  • Moved all error definitions to a separate api/err.rs file. (My theory is that this change will make it easier to keep track of the upcoming changes for [4.0] Consistent public error types in the Rust API #745.)
  • Moved tests in api.rs to join their friends in the tests.rs file.

The new files are included in api.rs using:

mod id;
pub use id::*;

mod err;
pub use err::*;

I believe this leaves the API unchanged from a user's perspective. Lmk if I'm wrong about that.

The changes in this PR introduce two new clippy warnings:

  • module_name_repetitions for both PolicyId and EntityId since these types end with "id" (the name of the containing module). I think we can safely ignore these because the types are pub used in api.rs, so users should write cedar_policy::PolicyId instead of cedar_policy::id::PolicyId.
  • redundant_pub_crate for the fold_partition function. The reason I made it pub(crate) instead of private is that it's also used in testing code. I think it is still effectively private with the current setting.

Issue #, if available

Checklist for requesting a review

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

  • A change "invisible" to users (e.g., documentation, changes to "internal" crates like cedar-policy-core, cedar-validator, etc.)

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

  • 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.

Comment on lines +130 to +134
/// Construct an [`EntityTypeName`] from the internal type.
/// This function is only intended to be used internally.
pub(crate) fn new(ty: ast::Name) -> Self {
Self(ty)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New function added by this PR

Comment on lines 210 to 226
/// Construct an [`EntityUid`] from the internal type.
/// This function is only intended to be used internally.
pub(crate) fn new(uid: ast::EntityUID) -> Self {
Self(uid)
}

/// Deconstruct an [`EntityUid`] to get the internal type.
/// This function is only intended to be used internally.
pub(crate) fn into_inner(self) -> ast::EntityUID {
self.0
}

/// Deconstruct an [`EntityUid`] to get the internal type.
/// This function is only intended to be used internally.
pub(crate) fn as_inner(&self) -> &ast::EntityUID {
&self.0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New functions added by this PR

Comment on lines 252 to 267
/// Construct a [`PolicyId`] from a source string
pub fn new(id: impl AsRef<str>) -> Self {
Self(ast::PolicyID::from_string(id.as_ref()))
}

/// Deconstruct an [`PolicyId`] to get the internal type.
/// This function is only intended to be used internally.
pub(crate) fn into_inner(self) -> ast::PolicyID {
self.0
}

/// Deconstruct an [`PolicyId`] to get the internal type.
/// This function is only intended to be used internally.
pub(crate) fn as_inner(&self) -> &ast::PolicyID {
&self.0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New functions added by this PR

Comment on lines +2843 to +2848
/// Deconstruct an [`Expression`] to get the internal type.
/// This function is only intended to be used internally.
#[cfg(test)]
pub(crate) fn into_inner(self) -> ast::Expr {
self.0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New function added by this PR

Comment on lines +2938 to +2943
/// Deconstruct an [`RestrictedExpression`] to get the internal type.
/// This function is only intended to be used internally.
#[cfg(test)]
pub(crate) fn into_inner(self) -> ast::RestrictedExpr {
self.0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New function added by this PR

@khieta
Copy link
Contributor Author

khieta commented May 2, 2024

Marking this as ready for review now that (most of) the CI has passed. I don't think the Java failures are the fault of this PR -- I think we just need to review & merge cedar-java#133. Will re-run CI once that PR is merged.

@khieta khieta marked this pull request as ready for review May 2, 2024 15:30
Copy link
Contributor

@cdisselkoen cdisselkoen left a comment

Choose a reason for hiding this comment

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

Definitely approve of the idea, making api.rs more organized.

cedar-policy/src/api.rs Outdated Show resolved Hide resolved
cedar-policy/src/api.rs Outdated Show resolved Hide resolved
cedar-policy/src/api.rs Outdated Show resolved Hide resolved
cedar-policy/src/api/err.rs Show resolved Hide resolved
cedar-policy/src/api/id.rs Outdated Show resolved Hide resolved
cedar-policy/src/api/id.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws left a comment

Choose a reason for hiding this comment

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

We should add allow directives for the new clippy lints if we're OK with them.

@khieta
Copy link
Contributor Author

khieta commented May 2, 2024

Latest updates:

  • Moved SlotId to the id.rs file (missed it on my first pass)
  • Added #[allow(clippy...)] to ignore the warnings introduced by my changes
  • Moved remaining EntityUid and PolicyId-related functions to id.rs
  • Switched to using implementations of AsRef and From (with doc(hidden)) instead of explicit into_inner and as_inner functions

The only potentially contentious change is this last one. Because AsRef was already defined for PolicyId using the str type, users may need to add annotations to distinguish between the different variants. I needed to do this in one doc comment in my last commit.

Copy link
Contributor

@andrewmwells-amazon andrewmwells-amazon left a comment

Choose a reason for hiding this comment

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

LGTM

@khieta khieta merged commit c0cc58f into main May 3, 2024
16 checks passed
@khieta khieta deleted the khieta/api-refactor branch May 3, 2024 14:09
@john-h-kastner-aws john-h-kastner-aws mentioned this pull request May 3, 2024
3 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.

None yet

4 participants