Skip to content

Refactor built-in policies and how they are exposed - #12

Merged
faern merged 1 commit into
mainfrom
refactor-builtin-policies
Feb 4, 2026
Merged

Refactor built-in policies and how they are exposed#12
faern merged 1 commit into
mainfrom
refactor-builtin-policies

Conversation

@faern

@faern faern commented Feb 3, 2026

Copy link
Copy Markdown

Before this PR, the only way to access a built-in policy was via Policy::builtin(name: &str) -> Option<Self>. This provided no compile time guarantee that the policy you tried to use actually existed, and you had to unwrap at runtime to find out. The code also repeated all policy names a bunch of times, which can be error prone and also a bit harder to read.

This PR refactors how the library includes the built in policies and exposes them to library users. Key changes include:

  • Keep Policy::builtin(&str) -> Option<&Policy> for usage when the desired policy name is only known at runtime. For example user specified.
  • Expose a public static for each built in policy. This allows referring directly to a policy in code and get compile time checks if that policy exists.
  • Move actual raw policy files into their own directory, to separate them from the code
  • Put built-in policies inside LazyLock to only have to parse them once instead of on each access. But hide the LazyLock from the public API to make it possible to change the implementation later.
  • Put everything in a macro, so that adding another policy is a oneliner (plus adding the file with the raw unparsed policy)

@gregoire-mullvad gregoire-mullvad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this. I like that the BuiltinPolicy struct hides the LazyLock.

Comment thread src/policy/mod.rs Outdated
@faern
faern force-pushed the refactor-builtin-policies branch from 6837a8c to 495c5ac Compare February 4, 2026 09:48
@faern
faern merged commit 006f090 into main Feb 4, 2026
20 checks passed
@faern
faern deleted the refactor-builtin-policies branch February 4, 2026 13:11
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.

3 participants