Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting an Err(missing field `web3_node_provider`)?

I am new to Rust–confused why I am getting an Err(missing field "web3_node_provider") error when I cargo run using config-rs. Appears to fail at s.try_deserialize()

config.rs

use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(unused)]
struct Web3NodeProvider {
    ethereum_mainnet_node_url_http: String,
    alchemy_api_key: String,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Settings {
    web3_node_provider: Web3NodeProvider,
}

impl Settings {
    pub fn new() -> Result<Self, ConfigError> {
        let s = Config::builder()
            .add_source(File::with_name("config/default"))
            .add_source(File::with_name("config/local").required(false))
            .add_source(Environment::with_prefix("app"))
            .build()?;
        s.try_deserialize()
    }
}

main.rs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mod settings;

use settings::Settings;

fn main() {
    let settings = Settings::new();
    println!("{:?}", settings);
}

I’ve pretty much exactly followed the hierarchy example in config-rs, so I’m sure I’m just misunderstanding something basic or missing something. I am able to use "Web3NodeProvider.url" but not "web3_node_provider.ethereum_mainnet_node_url_http".

default.toml

[Web3NodeProvider]
ethereum_mainnet_node_url_http = "https://eth-mainnet.g.alchemy.com/v2/"
alchemy_api_key = "alchemy-api-key"

local.toml

[Web3NodeProvider]
alchemy_api_key = "randomapikey"

>Solution :

I think you have to name your field in config as name of attribute not name of class. Like so:

[web3_node_provider]
ethereum_mainnet_node_url_http = "https://eth-mainnet.g.alchemy.com/v2/"
alchemy_api_key = "alchemy-api-key"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading