Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2017 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/476064102dbab8f6658a055786654448 to your computer and use it in GitHub Desktop.
Save anonymous/476064102dbab8f6658a055786654448 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
#[macro_use]
extern crate serde_derive;
extern crate toml;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config<'a> {
target: &'a str,
email: &'a str,
passwd: &'a str,
#[serde(default)]
smtp: &'a str,
}
fn main() {
let mut file = File::open("foo").expect("Failed to open configuration file");
let mut cfgstr = String::new();
file.read_to_string(&mut cfgstr).expect(
"Failed to read the configuration file",
);
let mut config: Config = toml::from_str(&cfgstr).expect("Failed to load configuration");
// TODO add some more robust default value handling
if config.smtp == "" {
let mut s = config.email.split('@').skip(1);
let domain = s.next().expect("Invalid e-mail format: no domain");
let smtp = "smtp.".to_owned() + domain;
println!("Assuming smtp server: {}", smtp);
config.smtp = &smtp;
}
// do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment