rust-cli / rust-cli/config-rs

Full path to broken field when Deserializing

Open
#83 2 comments 3 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

Hello

Let's say I want to deserialize some configuration using serde and something is broken (eg. missing field):

use config::{Config, File, FileFormat};
use serde_derive::Deserialize;

#[derive(Debug, Deserialize)]
struct Inner {
    test: String,
}

#[derive(Debug, Deserialize)]
struct Outer {
    inner: Inner,
}

const CFG: &str = r#"
inner:
    stuff: ABC
"#;

fn main() {
    let mut cfg = Config::new();
    cfg.merge(File::from_str(CFG, FileFormat::Yaml)).unwrap();
    let cfg: Outer = cfg.try_into().unwrap();
    println!("{:?}", cfg);
}

This produces this: thread 'main' panicked at 'called Result::unwrap()on anErrvalue: missing fieldtest', libcore/result.rs:1009:5.

But if the configuration is bigger and the user forgot to put some config field in there, this will not help them to know in which section of configuration it is missing. Could this be added?

I'm guessing that Config is its own deserialization „format“ for serde and therefore has its own de::Error type. Also, it seems formats are able to put the path in somehow, eg. I can get something like this from serde-yaml:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Message("missing field `test`", Some(Pos { marker: Marker { index: 17, line: 3, col: 9 }, path: "inner" }))', libcore/result.rs:1009:5

(I'd much prefer to have the path actually in the message itself, because it is what eventually gets printed to logs or somewhere).

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 Config's serde deserialization path from cfg.try_into() and inspect its de::Error handling. Compare the existing missing-field error with the serde-yaml example in the issue. Done means nested errors include the full configuration path in the displayed message, with coverage for the shown Inner/Outer case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.