webserver_config/
config.rs

1pub struct Config {
2	pub content: Vec<ConfigItem>
3}
4
5pub enum ConfigItem {
6	Directive(Directive),
7	Block(Block)
8}
9
10impl Config {
11
12	pub fn new() -> Self {
13		Self {content: vec![]}
14	}
15
16	pub fn add_directive(&mut self, directive: Directive) {
17		self.content.push(ConfigItem::Directive(directive));
18	}
19
20	pub fn add_block(&mut self, block: Block) {
21		self.content.push(ConfigItem::Block(block));
22	}
23
24	pub fn add(&mut self, name: &str, value: &str) {
25		self.add_directive(Directive {
26			name: name.to_string(),
27			values: vec![value.to_string()]
28		});
29	}
30
31	pub fn add_multi<T: Into<String>>(&mut self, name: &str, values: Vec<T>) {
32		self.add_directive(Directive {
33			name: name.to_string(),
34			values: values.into_iter().map(Into::into).collect()
35		});
36	}
37
38}
39
40pub struct Block {
41	pub name: String,
42	pub params: Vec<String>,
43	pub config: Config
44}
45
46impl Block {
47
48	pub fn new(name: &str, param: &str) -> Self {
49		Self::new_multi(name, vec![param])
50	}
51
52	pub fn new_multi<T: Into<String>>(name: &str, params: Vec<T>) -> Self {
53		let conf = Config::new();
54		Self::from(name, params, conf)
55	}
56
57	pub fn new_simple(name: &str) -> Self {
58		Self::new_multi(name, Vec::<String>::new())
59	}
60
61	pub fn from<T: Into<String>>(name: &str, params: Vec<T>, conf: Config) -> Self {
62		Self {
63			name: name.to_string(),
64			params: params.into_iter().map(Into::into).collect(),
65			config: conf
66		}
67	}
68
69	pub fn setup<F>(mut self, mut f: F) -> Self where F: FnMut(&mut Config) {
70		f(&mut self.config);
71		self
72	}
73
74	pub fn modify_config<F>(&mut self, mut f: F) where F: FnMut(&mut Config) {
75		f(&mut self.config)
76	}
77
78}
79
80pub struct Directive {
81	pub name: String,
82	pub values: Vec<String>
83}