-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.rs
39 lines (36 loc) · 1.09 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::cli::{ADDRESS, PORT, ROOT_DIR, SILENT};
use clap::App;
use std::env::current_dir;
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::str::FromStr;
/// Configuration for the HTTP/S Server
#[derive(Debug)]
pub struct Config {
pub address: IpAddr,
pub port: u16,
pub socket_address: SocketAddr,
pub root_dir: PathBuf,
pub silent: bool,
}
impl From<App<'static, 'static>> for Config {
fn from(app: App) -> Self {
let matches = app.get_matches();
let address = IpAddr::from_str(matches.value_of(ADDRESS.1).unwrap()).unwrap();
let port = matches.value_of(PORT.1).unwrap().parse::<u16>().unwrap();
let socket_address = SocketAddr::new(address, port);
let root_dir = if let Some(root_dir) = matches.value_of(ROOT_DIR.1) {
PathBuf::from_str(root_dir).unwrap()
} else {
current_dir().unwrap()
};
let silent = matches.is_present(SILENT.1);
Self {
address,
port,
socket_address,
root_dir,
silent,
}
}
}