Modern Apps git

← Modern-Apps.git

wps_harvest: dedupe binary main into run_main

Commit
bf610cbbc45350059ea76dd2998d76cf4861ed3c
Author
Vayun Mathur <[email protected]>
When
2026-12-01 21:39 UTC (4 days ago)
Parents
013b178
Four of the five harvest binaries (wps_build, wps_cellcrawl,
wps_crawl, wps_extract) carried a byte-identical 19-line main:
collect argv, parse it, print the usage and exit 0 only on an
explicit help request when parsing fails, then run and map the
io result onto the exit code. wps_seed differs (it announces the
dump it is reading mid-main), so it keeps its own main.

Move the shared body into the library as
wps_harvest::run_main(parse_args, usage, run), generic over each
binary's own Args type, so every binary keeps its own parse_args,
run and USAGE and only its main shrinks to a one-line call. No
body logic changes: the helper is the deleted main with the three
per-binary names passed in. Net -39 lines.

Verified: main bodies compared verbatim before deleting - all
four identical. cargo test --locked in the crate: 103 passed,
0 failed (100 lib + 3 bin tests, same as baseline). Smoke:
wps_extract --help prints usage and exits 0, wps_build with no
arguments prints its parse error plus usage and exits 1, exactly
as before. cargo fmt --all -- --check clean, structure_check
lines OK (networklocation 8,507).

Diff

scripts/networklocation/wps_harvest/src/bin/wps_build.rs
diff --git a/scripts/networklocation/wps_harvest/src/bin/wps_build.rs b/scripts/networklocation/wps_harvest/src/bin/wps_build.rsindex d19648229a..9a82664e84 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_build.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_build.rs@@ -231,22 +231,5 @@ fn stream_shard(path: &Path) -> std::io::Result<impl Iterator<Item = std::io::Re }  fn main() -> ExitCode {-    let argv: Vec<String> = std::env::args().skip(1).collect();-    let args = match parse_args(&argv) {-        Ok(a) => a,-        Err(e) => {-            if e != "help" {-                eprintln!("error: {e}");-            }-            eprintln!("{USAGE}");-            return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };-        }-    };-    match run(&args) {-        Ok(()) => ExitCode::SUCCESS,-        Err(e) => {-            eprintln!("error: {e}");-            ExitCode::FAILURE-        }-    }+    wps_harvest::run_main(parse_args, USAGE, run) }
scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs
diff --git a/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs b/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rsindex fac1ea77a0..411bd8cf9f 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs@@ -200,22 +200,5 @@ fn run(args: &Args) -> std::io::Result<()> { }  fn main() -> ExitCode {-    let argv: Vec<String> = std::env::args().skip(1).collect();-    let args = match parse_args(&argv) {-        Ok(a) => a,-        Err(e) => {-            if e != "help" {-                eprintln!("error: {e}");-            }-            eprintln!("{USAGE}");-            return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };-        }-    };-    match run(&args) {-        Ok(()) => ExitCode::SUCCESS,-        Err(e) => {-            eprintln!("error: {e}");-            ExitCode::FAILURE-        }-    }+    wps_harvest::run_main(parse_args, USAGE, run) }
scripts/networklocation/wps_harvest/src/bin/wps_crawl.rs
diff --git a/scripts/networklocation/wps_harvest/src/bin/wps_crawl.rs b/scripts/networklocation/wps_harvest/src/bin/wps_crawl.rsindex f807821c96..710fba539b 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_crawl.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_crawl.rs@@ -383,24 +383,7 @@ fn format_mac(mac: u64) -> String { }  fn main() -> ExitCode {-    let argv: Vec<String> = std::env::args().skip(1).collect();-    let args = match parse_args(&argv) {-        Ok(a) => a,-        Err(e) => {-            if e != "help" {-                eprintln!("error: {e}");-            }-            eprintln!("{USAGE}");-            return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };-        }-    };-    match run(&args) {-        Ok(()) => ExitCode::SUCCESS,-        Err(e) => {-            eprintln!("error: {e}");-            ExitCode::FAILURE-        }-    }+    wps_harvest::run_main(parse_args, USAGE, run) }  #[cfg(test)]
scripts/networklocation/wps_harvest/src/bin/wps_extract.rs
diff --git a/scripts/networklocation/wps_harvest/src/bin/wps_extract.rs b/scripts/networklocation/wps_harvest/src/bin/wps_extract.rsindex f859dfafcc..56dcc3ab0b 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_extract.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_extract.rs@@ -126,22 +126,5 @@ fn run(args: &Args) -> std::io::Result<()> { }  fn main() -> ExitCode {-    let argv: Vec<String> = std::env::args().skip(1).collect();-    let args = match parse_args(&argv) {-        Ok(a) => a,-        Err(e) => {-            if e != "help" {-                eprintln!("error: {e}");-            }-            eprintln!("{USAGE}");-            return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };-        }-    };-    match run(&args) {-        Ok(()) => ExitCode::SUCCESS,-        Err(e) => {-            eprintln!("error: {e}");-            ExitCode::FAILURE-        }-    }+    wps_harvest::run_main(parse_args, USAGE, run) }
scripts/networklocation/wps_harvest/src/lib.rs
diff --git a/scripts/networklocation/wps_harvest/src/lib.rs b/scripts/networklocation/wps_harvest/src/lib.rsindex 6edd979970..c5e96ade94 100644--- a/scripts/networklocation/wps_harvest/src/lib.rs+++ b/scripts/networklocation/wps_harvest/src/lib.rs@@ -40,6 +40,35 @@ pub mod v1;  use std::io::{self, BufRead, BufReader}; use std::path::Path;+use std::process::ExitCode;++/// Shared `main` for the harvest binaries: parse, report usage on a parse+/// error (exit success only for an explicit `help` request), then run and map+/// the result onto the process exit code.+pub fn run_main<A>(+    parse_args: impl Fn(&[String]) -> Result<A, String>,+    usage: &str,+    run: impl Fn(&A) -> io::Result<()>,+) -> ExitCode {+    let argv: Vec<String> = std::env::args().skip(1).collect();+    let args = match parse_args(&argv) {+        Ok(a) => a,+        Err(e) => {+            if e != "help" {+                eprintln!("error: {e}");+            }+            eprintln!("{usage}");+            return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };+        }+    };+    match run(&args) {+        Ok(()) => ExitCode::SUCCESS,+        Err(e) => {+            eprintln!("error: {e}");+            ExitCode::FAILURE+        }+    }+}  /// Open a path for buffered reading, or stdin when it is `-`. ///