summaryrefslogtreecommitdiffstats
path: root/config/src/lib.rs
diff options
context:
space:
mode:
authorFilip Wandzio <contact@philw.dev>2025-12-28 00:45:12 +0100
committerFilip Wandzio <contact@philw.dev>2025-12-28 00:45:12 +0100
commit7995282f65183b0a615c224a3ea13eeb10a1e828 (patch)
tree84705b645f3cc799b8d6cf8af55b67dbc045e82a /config/src/lib.rs
downloadpdat-7995282f65183b0a615c224a3ea13eeb10a1e828.tar.gz
pdat-7995282f65183b0a615c224a3ea13eeb10a1e828.zip
Kind of Initial CommentHEADmaster
Implement basic way of testing various http methods via simple cli interface. Also write basic config file kind of parser.
Diffstat (limited to '')
-rw-r--r--config/src/lib.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/config/src/lib.rs b/config/src/lib.rs
new file mode 100644
index 0000000..d83b615
--- /dev/null
+++ b/config/src/lib.rs
@@ -0,0 +1,94 @@
1use serde::Deserialize;
2use std::fs;
3use std::path::Path;
4
5#[derive(Debug, Deserialize)]
6pub struct DefaultConfig {
7 pub headers: Option<Vec<String>>,
8 pub indent: Option<usize>,
9}
10
11#[derive(Debug, Deserialize)]
12pub struct Config {
13 pub default: Option<DefaultConfig>,
14}
15
16impl Config {
17 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
18 let content = fs::read_to_string(path)?;
19 let config: Config = toml::from_str(&content)?;
20 Ok(config)
21 }
22
23 pub fn headers(&self) -> Vec<String> {
24 self.default
25 .as_ref()
26 .and_then(|d| d.headers.clone())
27 .unwrap_or_default()
28 }
29
30 pub fn indent(&self) -> usize {
31 self.default.as_ref().and_then(|d| d.indent).unwrap_or(2)
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38 use std::env;
39 use std::fs::write;
40
41 #[test]
42 fn test_from_file_with_realistic_headers() {
43 let toml_content = r#"
44 [default]
45 headers = [
46 "Authorization: Bearer YOUR_TOKEN",
47 "Content-Type: application/json"
48 ]
49 indent = 4
50 "#;
51
52 let tmp_path = std::env::temp_dir().join("test_real_config.toml");
53 std::fs::write(&tmp_path, toml_content).expect("Failed to write TOML file");
54
55 let config = Config::from_file(&tmp_path).expect("Failed to read config from file");
56
57 assert_eq!(
58 config.headers(),
59 vec![
60 "Authorization: Bearer YOUR_TOKEN",
61 "Content-Type: application/json"
62 ]
63 );
64 assert_eq!(config.indent(), 4);
65 }
66 #[test]
67 fn test_missing_values_defaults() {
68 let toml_content = r#""#;
69
70 let config: Config = toml::from_str(toml_content).expect("Failed to parse empty TOML");
71
72 assert_eq!(config.headers(), Vec::<String>::new());
73 assert_eq!(config.indent(), 2);
74 }
75
76 #[test]
77 fn test_from_file_function() {
78 let toml_content = r#"
79 [default]
80 headers = ["User", "Email"]
81 indent = 3
82 "#;
83
84 let tmp_dir = env::temp_dir();
85 let file_path = tmp_dir.join("test_config.toml");
86
87 write(&file_path, toml_content).expect("Failed to write temp TOML");
88
89 let config = Config::from_file(&file_path).expect("Failed to read config");
90
91 assert_eq!(config.headers(), vec!["User", "Email"]);
92 assert_eq!(config.indent(), 3);
93 }
94}