Skip to content

Add derive macros to cli config - #684

Closed
thebrandonlucas wants to merge 13 commits into
payjoin:masterfrom
thebrandonlucas:feat/config-error-handling
Closed

thebrandonlucas wants to merge 13 commits into
payjoin:masterfrom
thebrandonlucas:feat/config-error-handling

Conversation

@thebrandonlucas

@thebrandonlucas thebrandonlucas commented May 6, 2025 •

Copy link
Copy Markdown
Collaborator

This WIP commit begins refactoring to use the derive macros provided by clap to replace the builder we are currently using. This is essentially a sample of how I intend to refactor this code if we are in agreement.

Clap appears to recommend using derive when a lot of flexibility isn't needed. See FAQ. I believe this would allow us to simplify the logic, make the code DRYer, make the code more readable and maintainable, and conform to clap recommendations.

There may barrier or complexity I'm failing to see here, but it seems like this refactor would make things much more maintainable and easier to build on moving forward. Also, if we find ourselves needing the lower-level API's provided by the builder method, we can still use it on an as-needed basis

This WIP commit begins refactoring to use the derive macros provided
by clap to replace the builder method for maintainability and ease
of use.
@DanGould

DanGould commented May 6, 2025

Copy link
Copy Markdown
Member

I do see that derive macros could save us some maintenance, but I'm not sure how to incorporate the macros on the config that also takes in toml. It would be much easier to evaluate if the proof of concept compiled.

The main problem with our Config at the moment seems that it's overloaded to parse toml and merge args with that toml.
If these were separated into two-phase struct TomlConfig and [Resolved]Config that gets created from TomlConfig the concerns may be separated to more correctly identify and throw errors. (I'm writing [Resolved]Config because that's effectively what our current struct does even though it lacks the name and the Towards Impeccable Rust video mentions this two-phase struct pattern with these names).

It's possible that the Arg derive macros belong on the [Resolved]Config, but it's also possible there should be Arg structs that accept the clap derives, as written here, which get merged with the TomlConfig to create a [Resolved]Config. The reason I think they might belong on our existing [Resolved]Config is because in our code args take highest precedence, which override toml, which overrides defaults. It's not yet clear to me whether this override is easier to express with a 2-tier heirarchy of TomlConfig and [Resolved]Config or if a third of Args would make this more explicit and easier to maintain.

My biggest priority with the config is to make misuse inexpressible, then to report errors beautifully, then to be DRY. In my experience code with the clearest semantics tends to be the easiest to maintain, most beautiful, and most DRY as an unintended consequence.

`VersionConfig` is part of the config but not actually an argument,
and breaks compilation because it was being treated as an argument.

Added the #[arg(skip)] macro to fix that.

Also, updated the outdated `#[clap(flatten)]` macro in favor of the
`#[command(flatten)]` macro.
@coveralls

Copy link
Copy Markdown
Collaborator

Pull Request Test Coverage Report for Build 14908012592

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 8 (0.0%) changed or added relevant lines in 1 file are covered.
  • 45 unchanged lines in 8 files lost coverage.
  • Overall coverage increased (+0.02%) to 82.072%

Changes Missing Coverage Covered Lines Changed/Added Lines %
payjoin-cli/src/app/config.rs 0 8 0.0%
Files with Coverage Reduction New Missed Lines %
payjoin/src/receive/mod.rs 1 96.77%
payjoin/src/send/mod.rs 2 94.46%
payjoin/src/receive/v1/exclusive/mod.rs 3 96.39%
payjoin/src/receive/multiparty/error.rs 5 50.0%
payjoin/src/uri/mod.rs 5 87.84%
payjoin/src/receive/v1/mod.rs 7 95.38%
payjoin-cli/src/app/config.rs 11 74.87%
payjoin/src/receive/optional_parameters.rs 11 81.52%
Totals Coverage Status
Change from base Build 14799403587: 0.02%
Covered Lines: 5475
Relevant Lines: 6671

💛 - Coveralls

This is a WIP commit to get things to a compiling state.

Running with `--help` works, but full functionality is not yet restored
Performs some cleanup, more accurate commenting, and renames
builder to config to more accurately make the distinction
between what is being passed via cli and config file
WIP - introduce concept of a RawConfig and ValidatedConfig.
Derived from concept TomlConfig and ResolvedConfig in
[this](https://youtu.be/qfknfCsICUM?feature=shared&t=2135) video.
Currently, payjoin-cli parses the command line args, then
conditionally overrides the config file with each that are
present, and validation occurs "along the way" while parsing
and adding default values from the config file.

This method means the configuration code is tightly coupled.

This commit begins splitting the cli parsing and config file
parsing into two separate steps, with the plan to validate and
override config values with cli values as a separate third step.
Adds validated versions of RawBitcoindConfig, and raw version configs.
Update version related feature flagged code and remove the
determine_version code as it is no longer necessary with the removal
of the .version field in ValidatedConfig
I wasn't setting bip78/bip77 error message defaults anywhere in
the config. So when I didn't pass that in the cli it would break.
./contrib/lint.sh was revealing two lint errors, one caused by
a missing "v2" feature flags for the OhttpKeys struct, another
caused by passing the 'config' crate to the app rather than
the 'validated_config' struct as a result of a missed variable
rename.
@thebrandonlucas

Copy link
Copy Markdown
Collaborator Author

Closing this exploratory draft in favor of #703

thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 20, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 20, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 20, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 20, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 27, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 29, 2025
Refactors the code to use derive macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on derive vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
thebrandonlucas added a commit to thebrandonlucas/rust-payjoin that referenced this pull request May 29, 2025
Refactors the code to use proc macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on proc macros vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](#1)
DanGould added a commit that referenced this pull request Jun 1, 2025
Refactors the code to use proc macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See

[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on proc macro vs builder method rationales.

Discussion preceding this PR in #684 and
[here](thebrandonlucas#1)
Johnosezele pushed a commit to Johnosezele/rust-payjoin that referenced this pull request Jun 2, 2025
Refactors the code to use proc macros over the "legacy" builder method
as per `clap`'s recommendation.

The macros allow the code to be more maintainable, readable, and
reusable moving forward.

See
[here](https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis)
for more details on proc macros vs builder method rationales.

Discussion preceding this PR in payjoin#684 and
[here](thebrandonlucas#1)
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