From 7995282f65183b0a615c224a3ea13eeb10a1e828 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Sun, 28 Dec 2025 00:45:12 +0100 Subject: Kind of Initial Comment Implement basic way of testing various http methods via simple cli interface. Also write basic config file kind of parser. --- config/src/lib.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 config/src/lib.rs (limited to 'config/src/lib.rs') 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 @@ +use serde::Deserialize; +use std::fs; +use std::path::Path; + +#[derive(Debug, Deserialize)] +pub struct DefaultConfig { + pub headers: Option>, + pub indent: Option, +} + +#[derive(Debug, Deserialize)] +pub struct Config { + pub default: Option, +} + +impl Config { + pub fn from_file>(path: P) -> Result> { + let content = fs::read_to_string(path)?; + let config: Config = toml::from_str(&content)?; + Ok(config) + } + + pub fn headers(&self) -> Vec { + self.default + .as_ref() + .and_then(|d| d.headers.clone()) + .unwrap_or_default() + } + + pub fn indent(&self) -> usize { + self.default.as_ref().and_then(|d| d.indent).unwrap_or(2) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::env; + use std::fs::write; + + #[test] + fn test_from_file_with_realistic_headers() { + let toml_content = r#" + [default] + headers = [ + "Authorization: Bearer YOUR_TOKEN", + "Content-Type: application/json" + ] + indent = 4 + "#; + + let tmp_path = std::env::temp_dir().join("test_real_config.toml"); + std::fs::write(&tmp_path, toml_content).expect("Failed to write TOML file"); + + let config = Config::from_file(&tmp_path).expect("Failed to read config from file"); + + assert_eq!( + config.headers(), + vec![ + "Authorization: Bearer YOUR_TOKEN", + "Content-Type: application/json" + ] + ); + assert_eq!(config.indent(), 4); + } + #[test] + fn test_missing_values_defaults() { + let toml_content = r#""#; + + let config: Config = toml::from_str(toml_content).expect("Failed to parse empty TOML"); + + assert_eq!(config.headers(), Vec::::new()); + assert_eq!(config.indent(), 2); + } + + #[test] + fn test_from_file_function() { + let toml_content = r#" + [default] + headers = ["User", "Email"] + indent = 3 + "#; + + let tmp_dir = env::temp_dir(); + let file_path = tmp_dir.join("test_config.toml"); + + write(&file_path, toml_content).expect("Failed to write temp TOML"); + + let config = Config::from_file(&file_path).expect("Failed to read config"); + + assert_eq!(config.headers(), vec!["User", "Email"]); + assert_eq!(config.indent(), 3); + } +} -- cgit v1.2.3