Improve regression resistance with named consts

This commit is contained in:
Magnus Hoff 2017-10-30 14:40:49 +01:00
parent 7d431b46fe
commit b6d3cbf2c6

View file

@ -36,41 +36,45 @@ mod state;
mod web; mod web;
mod wiki_lookup; mod wiki_lookup;
const DATABASE: &str = "DATABASE";
const TRUST_IDENTITY: &str = "trust-identity";
const PORT: &str = "port";
fn args<'a>() -> clap::ArgMatches<'a> { fn args<'a>() -> clap::ArgMatches<'a> {
use clap::{App, Arg}; use clap::{App, Arg};
App::new("sausagewiki") App::new("sausagewiki")
.about("A wiki engine") .about("A wiki engine")
.arg(Arg::with_name("DATABASE") .arg(Arg::with_name(DATABASE)
.help("Sets the database file to use") .help("Sets the database file to use")
.required(true)) .required(true))
.arg(Arg::with_name("port") .arg(Arg::with_name(PORT)
.help("Sets the listening port") .help("Sets the listening port")
.short("p") .short("p")
.long("port") .long(PORT)
.validator(|x| match x.parse::<u16>() { .validator(|x| match x.parse::<u16>() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err("Must be an integer in the range [0, 65535]".to_owned()) Err(_) => Err("Must be an integer in the range [0, 65535]".to_owned())
}) })
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("trust-identity") .arg(Arg::with_name(TRUST_IDENTITY)
.help("Trust the value in the X-Identity header to be an \ .help("Trust the value in the X-Identity header to be an \
authenticated username. This only makes sense when Sausagewiki \ authenticated username. This only makes sense when Sausagewiki \
runs behind a reverse proxy which sets this header.") runs behind a reverse proxy which sets this header.")
.long("trust-identity")) .long(TRUST_IDENTITY))
.get_matches() .get_matches()
} }
fn core_main() -> Result<(), Box<std::error::Error>> { fn core_main() -> Result<(), Box<std::error::Error>> {
let args = args(); let args = args();
let db_file = args.value_of("DATABASE").expect("Guaranteed by clap").to_owned(); let db_file = args.value_of(DATABASE).expect("Guaranteed by clap").to_owned();
let bind_host = "127.0.0.1".parse().unwrap(); let bind_host = "127.0.0.1".parse().unwrap();
let bind_port = args.value_of("port") let bind_port = args.value_of(PORT)
.map(|p| p.parse().expect("Guaranteed by validator")) .map(|p| p.parse().expect("Guaranteed by validator"))
.unwrap_or(8080); .unwrap_or(8080);
let trust_identity = args.is_present("trust-identity"); let trust_identity = args.is_present(TRUST_IDENTITY);
let db_pool = db::create_pool(db_file)?; let db_pool = db::create_pool(db_file)?;
let cpu_pool = futures_cpupool::CpuPool::new_num_cpus(); let cpu_pool = futures_cpupool::CpuPool::new_num_cpus();