Skip to content
Snippets Groups Projects
Unverified Commit 22602d57 authored by Louis's avatar Louis :fire:
Browse files

Add relevant command line options as config values, load config from file with fallback

parent bbc563d6
No related branches found
No related tags found
No related merge requests found
pub use std::path::PathBuf;
pub use std::env::current_dir;
pub const USAGE: &'static str = " pub const USAGE: &'static str = "
Static file swerver and api mocker for local development. Static file swerver and api mocker for local development.
For full documentation, visit https://swerve.louiscap.io. For full documentation, visit https://swerve.louiscap.io.
...@@ -39,4 +42,16 @@ pub struct Args { ...@@ -39,4 +42,16 @@ pub struct Args {
pub flag_upload: bool, pub flag_upload: bool,
pub flag_upload_path: Option<String>, pub flag_upload_path: Option<String>,
pub flag_license: bool, 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
...@@ -3,26 +3,119 @@ use std::convert::AsRef; ...@@ -3,26 +3,119 @@ use std::convert::AsRef;
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::fs::File; 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 { pub enum HandlerMethod {
Log, Log,
File, 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 { pub struct SwerveConfig {
#[serde(default)]
pub field_handling: HandlerMethod, pub field_handling: HandlerMethod,
#[serde(default)]
pub file_handling: HandlerMethod, pub file_handling: HandlerMethod,
#[serde(default)]
pub server: ServerOptions,
} }
impl SwerveConfig { #[derive(Deserialize, Debug, Clone)]
pub fn from_file<P>(path: P) -> io::Result<SwerveConfig> where P: AsRef<Path> { pub struct ServerOptions {
// let mut file = File::open(path)?; #[serde(default="get_default_port")]
// let mut buffer = String::new(); pub port: u16,
// file.read_to_string(buffer)?; #[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, field_handling: HandlerMethod::Log,
file_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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment