-
-
Notifications
You must be signed in to change notification settings - Fork 49
feat(config): hold a value to the choices its setting declares #868
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
|
|
||
| use crate::source::SourceKind; | ||
| use crate::ty::{Parser, Ty}; | ||
| use crate::value::Const; | ||
| use crate::value::{Const, Value}; | ||
|
|
||
| /// A setting's index in its registry. | ||
| /// | ||
|
|
@@ -76,6 +76,12 @@ pub struct PropMeta { | |
| /// asks the registry for its own kind and iterates what it finds, which is the whole | ||
| /// mechanism behind hk's git and pkl layers and aube's `.npmrc`. | ||
| pub bindings: &'static [(&'static str, &'static str)], | ||
| /// The only values this setting accepts, when it says. | ||
| /// | ||
| /// Empty means anything the type allows. Declared in the spec as `choice` nodes, where they | ||
| /// already reach the docs, the JSON schema and completions — and, until this, nothing that | ||
| /// *resolved* a value, so a CLI documenting three allowed values accepted a fourth in silence. | ||
| pub choices: &'static [Const], | ||
| /// Kept out of documentation and completions. Still settable. | ||
| pub hide: bool, | ||
| /// Why not to use this any more. | ||
|
|
@@ -98,6 +104,7 @@ impl PropMeta { | |
| parse: None, | ||
| envs: &[], | ||
| bindings: &[], | ||
| choices: &[], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choices never reach generated registriesHigh Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit e78b52b. Configure here. |
||
| hide: false, | ||
| deprecated: None, | ||
| renamed_to: None, | ||
|
|
@@ -121,6 +128,37 @@ pub struct Lookup { | |
| pub renamed_from: Option<&'static str>, | ||
| } | ||
|
|
||
| impl PropMeta { | ||
| /// The first value here that this setting does not allow, if there is one. | ||
| /// | ||
| /// A collection is checked item by item, because choices on a `list<string>` mean each item is | ||
| /// one of them — the same rule `usage g json-schema` follows, which puts the enum on every value | ||
| /// position rather than on the container. Returning the offender rather than a bool is what lets | ||
| /// the warning quote the item that is wrong instead of the whole list it was in. | ||
| pub fn refuses<'v>(&self, value: &'v Value) -> Option<&'v Value> { | ||
| if self.choices.is_empty() { | ||
| return None; | ||
| } | ||
| match value { | ||
| Value::List(items) => items.iter().find_map(|item| self.refuses(item)), | ||
| Value::Map(entries) => entries.values().find_map(|item| self.refuses(item)), | ||
| scalar => match self.choices.iter().any(|choice| choice.matches(scalar)) { | ||
| true => None, | ||
| false => Some(scalar), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// What it allows, written the way the spec declared them, for a message. | ||
| pub fn allowed(&self) -> String { | ||
| self.choices | ||
| .iter() | ||
| .map(|choice| choice.to_value().display()) | ||
| .collect::<Vec<_>>() | ||
| .join(", ") | ||
| } | ||
| } | ||
|
|
||
| impl Registry { | ||
| pub const fn new(props: &'static [PropMeta]) -> Self { | ||
| Self { props } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a CLI generates its registry from a spec containing
choicenodes, the generator leavesPropMeta::choicesempty, sorefusesaccepts every value and the declared restrictions remain unenforced.