1use std::env::VarError;
5use std::net::AddrParseError;
6use std::num::ParseIntError;
7use std::str::ParseBoolError;
8
9use thiserror::Error;
10
11mod handler;
12mod server;
13
14pub use handler::*;
15pub use server::*;
16
17#[derive(Default)]
18pub struct Settings {
19 pub server: ServerSettings,
20 pub handler: HandlerSettings,
21}
22
23#[derive(Debug, Error)]
24pub enum SettingsFromEnvError {
25 #[error("VarError while reading `{0}`: {1}")]
26 VarError(&'static str, VarError),
27
28 #[error("Bad file path: {0}")]
29 BadFilePath(std::io::Error),
30
31 #[error("Bad bind address: {0}")]
32 BadAddr(AddrParseError),
33
34 #[error("Bad port: {0}")]
35 BadPort(ParseIntError),
36
37 #[error("Root is not a directory")]
38 RootNotADirectory,
39
40 #[error("Bad allow hidden value: {0}")]
41 BadAllowHidden(ParseBoolError),
42}
43
44type Result<T, E = SettingsFromEnvError> = std::result::Result<T, E>;
45
46impl Settings {
47 pub fn from_env() -> Result<Self> {
48 Ok(Self {
49 server: ServerSettings::from_env()?,
50 handler: HandlerSettings::from_env()?,
51 })
52 }
53}
54
55fn env(key: &'static str) -> Result<Option<String>> {
56 match std::env::var(key) {
57 Ok(s) => Ok(Some(s)),
58 Err(VarError::NotPresent) => Ok(None),
59 Err(e) => Err(super::SettingsFromEnvError::VarError(key, e)),
60 }
61}