editable config file?
haug1 opened this issue · 6 comments
I want to start using confy
in my project, but I can't figure out how to include a manually editable configuration file?
From my understanding of the practical example in the readme, it is loading a .toml file named "app-name" out as the values. I try to include a app-name.toml
in the root of my project, but it does not work.
Is it possible to do this?
Here's the output I'm getting:
thread '<unnamed>' panicked at 'Config read error: BadTomlData(Error { inner: ErrorInner { kind: Custom, line: Some(0), col: 0, at: Some(0), message: "missing field
secret_key", key: [] } })', src/utils/config.rs:25:36
From this code:
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Config {
pub secret_key: String,
pub test_attr: u8,
}
impl ::std::default::Default for Config {
fn default() -> Self {
Self {
test_attr: 0,
secret_key: "secret".into(),
}
}
}
pub fn load_config() -> Config {
confy::load("app-name").expect("Config read error")
}
In my project root folder app-name.toml
alongside cargo.toml
:
secret_key = "something"
test_attr = 123
Is this a feature request? I dont know.
My bad, I wasn't specifying the full path for the file.
fn config_filename() -> &'static str {
"my-config"
}
fn config_filepath() -> String {
let path = std::env::current_dir().expect("config_filepath() : Current dir error");
format!("{}/{}", path.display(), config_filename())
}
pub fn load_config() -> Config {
confy::load(&config_filepath()).expect("Config read error")
}
confy
uses directory
(and that should be upgraded).
But depending on your system, it should be on the common path for configuration files. See https://crates.io/crates/directories/2.0.0 for specific infos (look for config_dir).
By the way do you think it would have been clearer if a function like the one proposed in #36 was available ?
pub fn get_configuration_file_path(name: &str) -> Result<PathBuf, ConfyError>
I will try to merge that and create a new version for the crate fo the end of the year
@Zykino See my implementation. I would like it to just do that behind the scenes from the load
function. If not, expose a new load
that does it. I don't want to be calling a lot of small functions from the library by hand. I simply want a library does it. And it seems great so far although I'm not far into using it 👍
PS.: I'm very new to Rust and just starting out. Just trying to give my impression
Which version are you using ? In 0.4 (and hopefully later) load
and store
should store in the standards project directories. To store at any other path use load_path
and store_path
instead.
So on Linux, load
should create the file in ~/.config/my-config/my-config.toml
I cannot say I have a lot of experience in Rust yet either 😄.
Ah, that makes a lot of sense then. I didn't even think to look in ~/.config/my-config/my-config.toml
, but I must admit that is very intuitive and probably good practice. You can disregard my earlier advice then, this is probably better, just a misunderstanding on my part. Thanks for taking the time to answer anyways