Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.