rust-cli / rust-cli/config-rs

Preserving casing while building configuration

Open
#564 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

When reading a JSON file, for example:

{
    "myStructField": "value"
}

Using config:

let settings = Config::builder()
    .add_source(config::File::with_name("src/settings.json").required(false))
    .build()
    .unwrap();

And trying to deserialize it to a struct defined as:

#[derive(Debug, Deserialize)]
#[serde(rename_all = "cascalCase")]
pub struct MyStruct {
    my_struct_field: String,
}

The program will panic with an error that myStructField does not exist. This is correct because config will by default lowercase all the keys. This creates a scenario where it's not possible to use serde deserializer to handle this, since there's no rename_all option that will fit this case.

I've seen this issue repeating multiple times, but didn't see an issue that suggests a way to solve this. I'm not too familiar with the internal implementation, but would it be possible to add an option to preserve the keys while building the configuration, for example by adding .add_source(config::File::with_name("src/settings.json").preserve_casing(true).required(false))?

If it should be possible, and it's accepted, I can try implementing this (unless there's some alternative to get through this).
We'd like to use the config library, but we have many JSON file configurations that this creates a lot of work to modify them, and the need to preserve the two versions of the JSON files for backward compatibilities.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how config::File loads JSON and lowercases keys during configuration building, then inspect the existing source options around that behavior. The change would be complete when a source can preserve JSON key casing and the provided serde example deserializes without requiring configuration-file rewrites.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.