diff options
author | Adam T. Carpenter <atc@53hor.net> | 2021-04-15 20:03:01 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2021-04-15 20:03:01 -0400 |
commit | d83fe68ed51016bbb87d83aa512ef8b9d3f0780e (patch) | |
tree | 05f93d34464692c706288f7bb3497f55e4539b09 /src/config.rs | |
parent | 93d9a3b8d6984a8f113a30fea523607162e71589 (diff) | |
download | twinh-d83fe68ed51016bbb87d83aa512ef8b9d3f0780e.tar.xz twinh-d83fe68ed51016bbb87d83aa512ef8b9d3f0780e.zip |
split config into modules that actually need it, started parsing args
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index da65385..0000000 --- a/src/config.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::TwinHError; -use once_cell::sync::Lazy; -use std::{env, error::Error, net::IpAddr, net::Ipv4Addr, net::SocketAddr}; - -pub static CONFIG_INSTANCE: Lazy<Config> = Lazy::new(|| match Config::new() { - Ok(c) => c, - Err(e) => panic!("twinh: config error: {}", e), -}); - -#[derive(Clone, Debug)] -pub struct Config { - pub bind_addr: SocketAddr, - pub db_path: String, -} - -impl Default for Config { - fn default() -> Self { - Self { - bind_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5353), - db_path: String::from("/var/db/twinh"), - } - } -} - -impl Config { - fn new() -> Result<Self, Box<dyn Error>> { - let mut args = env::args().skip(1); - - let db_path = args - .next() - .ok_or_else(|| TwinHError(String::from("database directory not provided")))?; - - let mut config = Config { - bind_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5353), - db_path, - }; - - while let Some(arg) = args.next() { - match arg.as_str() { - "--addr" => { - let addr = args.next().unwrap_or_default().parse()?; - config.bind_addr.set_ip(addr); - } - "--port" => { - let port = args.next().unwrap_or_default().parse()?; - config.bind_addr.set_port(port); - } - _ => {} - }; - } - - Ok(config) - } -} |