Refactor configuration to have sources in their own section
This commit is contained in:
parent
4de127f2d2
commit
88419cbf97
1 changed files with 11 additions and 11 deletions
22
src/main.rs
22
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<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Config {
|
||||
#[serde(flatten)]
|
||||
sections: HashMap<String, SectionConfig>,
|
||||
#[serde(default)]
|
||||
sources: HashMap<String, SourceConfig>,
|
||||
}
|
||||
|
||||
#[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))?,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue