From 88419cbf9749a32c06069319e0e479b940bb5ee4 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Thu, 1 May 2025 09:54:23 +1000 Subject: [PATCH] Refactor configuration to have sources in their own section --- src/main.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index a4b72ef..334b74c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,15 +27,15 @@ mod reddit_client; mod schema; #[derive(Debug, Serialize, Deserialize)] -struct SectionConfig { +struct SourceConfig { username: String, title_filter: Option, } #[derive(Debug, Serialize, Deserialize)] struct Config { - #[serde(flatten)] - sections: HashMap, + #[serde(default)] + sources: HashMap, } #[derive(Debug)] @@ -152,13 +152,13 @@ async fn main() -> Result<()> { .extract() .wrap_err_with(|| "Invalid configuration or insufficient command line arguments")?; - if conf.sections.is_empty() { - return Err(eyre!("No configuration sections found. Please add at least one section to your configuration file.").into()); + if conf.sources.is_empty() { + return Err(eyre!("No sources found in configuration. Please add at least one source to your configuration file.").into()); } let mut unique_usernames = HashSet::new(); - for (_, section_config) in &conf.sections { - unique_usernames.insert(section_config.username.clone()); + for (_, source_config) in &conf.sources { + unique_usernames.insert(source_config.username.clone()); } let reddit_client = RedditClient::new(); @@ -168,11 +168,11 @@ async fn main() -> Result<()> { user_posts.insert_many(username, submissions); } - for (section_name, section_config) in conf.sections { - println!("\nProcessing section [{}]", section_name); + for (source_name, source_config) in conf.sources { + println!("\nProcessing source [{}]", source_name); - let username = section_config.username.clone(); - let title_filter = match section_config.title_filter { + let username = source_config.username.clone(); + let title_filter = match source_config.title_filter { Some(filter) => Some( Regex::new(filter.as_str()) .context(format!("Invalid regex pattern: {}", filter))?,