Field defaults overwrite explicitly selected unit enum variants

Aperta
#511 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

Start at the derived Configuration implementation for enums and inspect how contains_non_secret_data() treats unit variants. Add a regression test using the issue’s LogFormat example and field default. Done means the explicitly configured Json value survives try_build() instead of being replaced by Plain.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

The Configuration derive returns false from contains_non_secret_data() for unit enum variants. This causes #[confik(default = …)] to overwrite explicit values—for example, configured LogFormat::Json becomes the default LogFormat::Plain.

An explicitly selected variant should count as supplied data and take precedence over the field default.

This works:

// Cargo.toml:
// confik = "=0.15.12"
// serde = { version = "1", features = ["derive"] }

use confik::{Configuration, TomlSource};

#[derive(Debug, PartialEq, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
enum LogFormat {
    Plain,
    Json,
}

impl Configuration for LogFormat {
    type Builder = Option<Self>;
}

#[derive(Configuration)]
struct Config {
    #[confik(default = LogFormat::Plain)]
    format: LogFormat,
}

fn main() {
    let config = Config::builder()
        .override_with(TomlSource::new(r#"format = "json""#))
        .try_build()
        .unwrap();

    assert_eq!(config.format, LogFormat::Json);
}

This doesn’t — a derived implementation with the same field default:

// Cargo.toml: confik = "=0.15.12"

use confik::{Configuration, TomlSource};

#[derive(Debug, PartialEq, Configuration)]
#[confik(forward(serde(rename_all = "lowercase")))]
enum LogFormat {
    Plain,
    Json,
}

#[derive(Configuration)]
struct Config {
    #[confik(default = LogFormat::Plain)]
    format: LogFormat,
}

fn main() {
    let config = Config::builder()
        .override_with(TomlSource::new(r#"format = "json""#))
        .try_build()
        .unwrap();

    // Fails: left is Plain, right is Json.
    assert_eq!(config.format, LogFormat::Json);
}
Lingua principale
Rust
Stelle
46
Fork
3
Merge medio
19h 35m
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di x52dev/confik

Tutte le issue di x52dev/confik

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.