wps_harvest: fix all cargo check and clippy warnings
- Commit
d6e7638ea4269afadc694c8a8aa7fe7f3a548826- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 14:18 UTC (18 days ago)
- Parents
89f9e5b
Proper Iterator impls for should_implement_trait, digit grouping, eq_ignore_ascii_case, io::Error::other, type aliases. Verified: check/clippy --all-targets 0 warnings, test 103 passed.
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 2c45cb3447..30be2ebbf8 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_build.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_build.rs@@ -135,7 +135,7 @@ fn sort_and_reduce( let mut sorter = ExternalSort::new(scratch, run_records)?; for path in shards { let mut rd = RecordReader::new(BufReader::with_capacity(1 << 20, File::open(path)?));- while let Some(r) = rd.next()? {+ while let Some(r) = rd.next().transpose()? { sorter.push(r)?; } println!(" read {} ({} observations so far)", path.display(), sorter.len());@@ -160,7 +160,7 @@ fn sort_and_reduce( Ok(()) }; - while let Some(r) = merge.next()? {+ while let Some(r) = merge.next().transpose()? { if group.first().is_some_and(|g| g.key != r.key) { flush(&mut group, &mut writer, &mut rejects)?; }@@ -233,8 +233,8 @@ fn run(args: &Args) -> std::io::Result<()> { /// Stream a shard as the `io::Result<Record>` sequence the store writer and verifier take. fn stream_shard(path: &Path) -> std::io::Result<impl Iterator<Item = std::io::Result<Record>>> {- let mut rd = RecordReader::new(BufReader::with_capacity(1 << 20, File::open(path)?));- Ok(std::iter::from_fn(move || rd.next().transpose()))+ let rd = RecordReader::new(BufReader::with_capacity(1 << 20, File::open(path)?));+ Ok(rd) } fn main() -> ExitCode {
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 78aaddd107..f562ccca94 100644--- a/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs+++ b/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs@@ -31,6 +31,11 @@ Usage: wps_cellcrawl --out SHARD --seed MCC,MNC,TAC,CID [--seed ...] [options] /// service that is refusing it. const MAX_CONSECUTIVE_ERRORS: u32 = 5; +/// One cell identity on the frontier: `(mcc, mnc, tac, cid)`.+type CellTuple = (u32, u32, u32, u32);+/// Cells waiting to be queried, nearest-first by distance from the centre.+type Frontier = BinaryHeap<(Reverse<u64>, CellTuple)>;+ struct Args { out: PathBuf, seeds: Vec<(u32, u32, u32, u32)>,@@ -121,8 +126,8 @@ fn run(args: &Args) -> std::io::Result<()> { // ordered by distance from the first seed that resolved. let mut seeds: Vec<(u32, u32, u32, u32)> = args.seeds.clone(); seeds.reverse();- let mut frontier: BinaryHeap<(Reverse<u64>, (u32, u32, u32, u32))> = BinaryHeap::new();- let mut queried: HashSet<(u32, u32, u32, u32)> = HashSet::new();+ let mut frontier: Frontier = BinaryHeap::new();+ let mut queried: HashSet<CellTuple> = HashSet::new(); let mut written: HashSet<u128> = HashSet::new(); let mut center: Option<(i64, i64)> = None; let (mut queries, mut errors) = (0u32, 0u32);
scripts/networklocation/wps_harvest/src/gsloc.rs
diff --git a/scripts/networklocation/wps_harvest/src/gsloc.rs b/scripts/networklocation/wps_harvest/src/gsloc.rsindex b1f5e76b5c..c943fc5101 100644--- a/scripts/networklocation/wps_harvest/src/gsloc.rs+++ b/scripts/networklocation/wps_harvest/src/gsloc.rs@@ -251,8 +251,8 @@ fn parse_location(body: &[u8]) -> Option<(i64, i64, i32, i16)> { let mut lon = None; let mut acc: i64 = -1; let mut alt: i64 = ALT_NOT_REPORTED;- let mut r = proto::Reader::new(body);- while let Some((field, v)) = r.next() {+ let r = proto::Reader::new(body);+ for (field, v) in r { if let Field::Varint(raw) = v { match field { 1 => lat = Some(raw as i64),@@ -275,16 +275,16 @@ fn parse_location(body: &[u8]) -> Option<(i64, i64, i32, i16)> { fn parse_wifi(body: &[u8]) -> Vec<WifiFix> { let mut out = Vec::new();- let mut r = proto::Reader::new(body);- while let Some((field, v)) = r.next() {+ let r = proto::Reader::new(body);+ for (field, v) in r { let Field::Bytes(dev) = v else { continue }; if field != F_WIFI_DEVICES { continue; } let mut bssid = None; let mut loc = None;- let mut dr = proto::Reader::new(dev);- while let Some((df, dv)) = dr.next() {+ let dr = proto::Reader::new(dev);+ for (df, dv) in dr { match (df, dv) { (1, Field::Bytes(s)) => bssid = std::str::from_utf8(s).ok(), (2, Field::Bytes(b)) => loc = parse_location(b),@@ -300,16 +300,16 @@ fn parse_wifi(body: &[u8]) -> Vec<WifiFix> { fn parse_cells(body: &[u8]) -> Vec<CellFix> { let mut out = Vec::new();- let mut r = proto::Reader::new(body);- while let Some((field, v)) = r.next() {+ let r = proto::Reader::new(body);+ for (field, v) in r { let Field::Bytes(tower) = v else { continue }; if field != F_CELL_RESPONSE { continue; } let (mut mcc, mut mnc, mut cell_id, mut area) = (None, None, None, None); let mut loc = None;- let mut tr = proto::Reader::new(tower);- while let Some((tf, tv)) = tr.next() {+ let tr = proto::Reader::new(tower);+ for (tf, tv) in tr { match (tf, tv) { (1, Field::Varint(x)) => mcc = Some(x as i64 as i32), (2, Field::Varint(x)) => mnc = Some(x as i64 as i32),@@ -332,6 +332,9 @@ fn parse_cells(body: &[u8]) -> Vec<CellFix> { mod tests { use super::*; + /// One wifi entry in a fake response: BSSID plus optional `(lat, lon, acc, alt)`.+ type WifiEntry<'a> = (&'a str, Option<(i64, i64, i64, i64)>);+ #[test] fn envelope_matches_wtfps_byte_for_byte() { let framed = frame(&[0xAB, 0xCD]);@@ -371,7 +374,7 @@ mod tests { } /// Build the response body the parser expects: `AppleWLoc { wifi_devices }`.- fn wifi_response(entries: &[(&str, Option<(i64, i64, i64, i64)>)]) -> Vec<u8> {+ fn wifi_response(entries: &[WifiEntry<'_>]) -> Vec<u8> { let mut body = Vec::new(); for (bssid, loc) in entries { let mut dev = Vec::new();@@ -393,7 +396,7 @@ mod tests { fn wifi_responses_decode_and_normalize_case() { let body = wifi_response(&[ ("AA:BB:CC:DD:EE:01", Some((37_77493000, -122_41942000, 35, 12))),- ("aa:bb:cc:dd:ee:02", Some((51_50735000, -0_12776000, 120, 44))),+ ("aa:bb:cc:dd:ee:02", Some((51_50735000, -12_776_000, 120, 44))), ]); let fixes = parse_wifi(&body); assert_eq!(fixes.len(), 2);@@ -401,7 +404,7 @@ mod tests { assert_eq!(fixes[0].lat_e8, 37_77493000); assert_eq!(fixes[0].accuracy_m, 35); assert_eq!(fixes[0].altitude_m, 12);- assert_eq!(fixes[1].lon_e8, -0_12776000);+ assert_eq!(fixes[1].lon_e8, -12_776_000); assert_eq!(fixes[1].altitude_m, 44); }
scripts/networklocation/wps_harvest/src/keys.rs
diff --git a/scripts/networklocation/wps_harvest/src/keys.rs b/scripts/networklocation/wps_harvest/src/keys.rsindex c231f9fcee..c5acd0c64e 100644--- a/scripts/networklocation/wps_harvest/src/keys.rs+++ b/scripts/networklocation/wps_harvest/src/keys.rs@@ -44,7 +44,7 @@ impl RadioType { /// Parse the spellings the open datasets use in their `radio` column. pub fn parse(s: &str) -> Option<RadioType> { match s.trim().to_ascii_uppercase().as_str() {- "GSM" | "2G" | "CDMA" => (s.trim().to_ascii_uppercase() != "CDMA").then_some(RadioType::Gsm),+ "GSM" | "2G" | "CDMA" => (!s.trim().eq_ignore_ascii_case("CDMA")).then_some(RadioType::Gsm), "UMTS" | "WCDMA" | "3G" | "HSPA" => Some(RadioType::Umts), "LTE" | "4G" => Some(RadioType::Lte), "NR" | "5G" | "NRNSA" => Some(RadioType::Nr),
scripts/networklocation/wps_harvest/src/proto.rs
diff --git a/scripts/networklocation/wps_harvest/src/proto.rs b/scripts/networklocation/wps_harvest/src/proto.rsindex 8465b57268..d4fdf316c2 100644--- a/scripts/networklocation/wps_harvest/src/proto.rs+++ b/scripts/networklocation/wps_harvest/src/proto.rs@@ -88,10 +88,15 @@ impl<'a> Reader<'a> { } } +}++impl<'a> Iterator for Reader<'a> {+ type Item = (u32, Field<'a>);+ /// Next `(field number, value)`, or `None` at the end of the message or on malformed /// input. The two are not distinguished: a truncated response and a complete one are /// handled the same way, by using whatever was decoded before the cut.- pub fn next(&mut self) -> Option<(u32, Field<'a>)> {+ fn next(&mut self) -> Option<Self::Item> { if self.pos >= self.buf.len() { return None; }
scripts/networklocation/wps_harvest/src/record.rs
diff --git a/scripts/networklocation/wps_harvest/src/record.rs b/scripts/networklocation/wps_harvest/src/record.rsindex 32e9a746aa..73459e7bce 100644--- a/scripts/networklocation/wps_harvest/src/record.rs+++ b/scripts/networklocation/wps_harvest/src/record.rs@@ -15,13 +15,13 @@ use std::io::{self, Read, Write}; pub const RECORD_BYTES: usize = 40; /// `lat * 1e8`, the range the store's 35-bit latitude field covers.-pub const LAT_E8_MIN: i64 = -90_00_000_000;+pub const LAT_E8_MIN: i64 = -9_000_000_000; /// `lat * 1e8`, upper bound.-pub const LAT_E8_MAX: i64 = 90_00_000_000;+pub const LAT_E8_MAX: i64 = 9_000_000_000; /// `lon * 1e8`, the range the store's 36-bit longitude field covers.-pub const LON_E8_MIN: i64 = -180_00_000_000;+pub const LON_E8_MIN: i64 = -18_000_000_000; /// `lon * 1e8`, upper bound.-pub const LON_E8_MAX: i64 = 180_00_000_000;+pub const LON_E8_MAX: i64 = 18_000_000_000; /// Stored accuracy meaning "the source did not report one". pub const ACC_UNKNOWN: u16 = 0xFFFF;@@ -179,7 +179,7 @@ impl<R: Read> RecordReader<R> { /// /// A partial trailing record is an error rather than an end: shards are produced by long /// runs that can be killed, and silently ignoring a torn tail would quietly drop data.- pub fn next(&mut self) -> io::Result<Option<Record>> {+ fn next_record(&mut self) -> io::Result<Option<Record>> { let mut filled = 0usize; while filled < RECORD_BYTES { match self.inner.read(&mut self.scratch[filled..])? {@@ -202,6 +202,14 @@ impl<R: Read> RecordReader<R> { } } +impl<R: Read> Iterator for RecordReader<R> {+ type Item = io::Result<Record>;++ fn next(&mut self) -> Option<Self::Item> {+ self.next_record().transpose()+ }+}+ #[cfg(test)] mod tests { use super::*;@@ -209,8 +217,8 @@ mod tests { fn sample() -> Record { Record { key: (0x4D904u128 << 64) | 0x3003039ABCDEF123,- lat_e8: 37_77_493_000,- lon_e8: -122_41_942_000,+ lat_e8: 3_777_493_000,+ lon_e8: -12_241_942_000, accuracy_m: 35, altitude_m: 12, source: Source::Gsloc,@@ -278,10 +286,10 @@ mod tests { } assert_eq!(w.finish().unwrap(), 500); - let mut rd = RecordReader::new(bytes.as_slice());+ let rd = RecordReader::new(bytes.as_slice()); let mut got = Vec::new();- while let Some(r) = rd.next().unwrap() {- got.push(r);+ for r in rd {+ got.push(r.unwrap()); } assert_eq!(got, records); }@@ -297,7 +305,10 @@ mod tests { bytes.truncate(RECORD_BYTES + 9); let mut rd = RecordReader::new(bytes.as_slice());- assert_eq!(rd.next().unwrap(), Some(sample()));- assert!(rd.next().is_err(), "a partial tail must not read as end of shard");+ assert_eq!(rd.next().unwrap().unwrap(), sample());+ assert!(+ matches!(rd.next(), Some(Err(_))),+ "a partial tail must not read as end of shard"+ ); } }
scripts/networklocation/wps_harvest/src/sort.rs
diff --git a/scripts/networklocation/wps_harvest/src/sort.rs b/scripts/networklocation/wps_harvest/src/sort.rsindex 50460efe95..ba4508c16b 100644--- a/scripts/networklocation/wps_harvest/src/sort.rs+++ b/scripts/networklocation/wps_harvest/src/sort.rs@@ -131,7 +131,7 @@ impl Merge { let mut heap = BinaryHeap::with_capacity(paths.len()); for (which, p) in paths.iter().enumerate() { let mut rd = RecordReader::new(BufReader::new(File::open(p)?));- if let Some(record) = rd.next()? {+ if let Some(record) = rd.next().transpose()? { heap.push(Head { record, which }); } readers.push(rd);@@ -149,16 +149,16 @@ impl Merge { self.remaining == 0 } - /// Next record in sorted order.- pub fn next(&mut self) -> io::Result<Option<Record>> {+ /// Next record in sorted order, or `Ok(None)` when the merge is drained.+ fn next_record(&mut self) -> io::Result<Option<Record>> { let Some(Head { record, which }) = self.heap.pop() else { return Ok(None); }; let reader = self .readers .get_mut(which)- .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "merge run index out of range"))?;- if let Some(next) = reader.next()? {+ .ok_or_else(|| io::Error::other("merge run index out of range"))?;+ if let Some(next) = reader.next().transpose()? { self.heap.push(Head { record: next, which }); } self.remaining = self.remaining.saturating_sub(1);@@ -166,6 +166,14 @@ impl Merge { } } +impl Iterator for Merge {+ type Item = io::Result<Record>;++ fn next(&mut self) -> Option<Self::Item> {+ self.next_record().transpose()+ }+}+ impl Drop for Merge { fn drop(&mut self) { for p in &self.paths {@@ -205,10 +213,10 @@ mod tests { } } - fn drain(mut m: Merge) -> Vec<Record> {+ fn drain(m: Merge) -> Vec<Record> { let mut out = Vec::new();- while let Some(r) = m.next().unwrap() {- out.push(r);+ for r in m {+ out.push(r.unwrap()); } out }
scripts/networklocation/wps_harvest/src/store_extra.rs
diff --git a/scripts/networklocation/wps_harvest/src/store_extra.rs b/scripts/networklocation/wps_harvest/src/store_extra.rsindex 1f2f09ef19..4d7b77503f 100644--- a/scripts/networklocation/wps_harvest/src/store_extra.rs+++ b/scripts/networklocation/wps_harvest/src/store_extra.rs@@ -81,8 +81,8 @@ mod tests { let mut recs: Vec<Record> = (0..800) .map(|_| Record { key: (((lcg(&mut state) & 0xFFFFF) as u128) << 64) | lcg(&mut state) as u128,- lat_e8: 37_77_493_000,- lon_e8: -122_41_942_000,+ lat_e8: 3_777_493_000,+ lon_e8: -12_241_942_000, accuracy_m: 900, altitude_m: ALT_UNKNOWN, source: Source::OpenCellId,
scripts/networklocation/wps_harvest/src/v1.rs
diff --git a/scripts/networklocation/wps_harvest/src/v1.rs b/scripts/networklocation/wps_harvest/src/v1.rsindex 789d8b46b2..bc3f483f03 100644--- a/scripts/networklocation/wps_harvest/src/v1.rs+++ b/scripts/networklocation/wps_harvest/src/v1.rs@@ -141,7 +141,7 @@ mod tests { l += 1; } }- let mut push_bits = |buf: &mut Vec<u8>, bitlen: &mut u64, v: u64, nbits: u32| {+ let push_bits = |buf: &mut Vec<u8>, bitlen: &mut u64, v: u64, nbits: u32| { for k in 0..nbits as u64 { if (*bitlen & 7) == 0 { buf.push(0);