Comments (2)
This crate does not support such functionality. You should use a templating library like handlebars for that!
from config-rs.
Thanks for the suggestion, I've been able to rewrite it to do that
use std::collections::HashMap;
use config::{Config, ConfigError, File, FileFormat};
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct AppConfiguration {
database_configuration: DatabaseConfiguration,
}
impl AppConfiguration {
pub fn get_configuration() -> Result<AppConfiguration, ConfigError> {
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
let configuration_path = base_path.join("configurations");
let env_string = std::fs::read_to_string(".env").unwrap_or("".to_string());
let environment_data: HashMap<String, String> = Config::builder()
.add_source(File::from_str(&env_string, FileFormat::Ini))
.build()?
.try_deserialize()?;
let handlebars = handlebars::Handlebars::new();
let template_string = std::fs::read_to_string(configuration_path.join("base.yaml"))
.expect("Unable to open configuration file");
let rendered = handlebars
.render_template(&template_string, &environment_data)
.expect("Unable to render template");
let builder = Config::builder()
.add_source(File::from_str(rendered.as_str(), FileFormat::Yaml).required(true));
return builder.build()?.try_deserialize();
}
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct DatabaseConfiguration {
postgres_host: String,
postgres_port: u32,
postgres_user: String,
postgres_password: String,
postgres_db: String,
}
configuration/base.yaml
database_configuration:
postgres_host: {{postgres_host}}
postgres_port: {{postgres_port}}
postgres_user: {{postgres_user}}
postgres_password: {{postgres_password}}
postgres_db: {{postgres_db}}
.env
postgres_host=127.0.0.1
postgres_port=6500
postgres_user=admin
postgres_password=password123
postgres_db=rust_jwt_authentication
main.rs
let app_configurations = AppConfiguration::get_configuration().expect("Unable to build configuration");
println!("{:?}", app_configurations);
from config-rs.
Related Issues (20)
- Enum variant aliases don't work HOT 1
- failed type conversion in .ini file for serde flatten HOT 10
- Add a `cargo check-external-types` lint in CI
- Implement serde::Serialize for Config HOT 3
- New release/sync from master HOT 5
- Remove lazy_static HOT 1
- Nom is included in public API HOT 6
- bug: Using the `serde` attribute `rename` fails since `0.14.0` with a `missing field` error HOT 4
- 0.14 pulls in CC0 license HOT 2
- bug: config `0.14.0` only supports lowercase fields (regression since `0.13.4`) HOT 8
- ux: Lack of key and origin in ConfigError::Message leads to uncertain sources from serde error messages HOT 1
- using Environment source to get List of struct HOT 1
- Very old version of async_trait crate is used HOT 2
- Incorrect description of separator usage in Environment::with_prefix()
- ron Deserialize fails with enum with named fields HOT 2
- Unreachable code fault at config-0.14.0/src/file/format/yaml.rs:53:26 HOT 2
- rust-ini version mismatch in github code and crates.io HOT 3
- Maintainer wanted HOT 10
- cargo-audit reports that `yaml-rust` is unmaintained HOT 7
- yaml-rust is unmaintained - replace with yaml-rust2? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from config-rs.