Skip to content

Commit

Permalink
fix clippy and rustfmt errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jlefkoff committed Dec 18, 2024
1 parent b8fa15f commit 90db8b0
Showing 1 changed file with 62 additions and 66 deletions.
128 changes: 62 additions & 66 deletions vzdv-site/src/endpoints/facility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use axum::{
Form, Router,
};
use chrono::{DateTime, Months, Utc};
use indexmap::IndexMap;
use itertools::Itertools;
use log::{error, warn};
use minijinja::context;
Expand All @@ -21,7 +22,6 @@ use std::{
sync::Arc,
};
use tower_sessions::Session;
use indexmap::IndexMap;
use vzdv::{
config::Config,
determine_staff_positions,
Expand All @@ -39,6 +39,8 @@ struct StaffPosition {
description: &'static str,
}

type ParsedAlias = Vec<(String, Vec<(String, Vec<String>)>)>;

fn generate_staff_outline(config: &Config) -> HashMap<&'static str, StaffPosition> {
let email_domain = &config.staff.email_domain;
HashMap::from([
Expand Down Expand Up @@ -398,75 +400,69 @@ async fn page_resources(
Ok(Html(rendered))
}

pub async fn fetch_and_parse_alias_file() -> Result<ParsedAlias, reqwest::Error> {
let url = "https://data-api.vnas.vatsim.net/Files/Aliases/ZDV.txt";
let response = reqwest::get(url).await?.text().await?;

let mut parsed_data: IndexMap<String, IndexMap<String, Vec<String>>> = IndexMap::new();
let mut current_h1 = String::new();
let mut current_h2 = String::new();

for line in response.lines() {
if line.starts_with(";;;;") {
// New Heading 1
current_h1 = line.strip_prefix(";;;;").unwrap_or(line).trim().to_string();
parsed_data.entry(current_h1.clone()).or_default();
current_h2 = String::new(); // Reset H2
} else if line.starts_with(";;;") {
// New Heading 2
current_h2 = line.strip_prefix(";;;").unwrap_or(line).trim().to_string();
parsed_data
.entry(current_h1.clone())
.or_default()
.entry(current_h2.clone())
.or_default();
} else if line.starts_with('.') {
// Command under current H1 or H2
if !current_h1.is_empty() {
if !current_h2.is_empty() {
parsed_data
.entry(current_h1.clone())
.or_default()
.entry(current_h2.clone())
.or_default()
.push(line.trim().to_string());
} else {
// Command directly under H1
parsed_data
.entry(current_h1.clone())
.or_default()
.entry("__root__".to_string())
.or_default()
.push(line.trim().to_string());
}
}
}
}

// Convert IndexMap to Vec for Jinja compatibility
let parsed_vec: ParsedAlias = parsed_data
.into_iter()
.map(|(h1, h2_map)| {
let h2_vec = h2_map.into_iter().collect();
(h1, h2_vec)
})
.collect();

pub async fn fetch_and_parse_alias_file() -> Result<Vec<(String, Vec<(String, Vec<String>)>)>, reqwest::Error> {
let url = "https://data-api.vnas.vatsim.net/Files/Aliases/ZDV.txt";
let response = reqwest::get(url).await?.text().await?;

let mut parsed_data: IndexMap<String, IndexMap<String, Vec<String>>> = IndexMap::new();
let mut current_h1 = String::new();
let mut current_h2 = String::new();

for line in response.lines() {
if line.starts_with(";;;;") {
// New Heading 1
current_h1 = line[4..].trim().to_string();
parsed_data.entry(current_h1.clone()).or_default();
current_h2 = String::new(); // Reset H2
} else if line.starts_with(";;;") {
// New Heading 2
current_h2 = line[3..].trim().to_string();
parsed_data
.entry(current_h1.clone())
.or_default()
.entry(current_h2.clone())
.or_default();
} else if line.starts_with('.') {
// Command under current H1 or H2
if !current_h1.is_empty() {
if !current_h2.is_empty() {
parsed_data
.entry(current_h1.clone())
.or_default()
.entry(current_h2.clone())
.or_default()
.push(line.trim().to_string());
} else {
// Command directly under H1
parsed_data
.entry(current_h1.clone())
.or_default()
.entry("__root__".to_string())
.or_default()
.push(line.trim().to_string());
}
}
}
}

// Convert IndexMap to Vec for Jinja compatibility
let parsed_vec: Vec<(String, Vec<(String, Vec<String>)>)> = parsed_data
.into_iter()
.map(|(h1, h2_map)| {
let h2_vec = h2_map
.into_iter()
.map(|(h2, commands)| (h2, commands))
.collect();
(h1, h2_vec)
})
.collect();

Ok(parsed_vec)
Ok(parsed_vec)
}


/// View Alias commands for the facility. (Polled from the vNAS API)
async fn alias_ref(
State(state): State<Arc<AppState>>) -> Result<Html<String>, AppError> {
let template = state.templates.get_template("facility/aliasref.jinja")?;
let alias_ref = fetch_and_parse_alias_file().await?;
let rendered = template.render(context! { alias_ref })?;
Ok(Html(rendered))
async fn alias_ref(State(state): State<Arc<AppState>>) -> Result<Html<String>, AppError> {
let template = state.templates.get_template("facility/aliasref.jinja")?;
let alias_ref = fetch_and_parse_alias_file().await?;
let rendered = template.render(context! { alias_ref })?;
Ok(Html(rendered))
}

/// Check visitor requirements and submit an application.
Expand Down

0 comments on commit 90db8b0

Please sign in to comment.