Code Monkey home page Code Monkey logo

Comments (2)

SergioBenitez avatar SergioBenitez commented on May 16, 2024

The Default trait doesn't integrate with Figment in any particular way. If you're reading the tips for app authors, you'll see that Default is implemented as an avenue for having a structure that can be merged into the figment as a default for config values, and it doesn't do anything beyond that.

Having said that, this line in your code is almost certainly not what you want. It's placing the address and timeout fields in your top-level config.

Back to the problem at hand. If we look at your figment with its defaults:

        let mut config = Figment::new().merge(Serialized::defaults(Config::default()));
        config = config.merge(Serialized::defaults(ListenerConfig::default()));

We'll have a figment with the following data after this runs:

default.listeners = [{address: 127.0.0.1:8080, timeout: 300_000}}
default.storage_path = "storage"
default.address = 127.0.0.1:8080
default.timeout = 300_000

Your TOML file as read contains:

default.listeners = [{address: 127.0.0.1:8088}]

When we merge this into the previous figment, we get:

default.listeners = [{address: 127.0.0.1:8088}]
default.storage_path = "storage"
default.address = 127.0.0.1:8080
default.timeout = 300_000

Since the TOML default.listeners array overrides the previous one. As you can see, the only listener in the array is incomplete, missing the address field.

If you want this to work, you probably want to specify a default via serde:

#[derive(Debug, Deserialize, Serialize)]
pub struct ListenerConfig {
    pub address: String,
    #[serde(default = "ListenerConfig::default_timeout")]
    pub timeout: u32,
}

from figment.

SergioBenitez avatar SergioBenitez commented on May 16, 2024

Hope the above resolved your question! If not, please feel free to reopen.

from figment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.