Skip to content

Commit

Permalink
Split imported events into multiple files.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Oct 24, 2023
1 parent 83d84a0 commit 9feea7b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
37 changes: 26 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ use axum::{
Router,
};
use eyre::Report;
use github::choose_file_for_event;
use log::info;
use schemars::schema_for;
use std::{
collections::HashMap,
env,
fs::write,
process::exit,
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -120,32 +123,44 @@ async fn sort(path: &str) -> Result<(), Report> {

async fn import_balbende() -> Result<(), Report> {
let events = folkbalbende::import_events().await?;
print_events(&events)
write_events_to_files(events)

Check warning on line 126 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L126

Added line #L126 was not covered by tests
}

async fn import_cdss() -> Result<(), Report> {
let events = cdss::import_events().await?;
print_events(&events)
write_events_to_files(events)

Check warning on line 131 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L131

Added line #L131 was not covered by tests
}

async fn import_webfeet() -> Result<(), Report> {
let events = webfeet::import_events().await?;
print_events(&events)
write_events_to_files(events)

Check warning on line 136 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L136

Added line #L136 was not covered by tests
}

async fn import_balfolknl() -> Result<(), Report> {
let events = balfolknl::import_events().await?;
print_events(&events)
write_events_to_files(events)

Check warning on line 141 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L141

Added line #L141 was not covered by tests
}

fn print_events(events: &Events) -> Result<(), Report> {
let yaml = serde_yaml::to_string(events)?;
let yaml = yaml.replacen(
"---",
"# yaml-language-server: $schema=../../events_schema.json",
1,
);
print!("{}", yaml);
print!("{}", events.as_yaml()?);
Ok(())
}

Check warning on line 147 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L145-L147

Added lines #L145 - L147 were not covered by tests

fn write_events_to_files(events: Events) -> Result<(), Report> {
let empty_events = Events::default();
let mut events_by_file: HashMap<String, Events> = HashMap::new();
for event in events.events {
let chosen_file = choose_file_for_event(&empty_events, &event).unwrap();
events_by_file
.entry(chosen_file)
.or_default()
.events
.push(event);
}
for (filename, events) in events_by_file {
info!("Writing {} events to {}", events.events.len(), filename);
write(filename, events.as_yaml()?)?;

Check warning on line 162 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L149-L162

Added lines #L149 - L162 were not covered by tests
}
Ok(())
}

Expand Down
12 changes: 11 additions & 1 deletion src/model/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::{
path::Path,
};

#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Events {
pub events: Vec<Event>,
Expand Down Expand Up @@ -106,6 +106,16 @@ impl Events {
Ok(events)
}

/// Converts the events to a YAML string suitable for writing to a file.
pub fn as_yaml(&self) -> Result<String, Report> {
let yaml = serde_yaml::to_string(self)?;
Ok(yaml.replacen(
"---",
"# yaml-language-server: $schema=../../events_schema.json",
1,
))
}

Check warning on line 117 in src/model/events.rs

View check run for this annotation

Codecov / codecov/patch

src/model/events.rs#L110-L117

Added lines #L110 - L117 were not covered by tests

/// Get all events matching the given filters.
pub fn matching(&self, filters: &Filters) -> Vec<&Event> {
let now = Utc::now();
Expand Down

0 comments on commit 9feea7b

Please sign in to comment.