Skip to content

Instantly share code, notes, and snippets.

@RalfJung
Created May 15, 2018 11:53
Show Gist options
  • Save RalfJung/52334b640acb76eb84057a7ec3c231a5 to your computer and use it in GitHub Desktop.
Save RalfJung/52334b640acb76eb84057a7ec3c231a5 to your computer and use it in GitHub Desktop.
// config.rs
use rocket;
use toml;
use serde::Deserialize;
use serde::de::IntoDeserializer;
#[derive(Serialize, Deserialize)]
pub struct Config {
pub instance_name: String,
pub root_url: String,
pub email_from: String,
}
impl Config {
/// Create a `Config` instance from a rocket config table
pub fn new(table: &rocket::config::Table) -> Self {
let val = toml::value::Value::Table(table.clone());
Self::deserialize(val.into_deserializer())
.expect("MY_APP config table has missing or extra value")
}
}
// in main
rocket::ignite()
.attach(rocket::fairing::AdHoc::on_attach(|rocket| {
let config = {
let config_table = rocket.config().get_table("MY_APP")
.expect("MY_APP table in Rocket.toml missing or not a table");
config::Config::new(config_table)
};
Ok(rocket.manage(config))
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment