Skip to content

Instantly share code, notes, and snippets.

@yann-yinn
Created September 26, 2018 14:10
Show Gist options
  • Save yann-yinn/80604ad2b240d9098e4440c04feb10da to your computer and use it in GitHub Desktop.
Save yann-yinn/80604ad2b240d9098e4440c04feb10da to your computer and use it in GitHub Desktop.
rust config mod
extern crate toml;
use std::fs::File;
use std::io::Read;
pub fn get() -> Config {
let config = parse_from_file("src/config.toml");
config
}
pub fn parse_from_file(file: &str) -> Config {
let config_str = get_file_content(&file);
let config: Config = toml::from_str(&config_str).unwrap();
config
}
fn get_file_content(name: &str) -> String {
let mut file: File = File::open(name).expect("Erreur à l'ouverture du fichier");
let mut contents = String::new();
file
.read_to_string(&mut contents)
.expect("Erreur en listant le fichier de configuration");
contents
}
#[derive(Debug, Deserialize)]
pub struct Config {
pub database: DatabaseConfig,
}
#[derive(Debug, Deserialize)]
pub struct DatabaseConfig {
pub uri: String,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment