diff --git a/packages/exporter/src/main.rs b/packages/exporter/src/main.rs index 780fc833..2382b52c 100644 --- a/packages/exporter/src/main.rs +++ b/packages/exporter/src/main.rs @@ -26,7 +26,7 @@ async fn main() -> std::io::Result<()> { let mut server = HttpServer::new(move || { App::new() .service(actix_files::Files::new("/data", "./data/output").show_files_listing()) - .default_service(web::to(|| not_found())) + .default_service(web::to(not_found)) }); #[cfg(feature = "dev")] diff --git a/packages/exporter/src/setup.rs b/packages/exporter/src/setup.rs index c5eed4f9..47b50205 100644 --- a/packages/exporter/src/setup.rs +++ b/packages/exporter/src/setup.rs @@ -5,6 +5,7 @@ use globset::{GlobBuilder, GlobMatcher}; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use regex::Regex; use serde::Deserialize; +use std::borrow::Cow; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::{collections::HashSet, env}; @@ -28,7 +29,7 @@ struct Info { // dependencies: Vec, } -async fn get_info>(path: P) -> Result> { +async fn get_info(path: &Path) -> Result> { let contents = tokio::fs::read_to_string(path).await?; let p: Info = serde_json::from_str(&contents)?; Ok(p) @@ -41,7 +42,11 @@ fn get_download_url(buid_type: &str, version: &str, username: &str, token: &str) ) } -async fn make_img_pow2(path: &PathBuf, tmp_dir: &PathBuf) -> Result> { +#[allow(clippy::needless_lifetimes)] +async fn make_img_pow2<'a>( + path: &'a Path, + tmp_dir: &Path, +) -> Result, Box> { let (w, h) = image::image_dimensions(path)?; let w_log = f32::log2(w as f32); let h_log = f32::log2(h as f32); @@ -65,9 +70,9 @@ async fn make_img_pow2(path: &PathBuf, tmp_dir: &PathBuf) -> Result Result> { +async fn generate_locale(factorio_data: &Path) -> Result> { let matcher = GlobBuilder::new("**/*/locale/en/*.cfg") .literal_separator(true) .build()? @@ -138,7 +143,7 @@ async fn generate_locale(factorio_data: &PathBuf) -> Result Result<(), Box> { +pub async fn extract(data_dir: &Path, factorio_data: &Path) -> Result<(), Box> { let output_dir = data_dir.join("output"); let mod_dir = data_dir.join("factorio/mods/export-data"); let scenario_dir = mod_dir.join("scenarios/export-data"); @@ -178,7 +183,7 @@ pub async fn extract(data_dir: &PathBuf, factorio_data: &PathBuf) -> Result<(), use tokio::io::AsyncReadExt; let mut buffer = String::new(); metadata_file.read_to_string(&mut buffer).await?; - let obj: serde_yaml::Value = if buffer.len() == 0 { + let obj: serde_yaml::Value = if buffer.is_empty() { serde_yaml::Value::Mapping(serde_yaml::mapping::Mapping::new()) } else { serde_yaml::from_str(&buffer)? @@ -217,7 +222,7 @@ pub async fn extract(data_dir: &PathBuf, factorio_data: &PathBuf) -> Result<(), futures::future::try_join_all((0..num_cpus::get()).map(|_| { compress_next_img( file_paths.clone(), - tmp_dir.clone(), + &tmp_dir, progress.clone(), obj.clone(), metadata_file.clone(), @@ -233,7 +238,7 @@ pub async fn extract(data_dir: &PathBuf, factorio_data: &PathBuf) -> Result<(), Ok(()) } -async fn get_len_and_mtime(path: &PathBuf) -> Result<(u64, u64), Box> { +async fn get_len_and_mtime(path: &Path) -> Result<(u64, u64), Box> { let file = tokio::fs::File::open(path).await?; let metadata = file.metadata().await?; let mtime = metadata @@ -248,7 +253,7 @@ async fn get_len_and_mtime(path: &PathBuf) -> Result<(u64, u64), Box> #[async_recursion] async fn compress_next_img( file_paths: Arc>>, - tmp_dir: PathBuf, + tmp_dir: &Path, progress: ProgressBar, obj: Arc>, metadata_file: Arc>, @@ -261,7 +266,7 @@ async fn compress_next_img( let same = { obj.lock().unwrap()[key] == new_val }; if !same { - let path = make_img_pow2(&in_path, &tmp_dir).await?; + let path = make_img_pow2(&in_path, tmp_dir).await?; tokio::fs::create_dir_all(out_path.parent().unwrap()).await?; @@ -300,8 +305,8 @@ async fn compress_next_img( // TODO: look into using https://wiki.factorio.com/Download_API pub async fn download_factorio( - data_dir: &PathBuf, - factorio_data: &PathBuf, + data_dir: &Path, + factorio_data: &Path, factorio_version: &str, ) -> Result<(), Box> { let username = get_env_var!("FACTORIO_USERNAME")?; @@ -309,7 +314,7 @@ pub async fn download_factorio( let info_path = factorio_data.join("base/info.json"); - let same_version = get_info(info_path) + let same_version = get_info(&info_path) .await .map(|info| info.version == factorio_version) .unwrap_or(false); @@ -341,7 +346,7 @@ pub async fn download_factorio( async fn wait_for_progress_bar(mpb: MultiProgress) -> Result<(), Box> { tokio::task::spawn_blocking(move || mpb.join()) .await? - .map_err(|e| Box::from(e)) + .map_err(Box::from) } tokio::try_join!(d0, d1, wait_for_progress_bar(mpb))?; @@ -352,7 +357,7 @@ pub async fn download_factorio( async fn download( url: String, - out_dir: &PathBuf, + out_dir: &Path, filter: I, pb: ProgressBar, ) -> Result<(), Box>