Get part result from default()

Open
#85 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
rust
Domain
cli, tooling

Research direction

Start with the reproducer in testconfig.rs and main.rs, especially the blank test.toml and the confy::load_path:: call. Verify the loaded value and compare it with the Default implementations; done should mean the expected behavior for an empty file is clearly established and covered by a focused test or documentation.

Written by the indexing model from the issue text.

Description

Result is None when test.toml is blank. I wish to get as below, how can I do it?

Some(
    Test {
        sub: Some(
            [
                Data {
                    id: 2,
                    name: "3",
                },
                Data {
                    id: 4,
                    name: "5",
                },
            ],
        ),
    },
)

my code is here:

testconfig.rs:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Data {
    pub id: usize,
    pub name: String,
}
impl Default for Data {
    fn default() -> Self {
        Self {
            id: 1,
            name: "test1".to_string(),
        }
    }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {
    #[allow(dead_code)]
    pub sub: Option<Vec<Data>>,
}
impl Default for Test {
    fn default() -> Self {
        Self { sub: Some(vec![Data{id:2,name: "3".to_string()}, Data{id:4, name: "5".to_string()}]) }
    }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TestGlobalConfig {
    #[allow(dead_code)]
    pub test: Option<Test>,
}
impl Default for TestGlobalConfig {
    fn default() -> Self {
        Self { test: Some(Test::default()) }
    }
}

main.rs:

use std::sync::RwLock;
pub mod testconfig;
use crate::testconfig::TestGlobalConfig;
lazy_static::lazy_static! {
    static ref GLOBAL: RwLock<TestGlobalConfig> ={
        let path = concat!(env!("CARGO_MANIFEST_DIR"),"/test.toml");
        if let Ok(global_config) = confy::load_path::<TestGlobalConfig>(path) { 
            RwLock::new(global_config)
        } else {
            panic!("load config file {path:?} error!");
        }
    };
}
fn main() {
    let binding = GLOBAL.read().unwrap();
    let settings = &binding.test;
    println!("{:#?}", settings);
}
Dominant language
Rust
Stars
1k
Forks
78
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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.

More from rust-cli/confy

All issues in rust-cli/confy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.