Skip to content

Commit

Permalink
support for layered config using a serde Deserializer as a source
Browse files Browse the repository at this point in the history
This allows a serde Deserializer, representing a config file, to be used
as a value source, via the builder API, which is also new in this commit.

To use the serde feature, you have to put the `#[conf(serde)]` tag on your struct.

We also introduce a `ConfSerde` extension trait, which is implemented
when `#[conf(serde)]` is annotated, and provides implementation details
for this layered config feature on that struct.

The priority order is args > env > serde > defaults

We also support this in subcommands, via the `SubcommandsSerde` extension trait.

To further adjust the serde system, most fields have serde options like
`serde(rename)` and `serde(skip)`. Structs have a `serde(allow_unknown_fields)`
option.

Parameter and Repeat fields have a `serde(use_value_parser)` option, which
can be useful if you just want serde to read a string and then forward it
to the value parser.
  • Loading branch information
cbeck88 committed Oct 13, 2024
1 parent c90cacc commit 6d3bcab
Show file tree
Hide file tree
Showing 37 changed files with 4,701 additions and 786 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
uses: Swatinem/rust-cache@v2
with:
workspaces: . -> target
key: default-features
save-if: ${{ github.ref == 'refs/heads/develop' }}
- name: Build Rust
run: cargo check --verbose --locked
Expand All @@ -43,6 +44,32 @@ jobs:
- name: Check dirty git
uses: ./.github/actions/check-dirty-git

# builds and tests rust code, with --no-default-features, with --deny warnings. tests for dirty git
build-and-test-rust-no-default-features:
runs-on: ubuntu-latest

env:
RUSTFLAGS: --deny warnings

steps:
- uses: actions/checkout@v4
- name: Rustup update
run: rustup update
- name: Show cargo version
run: cargo --version
- name: rust build caching
uses: Swatinem/rust-cache@v2
with:
workspaces: . -> target
key: no-default-features
save-if: ${{ github.ref == 'refs/heads/develop' }}
- name: Build Rust
run: cargo check --no-default-features --verbose --locked
- name: Test Rust
run: cargo test --no-default-features --verbose --locked
- name: Check dirty git
uses: ./.github/actions/check-dirty-git

# lints and checks formatting of rust code
lint-rust:
runs-on: ubuntu-latest
Expand Down
172 changes: 128 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ include = [
[package]
name = "conf"
version = "0.1.1"
description = "A derive-based config parser for CLI args and env parameters"
categories = ["command-line-interface"]
description = "A derive-based config parser for CLI args, env, and structured config files"
categories = ["configuration"]
keywords = [
"argument",
"config",
Expand All @@ -42,10 +42,15 @@ bench = false
[dependencies]
conf_derive = { path = "./conf_derive", version = "0.1.1" }
clap = { version = "4.5.8", features = ["string"] }
serde = { version = "1", optional = true }

[features]
default = ["serde"]

[dev-dependencies]
assert_matches = "1.5"
escargot = "0.5"
figment = { version = "0.10", features = ["json", "toml"] }
http = { version = "1.1" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
url = "1"
4 changes: 4 additions & 0 deletions MOTIVATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,10 @@ The initial feature set was the features of `clap-derive` I had used most heavil
I ended up adding more features besides this before the first `crates.io` release as I started migrating more of my projects to this, and encountered things that were either harder to migrate, or were just additional features that I realized I wanted and could fit into the framework with relative ease.
In version 0.2, we added support for subcommands.
In version XXX, we added more support for layered config using structured data loaded from files. This was designed as a `serde` integration. This feature was created based on `github` discussions.
## Testing
If we change the same simple program that we used for testing `clap-derive` to use `conf` instead, we can see that the error handling in these scenarios becomes better.
Expand Down
Loading

0 comments on commit 6d3bcab

Please sign in to comment.