From 53979496c591459bb68060eee7cfcd6142c9b4c8 Mon Sep 17 00:00:00 2001 From: William Venner Date: Wed, 6 Sep 2023 20:18:19 +0100 Subject: [PATCH] better formatting for that macro --- .github/workflows/ci.yml | 2 +- fastgmad-lib/src/create/mod.rs | 8 +- fastgmad-lib/src/extract/mod.rs | 217 +++++++++---------------------- fastgmad-lib/src/workshop/mod.rs | 59 +++------ 4 files changed, 83 insertions(+), 203 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d40869..32d400e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,4 +45,4 @@ jobs: steps: - uses: actions/checkout@v2 - name: Test - run: cargo test --release --package fastgmad --all + run: cargo test --release --package fastgmad diff --git a/fastgmad-lib/src/create/mod.rs b/fastgmad-lib/src/create/mod.rs index 75a8695..b095a1a 100644 --- a/fastgmad-lib/src/create/mod.rs +++ b/fastgmad-lib/src/create/mod.rs @@ -189,13 +189,9 @@ fn discover_entries(folder: &Path, ignore: &[String], warn_invalid: bool) -> Res let path = entry.path(); let relative_path = path .strip_prefix(folder) - .map_err( - |_| fastgmad_io_error!(error: std::io::Error::new(std::io::ErrorKind::InvalidData, "File not in addon directory"), path: path), - )? + .map_err(|_| fastgmad_io_error!(error: std::io::Error::new(std::io::ErrorKind::InvalidData, "File not in addon directory"), path: path))? .to_str() - .ok_or_else( - || fastgmad_io_error!(error: std::io::Error::new(std::io::ErrorKind::InvalidData, "File path not valid UTF-8"), path: path), - )? + .ok_or_else(|| fastgmad_io_error!(error: std::io::Error::new(std::io::ErrorKind::InvalidData, "File path not valid UTF-8"), path: path))? .replace('\\', "/"); if relative_path == "addon.json" { diff --git a/fastgmad-lib/src/extract/mod.rs b/fastgmad-lib/src/extract/mod.rs index f8bee29..2516407 100644 --- a/fastgmad-lib/src/extract/mod.rs +++ b/fastgmad-lib/src/extract/mod.rs @@ -44,9 +44,7 @@ trait ExtractGma { ); } - std::fs::create_dir_all(&conf.out).map_err(|error| { - fastgmad_io_error!(while "creating output directory",error: error,path: conf.out) - })?; + std::fs::create_dir_all(&conf.out).map_err(|error| fastgmad_io_error!(while "creating output directory", error: error, path: conf.out))?; log::info!("Reading metadata..."); @@ -65,9 +63,9 @@ trait ExtractGma { } } - let version = r.read_u8().map_err(|error| { - fastgmad_io_error!(while "reading version byte", error: error) - })?; + let version = r + .read_u8() + .map_err(|error| fastgmad_io_error!(while "reading version byte", error: error))?; if version != GMA_VERSION { log::warn!("File is in GMA version {version}, expected version {GMA_VERSION}, reading anyway..."); } @@ -84,12 +82,11 @@ trait ExtractGma { // Required content loop { buf.clear(); - let content = r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading required content", - error: error - ) - })?; + + let content = r + .read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading required content", error: error))?; + if content.is_empty() { break; } @@ -99,54 +96,41 @@ trait ExtractGma { // Addon name let title = { buf.clear(); - let title = r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading addon name", - error: error - ) - })?; + + let title = r + .read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading addon name", error: error))?; + title.to_vec() }; // addon.json let addon_json = { buf.clear(); - let addon_json = r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading addon description", - error: error - ) - })?; + + let addon_json = r + .read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading addon description", error: error))?; + addon_json.to_vec() }; // Addon author - r.skip_nul_str().map_err(|error| { - fastgmad_io_error!( - while "reading addon author", - error: error - ) - })?; + r.skip_nul_str() + .map_err(|error| fastgmad_io_error!(while "reading addon author", error: error))?; // Addon version (unused) - r.read_exact(&mut [0u8; 4]).map_err(|error| { - fastgmad_io_error!( - while "reading addon version", - error: error - ) - })?; + r.read_exact(&mut [0u8; 4]) + .map_err(|error| fastgmad_io_error!(while "reading addon version", error: error))?; log::info!("Writing addon.json..."); let addon_json_path; { addon_json_path = conf.out.join("addon.json"); - let mut addon_json_f = BufWriter::new(File::create(&addon_json_path).map_err(|error| { - fastgmad_io_error!( - while "creating addon.json file", - error: error, - path: addon_json_path - ) - })?); + let mut addon_json_f = BufWriter::new( + File::create(&addon_json_path) + .map_err(|error| fastgmad_io_error!(while "creating addon.json file", error: error, path: addon_json_path))?, + ); let res = if let Ok(mut kv) = serde_json::from_slice::>(&addon_json) { // Add title key if it doesn't exist if let serde_json::map::Entry::Vacant(v) = kv.entry("title".to_string()) { @@ -164,25 +148,14 @@ trait ExtractGma { }; res.map_err(|error| { if let Some(io_error) = error.io_error_kind() { - fastgmad_io_error!( - while "writing addon.json", - error: std::io::Error::from(io_error), - path: addon_json_path - ) + fastgmad_io_error!(while "writing addon.json", error: std::io::Error::from(io_error), path: addon_json_path) } else { - fastgmad_error!( - while "serializing addon.json", - error: error - ) + fastgmad_error!(while "serializing addon.json", error: error) } })?; - addon_json_f.flush().map_err(|error| { - fastgmad_io_error!( - while "flushing addon.json", - error: error, - path: addon_json_path - ) - })?; + addon_json_f + .flush() + .map_err(|error| fastgmad_io_error!(while "flushing addon.json", error: error, path: addon_json_path))?; } // File index @@ -199,27 +172,18 @@ trait ExtractGma { { let path = { buf.clear(); - let path = r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading entry path", - error: error - ) - })?; + let path = r + .read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading entry path", error: error))?; path.to_vec() }; - let size = r.read_i64::().map_err(|error| { - fastgmad_io_error!( - while "reading entry size", - error: error - ) - })?; - let _crc = r.read_u32::().map_err(|error| { - fastgmad_io_error!( - while "reading entry CRC", - error: error - ) - })?; + let size = r + .read_i64::() + .map_err(|error| fastgmad_io_error!(while "reading entry size", error: error))?; + let _crc = r + .read_u32::() + .map_err(|error| fastgmad_io_error!(while "reading entry CRC", error: error))?; if let Some(entry) = GmaEntry::try_new(&conf.out, path, size) { #[cfg(feature = "binary")] @@ -280,38 +244,19 @@ impl ExtractGma for StandardExtractGma { for GmaEntry { path, size } in file_index.iter() { if let Some(parent) = path.parent() { if parent != conf.out { - std::fs::create_dir_all(parent).map_err(|error| { - fastgmad_io_error!( - while "creating directory for GMA entry", - error: error, - path: parent - ) - })?; + std::fs::create_dir_all(parent) + .map_err(|error| fastgmad_io_error!(while "creating directory for GMA entry", error: error, path: parent))?; } } let mut take = r.take(*size as u64); - let mut w = File::create(path).map_err(|error| { - fastgmad_io_error!( - while "creating file for GMA entry", - error: error, - path: path - ) - })?; - std::io::copy(&mut take, &mut w).map_err(|error| { - fastgmad_io_error!( - while "copying GMA entry data", - error: error, - path: path - ) - })?; - w.flush().map_err(|error| { - fastgmad_io_error!( - while "flushing GMA entry file", - error: error, - path: path - ) - })?; + let mut w = File::create(path).map_err(|error| fastgmad_io_error!(while "creating file for GMA entry", error: error, path: path))?; + + std::io::copy(&mut take, &mut w).map_err(|error| fastgmad_io_error!(while "copying GMA entry data", error: error, path: path))?; + + w.flush() + .map_err(|error| fastgmad_io_error!(while "flushing GMA entry file", error: error, path: path))?; + r = take.into_inner(); #[cfg(feature = "binary")] @@ -369,13 +314,8 @@ impl ExtractGma for ParallelExtractGma { let mut buf = Vec::with_capacity(*size); let mut take = r.take(*size as u64); - take.read_to_end(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading GMA entry data", - error: error, - path: path - ) - })?; + take.read_to_end(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading GMA entry data", error: error, path: path))?; r = take.into_inner(); let memory_used = &memory_used; @@ -384,23 +324,13 @@ impl ExtractGma for ParallelExtractGma { let res = (move || { if let Some(parent) = path.parent() { if parent != conf.out { - std::fs::create_dir_all(parent).map_err(|error| { - fastgmad_io_error!( - while "creating directory for GMA entry", - error: error, - path: parent - ) - })?; + std::fs::create_dir_all(parent) + .map_err(|error| fastgmad_io_error!(while "creating directory for GMA entry", error: error, path: parent))?; } } - std::fs::write(path, buf).map_err(|error| { - fastgmad_io_error!( - while "writing GMA entry file", - error: error, - path: path - ) - })?; + std::fs::write(path, buf) + .map_err(|error| fastgmad_io_error!(while "writing GMA entry file", error: error, path: path))?; Ok::<_, FastGmadError>(()) })(); @@ -415,38 +345,17 @@ impl ExtractGma for ParallelExtractGma { // Just do it without buffering if let Some(parent) = path.parent() { if parent != conf.out { - std::fs::create_dir_all(parent).map_err(|error| { - fastgmad_io_error!( - while "creating directory for GMA entry", - error: error, - path: parent - ) - })?; + std::fs::create_dir_all(parent) + .map_err(|error| fastgmad_io_error!(while "creating directory for GMA entry", error: error, path: parent))?; } } let mut take = r.take(*size as u64); - let mut w = File::create(path).map_err(|error| { - fastgmad_io_error!( - while "creating file for GMA entry", - error: error, - path: path - ) - })?; - std::io::copy(&mut take, &mut w).map_err(|error| { - fastgmad_io_error!( - while "copying GMA entry data", - error: error, - path: path - ) - })?; - w.flush().map_err(|error| { - fastgmad_io_error!( - while "flushing GMA entry file", - error: error, - path: path - ) - })?; + let mut w = + File::create(path).map_err(|error| fastgmad_io_error!(while "creating file for GMA entry", error: error, path: path))?; + std::io::copy(&mut take, &mut w).map_err(|error| fastgmad_io_error!(while "copying GMA entry data", error: error, path: path))?; + w.flush() + .map_err(|error| fastgmad_io_error!(while "flushing GMA entry file", error: error, path: path))?; r = take.into_inner(); } diff --git a/fastgmad-lib/src/workshop/mod.rs b/fastgmad-lib/src/workshop/mod.rs index cde9f2c..011369e 100644 --- a/fastgmad-lib/src/workshop/mod.rs +++ b/fastgmad-lib/src/workshop/mod.rs @@ -458,43 +458,29 @@ impl GmaPublishingMetadata { } } - let version = r.read_u8().map_err(|error| { - fastgmad_io_error!( - while "reading version byte", - error: error - ) - })?; + let version = r + .read_u8() + .map_err(|error| fastgmad_io_error!(while "reading version byte", error: error))?; if version != GMA_VERSION { log::warn!("File is in GMA version {version}, expected version {GMA_VERSION}, reading anyway..."); } // SteamID (unused) - r.read_exact(&mut [0u8; 8]).map_err(|error| { - fastgmad_io_error!( - while "reading SteamID", - error: error - ) - })?; + r.read_exact(&mut [0u8; 8]) + .map_err(|error| fastgmad_io_error!(while "reading SteamID", error: error))?; // Timestamp - r.read_exact(&mut [0u8; 8]).map_err(|error| { - fastgmad_io_error!( - while "reading timestamp", - error: error - ) - })?; + r.read_exact(&mut [0u8; 8]) + .map_err(|error| fastgmad_io_error!(while "reading timestamp", error: error))?; if version > 1 { // Required content let mut buf = Vec::new(); loop { buf.clear(); - let content = r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading required content", - error: error - ) - })?; + let content = r + .read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading required content", error: error))?; if content.is_empty() { break; } @@ -504,34 +490,23 @@ impl GmaPublishingMetadata { // Addon name metadata.title = { let mut buf = Vec::new(); - r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading addon name", - error: error - ) - })?; + r.read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading addon name", error: error))?; if buf.last() == Some(&0) { buf.pop(); } - String::from_utf8(buf).map_err(|error| { - fastgmad_io_error!( - while "decoding addon name", - error: std::io::Error::new(std::io::ErrorKind::InvalidData, error) - ) - })? + String::from_utf8(buf).map_err( + |error| fastgmad_io_error!(while "decoding addon name", error: std::io::Error::new(std::io::ErrorKind::InvalidData, error)), + )? }; // addon.json { let mut buf = Vec::new(); - r.read_nul_str(&mut buf).map_err(|error| { - fastgmad_io_error!( - while "reading addon description", - error: error - ) - })?; + r.read_nul_str(&mut buf) + .map_err(|error| fastgmad_io_error!(while "reading addon description", error: error))?; if buf.last() == Some(&0) { buf.pop();