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

Generate option value derivations in the rust parser. #20535

Merged
merged 3 commits into from
Feb 13, 2024

Conversation

benjyw
Copy link
Contributor

@benjyw benjyw commented Feb 12, 2024

We use these to show the source of option values in help output.

The derivations can be a lot of data, so we only produce them
if asked to.

Note that the derivations currently only show the source type
(args, env, config, default) but not, for example, which config
file a value came from. A future change may add this, but it
will require some changes to the Source enum which are out
of scope for this PR.

Also: use maplit for easy hashmap literals in the test.

@benjyw benjyw added the category:internal CI, fixes for not-yet-released features, etc. label Feb 12, 2024
@@ -200,20 +199,28 @@ pub enum Source {

#[derive(Debug)]
pub struct OptionValue<T> {
pub derivation: Option<Vec<(Source, T)>>,
// Scalar options are always set from a single source, so we provide that
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just documenting why we have this here but not for list or dict-valued options. We could also get rid of it, but it is slightly useful in error messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just confirming my understanding: source is the same as the last source in derivation (if that is Some), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct - it's broken out here because derivation may not be set, and we were already using it prior in a handful of user-facing messages.

pub source: Source,
pub value: T,
}

impl<T> Deref for OptionValue<T> {
Copy link
Contributor Author

@benjyw benjyw Feb 12, 2024

Choose a reason for hiding this comment

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

I prefer to be explicit about which field we're accessing.

@benjyw
Copy link
Contributor Author

benjyw commented Feb 12, 2024

Most of the diff lines are the added testing.

Copy link
Contributor

@huonw huonw left a comment

Choose a reason for hiding this comment

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

Nice tests!

@@ -200,20 +199,28 @@ pub enum Source {

#[derive(Debug)]
pub struct OptionValue<T> {
pub derivation: Option<Vec<(Source, T)>>,
// Scalar options are always set from a single source, so we provide that
Copy link
Contributor

Choose a reason for hiding this comment

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

Just confirming my understanding: source is the same as the last source in derivation (if that is Some), right?

("key1".to_string(), Val::Int(1)),
("key2".to_string(), Val::String("val2".to_string())),
]),
hashmap! {
Copy link
Contributor

Choose a reason for hiding this comment

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

maplit looks nice 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was googling "macro for hashmap literals" and it turned out we already had one we were using elsewhere.

@benjyw benjyw merged commit 600a67b into pantsbuild:main Feb 13, 2024
24 checks passed
@benjyw benjyw deleted the option_value_derivations branch February 13, 2024 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants