From 22602d5783b62dd24162a4d7e24bb936f0540cce Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Fri, 13 Apr 2018 14:55:18 +0100 Subject: [PATCH] Add relevant command line options as config values, load config from file with fallback --- src/cli/cli.rs | 15 ++++++ src/cli/config_file.rs | 107 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 7 deletions(-) diff --git a/src/cli/cli.rs b/src/cli/cli.rs index 65dd570..578dc8c 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -1,3 +1,6 @@ +pub use std::path::PathBuf; +pub use std::env::current_dir; + pub const USAGE: &'static str = " Static file swerver and api mocker for local development. For full documentation, visit https://swerve.louiscap.io. @@ -39,4 +42,16 @@ pub struct Args { pub flag_upload: bool, pub flag_upload_path: Option<String>, pub flag_license: bool, +} + +impl Args { + pub fn get_dir(&self) -> PathBuf { + let dir_flag = self.flag_dir.clone(); + PathBuf::from( + dir_flag.unwrap_or( + current_dir().unwrap_or( + PathBuf::from("") + ).to_string_lossy().into_owned()) + ) + } } \ No newline at end of file diff --git a/src/cli/config_file.rs b/src/cli/config_file.rs index 538dac1..09e088f 100644 --- a/src/cli/config_file.rs +++ b/src/cli/config_file.rs @@ -3,26 +3,119 @@ use std::convert::AsRef; use std::io::prelude::*; use std::io; use std::fs::File; +use std::io::BufReader; +use std::default::Default; +use serde::{Deserialize, Deserializer, de::{self, Error}}; +use std::fmt; +use serde_yaml as yaml; +#[derive(Debug, Copy, Clone)] pub enum HandlerMethod { Log, File, } +struct HandlerMethodVisitor; +impl <'de>de::Visitor<'de> for HandlerMethodVisitor { + type Value = HandlerMethod; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a string with value 'Log' or 'File'") + } + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where + E: de::Error, { + match v { + "Log" => Ok(HandlerMethod::Log), + "File" => Ok(HandlerMethod::File), + _ => Err(de::Error::unknown_variant(v, &["Log", "File"])), + } + } +} + +impl <'de>Deserialize<'de> for HandlerMethod { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where + D: Deserializer<'de> { + deserializer.deserialize_str(HandlerMethodVisitor) + } +} + +impl Default for HandlerMethod { + fn default() -> Self { + HandlerMethod::Log + } +} + +#[derive(Deserialize, Debug, Clone)] pub struct SwerveConfig { + #[serde(default)] pub field_handling: HandlerMethod, + #[serde(default)] pub file_handling: HandlerMethod, + #[serde(default)] + pub server: ServerOptions, } -impl SwerveConfig { - pub fn from_file<P>(path: P) -> io::Result<SwerveConfig> where P: AsRef<Path> { -// let mut file = File::open(path)?; -// let mut buffer = String::new(); -// file.read_to_string(buffer)?; +#[derive(Deserialize, Debug, Clone)] +pub struct ServerOptions { + #[serde(default="get_default_port")] + pub port: u16, + #[serde(default="get_default_threads")] + pub threads: u16, + #[serde(default="get_default_address")] + pub address: String, + #[serde(default="get_default_quiet_attr")] + pub quiet: bool, + #[serde(default="get_default_index_attr")] + pub no_index: bool, +} + +fn get_default_port() -> u16 { 8200 } +fn get_default_threads() -> u16 { 32} +fn get_default_address() -> String { String::from("localhost") } +fn get_default_quiet_attr() -> bool { false } +fn get_default_index_attr() -> bool { false } - Ok(SwerveConfig { +impl Default for SwerveConfig { + fn default() -> Self { + SwerveConfig { field_handling: HandlerMethod::Log, file_handling: HandlerMethod::Log, - }) + server: ServerOptions::default(), + } + } +} + +impl Default for ServerOptions { + fn default() -> Self { + ServerOptions { + port: get_default_port(), + threads: get_default_threads(), + address: get_default_address(), + quiet: get_default_quiet_attr(), + no_index: get_default_index_attr(), + } + } +} + +impl SwerveConfig { + pub fn from_file<P>(path: P) -> io::Result<SwerveConfig> where P: AsRef<Path> { + let mut buffer = String::new(); + { + match File::open(path) { + Ok(mut file) => file.read_to_string(&mut buffer)?, + Err(e) => { + if e.kind() == io::ErrorKind::NotFound { + return Ok(SwerveConfig::default()); + } else { + return Err(e); + } + } + }; + } + + match yaml::from_str(&buffer) { + Ok(conf) => Ok(conf), + Err(er) => Err(io::Error::new(io::ErrorKind::InvalidData, format!("{}", er))), + } } } \ No newline at end of file -- GitLab