From ae9219ef3055139ca3d5b610b77aa41a34ba8d4d Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Tue, 28 Oct 2025 23:40:30 +0800 Subject: [PATCH 1/6] refactor: remove using rust unstable features --- README.md | 3 - pbf-craft/README.md | 3 - pbf-craft/src/lib.rs | 8 --- pbf-craft/src/readers/indexed_reader.rs | 94 ++++++++++++------------- pbf-craft/src/readers/raw_reader.rs | 2 +- 5 files changed, 46 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index e4defa7..7477edf 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,6 @@ It contains a variety of PBF readers for a variety of scenarios. For example, a reader with an index can locate and read elements more efficiently. It also provides a PBF writer that can write PBF data to a file. -Since this crate uses the btree_cursors feature, it requires you to use the **nightly** -version of rust. - - Written in pure Rust - Provides an indexing feature to the PBF to greatly improve read performance. diff --git a/pbf-craft/README.md b/pbf-craft/README.md index 3926da3..64b5bbb 100644 --- a/pbf-craft/README.md +++ b/pbf-craft/README.md @@ -6,9 +6,6 @@ It contains a variety of PBF readers for a variety of scenarios. For example, a reader with an index can locate and read elements more efficiently. It also provides a PBF writer that can write PBF data to a file. -Since this crate uses the btree_cursors feature, it requires you to use the **nightly** -version of rust. - - Written in pure Rust - Provides an indexing feature to the PBF to greatly improve read performance. diff --git a/pbf-craft/src/lib.rs b/pbf-craft/src/lib.rs index 3ec7207..0efc52b 100644 --- a/pbf-craft/src/lib.rs +++ b/pbf-craft/src/lib.rs @@ -4,9 +4,6 @@ //! reader with an index can locate and read elements more efficiently. It also provides //! a PBF writer that can write PBF data to a file. //! -//! Since this crate uses the btree_cursors feature, it requires you to use the **nightly** -//! version of rust. -//! //! # Example //! //! Read PBF data from a file: @@ -48,11 +45,6 @@ //! ``` //! -#![feature(btree_cursors)] -#![feature(test)] - -extern crate test; - mod codecs; /// Contains models for elements of OpenStreetMap data. pub mod models; diff --git a/pbf-craft/src/readers/indexed_reader.rs b/pbf-craft/src/readers/indexed_reader.rs index 6163cb4..e94ca48 100644 --- a/pbf-craft/src/readers/indexed_reader.rs +++ b/pbf-craft/src/readers/indexed_reader.rs @@ -1,7 +1,6 @@ use std::collections::{BTreeMap, HashSet}; use std::fs::File; use std::io::{BufReader, BufWriter, Read, Write}; -use std::ops::Bound; use std::str; use anyhow; @@ -122,17 +121,12 @@ impl PbfIndex { } pub fn get_offset(&self, element_type: &ElementType, element_id: i64) -> Option { - let cursor = match element_type { - ElementType::Node => self.node_index.lower_bound(Bound::Included(&element_id)), - ElementType::Way => self.way_index.lower_bound(Bound::Included(&element_id)), - ElementType::Relation => self - .relation_index - .lower_bound(Bound::Included(&element_id)), + let mut range = match element_type { + ElementType::Node => self.node_index.range(element_id..), + ElementType::Way => self.way_index.range(element_id..), + ElementType::Relation => self.relation_index.range(element_id..), }; - match cursor.peek_next() { - Some((_, offset)) => Some(*offset), - None => None, - } + range.next().map(|(_, offset)| *offset) } fn persist(&self, index_path: &str, checksum: &str) -> anyhow::Result<()> { @@ -291,7 +285,10 @@ impl IndexedReader { let result: Vec = offsets .into_iter() .flat_map(|offset| { - let blob_data = self.pbf_reader.read_blob_by_offset(offset).unwrap(); + let blob_data = self + .pbf_reader + .read_blob_by_offset(offset) + .expect("Failed to read blob by offset"); get_vec(&blob_data) .iter() .filter(|e| element_ids.contains(&e.get_id())) @@ -490,7 +487,6 @@ impl IndexedReader { #[cfg(test)] mod tests { use super::*; - use test::{black_box, Bencher}; #[test] fn test_index_from_pbf_file() { @@ -590,40 +586,40 @@ mod tests { assert!(PbfIndex::load_from_file("nonexistent.pif").is_err()); } - #[bench] - fn bench_find_without_cache(b: &mut Bencher) { - let pbf_file = "./resources/andorra-latest.osm.pbf"; - let mut indexed_reader = IndexedReader::from_path(pbf_file).unwrap(); - - b.iter(|| { - for _ in 1..30 { - let target_op = indexed_reader.find(&ElementType::Node, 4254529698).unwrap(); - target_op.unwrap(); - } - }); - } - - #[bench] - fn bench_find_with_cache(b: &mut Bencher) { - let pbf_file = "./resources/andorra-latest.osm.pbf"; - let mut indexed_reader = IndexedReader::from_path_with_cache(pbf_file, 10000).unwrap(); - - b.iter(|| { - for _ in 1..30 { - let target_op = indexed_reader.find(&ElementType::Node, 4254529698).unwrap(); - target_op.unwrap(); - } - }); - } - - #[bench] - fn bench_batch_operations(b: &mut Bencher) { - let pbf_file = "./resources/andorra-latest.osm.pbf"; - let mut indexed_reader = IndexedReader::from_path(pbf_file).unwrap(); - let node_ids = vec![4254529698, 4254529699, 4254529700]; - - b.iter(|| { - indexed_reader.find_nodes(&node_ids).unwrap(); - }); - } + // #[bench] + // fn bench_find_without_cache(b: &mut Bencher) { + // let pbf_file = "./resources/andorra-latest.osm.pbf"; + // let mut indexed_reader = IndexedReader::from_path(pbf_file).unwrap(); + + // b.iter(|| { + // for _ in 1..30 { + // let target_op = indexed_reader.find(&ElementType::Node, 4254529698).unwrap(); + // target_op.unwrap(); + // } + // }); + // } + + // #[bench] + // fn bench_find_with_cache(b: &mut Bencher) { + // let pbf_file = "./resources/andorra-latest.osm.pbf"; + // let mut indexed_reader = IndexedReader::from_path_with_cache(pbf_file, 10000).unwrap(); + + // b.iter(|| { + // for _ in 1..30 { + // let target_op = indexed_reader.find(&ElementType::Node, 4254529698).unwrap(); + // target_op.unwrap(); + // } + // }); + // } + + // #[bench] + // fn bench_batch_operations(b: &mut Bencher) { + // let pbf_file = "./resources/andorra-latest.osm.pbf"; + // let mut indexed_reader = IndexedReader::from_path(pbf_file).unwrap(); + // let node_ids = vec![4254529698, 4254529699, 4254529700]; + + // b.iter(|| { + // indexed_reader.find_nodes(&node_ids).unwrap(); + // }); + // } } diff --git a/pbf-craft/src/readers/raw_reader.rs b/pbf-craft/src/readers/raw_reader.rs index 6777883..eb3bbe2 100644 --- a/pbf-craft/src/readers/raw_reader.rs +++ b/pbf-craft/src/readers/raw_reader.rs @@ -10,7 +10,7 @@ use crate::codecs::blob::{BlobReader, DecodedBlob}; use crate::codecs::block_decorators::{HeaderReader, PrimitiveReader}; use crate::models::{Element, ElementType}; -/// A foundamental reader for PBF data. +/// A fundamental reader for PBF data. /// /// The `PbfReader` struct provides functionality to read and process PBF files, /// which are commonly used for storing OpenStreetMap (OSM) data. It wraps around From 0150deecdee90f975bc4d9b597f9d7b563b86ac1 Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Wed, 29 Oct 2025 00:01:48 +0800 Subject: [PATCH 2/6] ci and perf fix --- .github/workflows/main.yml | 23 ++++++++++++++++++----- pbf-craft/src/readers/indexed_reader.rs | 3 ++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 82d8ae9..1f940a4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: Main +name: CI on: push: @@ -11,11 +11,24 @@ env: jobs: build: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Build - run: cargo build --release + + - name: Install stable toolchain + uses: dtolnay/rust-toolchain@stable + with: + components: clippy, rustfmt + + - name: Cache cargo artifacts + uses: Swatinem/rust-cache@v2 + + - name: Check formatting + run: cargo fmt --all -- --check + + - name: Run clippy + run: cargo clippy --all-targets --all-features -- -D warnings + - name: Run tests - run: cargo test --verbose + run: cargo test --workspace --all-features --verbose diff --git a/pbf-craft/src/readers/indexed_reader.rs b/pbf-craft/src/readers/indexed_reader.rs index e94ca48..2d617e7 100644 --- a/pbf-craft/src/readers/indexed_reader.rs +++ b/pbf-craft/src/readers/indexed_reader.rs @@ -278,6 +278,7 @@ impl IndexedReader { E: BasicElement, F: Fn(&BlobData) -> &Vec, { + let id_sets: HashSet = element_ids.iter().map(|id| *id).collect(); let offsets: HashSet = element_ids .into_iter() .filter_map(|id| self.pbf_index.get_offset(element_type, *id)) @@ -291,7 +292,7 @@ impl IndexedReader { .expect("Failed to read blob by offset"); get_vec(&blob_data) .iter() - .filter(|e| element_ids.contains(&e.get_id())) + .filter(|e| id_sets.contains(&e.get_id())) .cloned() .collect::>() }) From 1b1c2099d5650e7756dbb7521f871006dae4b357 Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Wed, 29 Oct 2025 00:43:31 +0800 Subject: [PATCH 3/6] refactor: fix lint --- pbf-craft-cli/src/commands/boundary.rs | 8 ++-- pbf-craft-cli/src/commands/diff.rs | 7 +--- pbf-craft-cli/src/commands/search.rs | 15 +++---- pbf-craft-cli/src/commands/with_deps.rs | 1 - pbf-craft-cli/src/db/db_reader.rs | 53 +++++++++++++++---------- pbf-craft-cli/src/db/paging_cursor.rs | 9 ++--- pbf-craft-cli/src/main.rs | 1 - pbf-craft/src/codecs/block_builder.rs | 1 - pbf-craft/src/codecs/field.rs | 4 +- pbf-craft/src/readers/indexed_reader.rs | 15 +++---- pbf-craft/src/readers/raw_reader.rs | 42 +++++++++----------- pbf-craft/src/writers/raw_writer.rs | 2 +- 12 files changed, 79 insertions(+), 79 deletions(-) diff --git a/pbf-craft-cli/src/commands/boundary.rs b/pbf-craft-cli/src/commands/boundary.rs index 6bfdcb9..d00c417 100644 --- a/pbf-craft-cli/src/commands/boundary.rs +++ b/pbf-craft-cli/src/commands/boundary.rs @@ -13,12 +13,12 @@ pub struct BoundaryCommand { impl BoundaryCommand { pub fn run(self) { - let mut reader = - PbfReader::from_path(&self.file).expect(&format!("No such file: {}", self.file)); + let mut reader = PbfReader::from_path(&self.file) + .unwrap_or_else(|_| panic!("No such file: {}", self.file)); let mut polygons: Vec = Vec::new(); while let Some(blob_data) = reader.read_next_blob() { - if blob_data.nodes.len() > 0 { + if !blob_data.nodes.is_empty() { let points: Vec = blob_data .nodes .into_iter() @@ -37,6 +37,6 @@ impl BoundaryCommand { let geometry: Geometry = boundary.into(); let geojson = Value::from(&geometry); dark_yellow_ln!("---------"); - println!("{}", geojson.to_string()); + println!("{}", geojson); } } diff --git a/pbf-craft-cli/src/commands/diff.rs b/pbf-craft-cli/src/commands/diff.rs index df42caf..8ad535f 100644 --- a/pbf-craft-cli/src/commands/diff.rs +++ b/pbf-craft-cli/src/commands/diff.rs @@ -1,7 +1,6 @@ use std::fs::File; use clap::Args; -use csv; use serde::{Deserialize, Serialize}; use pbf_craft::models::{Element, ElementType}; @@ -42,11 +41,9 @@ impl DiffCommand { csv::WriterBuilder::new().from_writer(File::create(&self.output).unwrap()); let mut source = IterableReader::from_path(&self.source) - .expect(&format!("No such file: {}", self.source)) - .into_iter(); + .unwrap_or_else(|_| panic!("No such file: {}", self.source)); let mut target = IterableReader::from_path(&self.target) - .expect(&format!("No such file: {}", self.target)) - .into_iter(); + .unwrap_or_else(|_| panic!("No such file: {}", self.target)); let mut source_element_cnt = source.next(); let mut target_element_cnt = target.next(); diff --git a/pbf-craft-cli/src/commands/search.rs b/pbf-craft-cli/src/commands/search.rs index b1f7b9f..b6f2ab6 100644 --- a/pbf-craft-cli/src/commands/search.rs +++ b/pbf-craft-cli/src/commands/search.rs @@ -52,15 +52,13 @@ impl SearchCommand { } let element_type = element_type_result.unwrap(); - if self.exact.is_none() || self.exact.unwrap() == true { + if self.exact.is_none() || self.exact.unwrap() { let mut indexed_reader = IndexedReader::from_path(&self.file).expect("Indexed reader loading failed"); let find_result = indexed_reader.find(&element_type, *elid).unwrap(); match find_result { Some(ec) => { - let mut list = Vec::new(); - list.push(ec); - list + vec![ec] } None => Vec::with_capacity(0), } @@ -75,7 +73,8 @@ impl SearchCommand { return true; } } - return false; + + false } (Element::Way(way), ElementType::Way) => way.id == *elid, (Element::Relation(relation), ElementType::Relation) => { @@ -88,7 +87,8 @@ impl SearchCommand { return true; } } - return false; + + false } _ => false, }) @@ -133,7 +133,8 @@ impl SearchCommand { return way.way_nodes.iter().any(|ref_node| ref_node.id == first) && way.way_nodes.iter().any(|ref_node| ref_node.id == second); } - return false; + + false }) .expect("node pair error") } else { diff --git a/pbf-craft-cli/src/commands/with_deps.rs b/pbf-craft-cli/src/commands/with_deps.rs index 7dfc2c9..c4e56aa 100644 --- a/pbf-craft-cli/src/commands/with_deps.rs +++ b/pbf-craft-cli/src/commands/with_deps.rs @@ -2,7 +2,6 @@ use std::str::FromStr; use clap::Args; use colored_json::prelude::*; -use serde_json; use pbf_craft::models::{Element, ElementType}; use pbf_craft::readers::IndexedReader; diff --git a/pbf-craft-cli/src/db/db_reader.rs b/pbf-craft-cli/src/db/db_reader.rs index a3c41d7..3512e65 100644 --- a/pbf-craft-cli/src/db/db_reader.rs +++ b/pbf-craft-cli/src/db/db_reader.rs @@ -22,9 +22,19 @@ pub enum DbElementType { Relation, } -impl Into for DbElementType { - fn into(self) -> ElementType { - match self { +// impl Into for DbElementType { +// fn into(self) -> ElementType { +// match self { +// DbElementType::Node => ElementType::Node, +// DbElementType::Way => ElementType::Way, +// DbElementType::Relation => ElementType::Relation, +// } +// } +// } + +impl From for ElementType { + fn from(value: DbElementType) -> Self { + match value { DbElementType::Node => ElementType::Node, DbElementType::Way => ElementType::Way, DbElementType::Relation => ElementType::Relation, @@ -37,7 +47,7 @@ impl DatabaseReader { let mut config = Config::new(); let _ = config .host(&host) - .port(port.clone()) + .port(port) .dbname(&dbname) .user(&user) .password(&password); @@ -109,7 +119,7 @@ impl DatabaseReader { } while current_tag_id <= node.id || current_tag.is_none() { let has_tag = tag_iter.next(); - if let None = has_tag { + if has_tag.is_none() { break; } let tag_row = has_tag.unwrap(); @@ -185,7 +195,7 @@ impl DatabaseReader { } while current_tag_id <= way.id || current_tag.is_none() { let has_tag = tag_iter.next(); - if let None = has_tag { + if has_tag.is_none() { break; } let tag_row = has_tag.unwrap(); @@ -206,7 +216,7 @@ impl DatabaseReader { } while current_mem_id <= way.id || current_mem.is_none() { let has_mem = member_iter.next(); - if let None = has_mem { + if has_mem.is_none() { break; } let mem_row = has_mem.unwrap(); @@ -265,21 +275,20 @@ impl DatabaseReader { let mut current_mem_id = 0; let mut current_mem: Option = None; for el_row in el_cursor { - let mut relation = Relation::default(); - relation.id = el_row.get(0); - relation.changeset_id = el_row.get(1); let timestamp: NaiveDateTime = el_row.get(2); let utc_timestamp: DateTime = DateTime::from_naive_utc_and_offset(timestamp, Utc); - relation.timestamp = Some(utc_timestamp); - let version: i64 = el_row.get(3); - relation.version = version as i32; - relation.visible = el_row.get(4); - let user_id: i64 = el_row.get(5); - let user_name: String = el_row.get(6); - relation.user = Some(OsmUser { - id: user_id as i32, - name: user_name, - }); + let mut relation = Relation { + id: el_row.get(0), + changeset_id: el_row.get(1), + timestamp: Some(utc_timestamp), + version: el_row.get::<_, i64>(3) as i32, + visible: el_row.get(4), + user: Some(OsmUser { + id: el_row.get::<_, i64>(5) as i32, + name: el_row.get(6), + }), + ..Default::default() + }; if relation.id == current_tag_id && current_tag.is_some() { relation.tags.push(current_tag.unwrap()); @@ -287,7 +296,7 @@ impl DatabaseReader { } while current_tag_id <= relation.id || current_tag.is_none() { let has_tag = tag_iter.next(); - if let None = has_tag { + if has_tag.is_none() { break; } let tag_row = has_tag.unwrap(); @@ -308,7 +317,7 @@ impl DatabaseReader { } while current_mem_id <= relation.id || current_mem.is_none() { let has_mem = member_iter.next(); - if let None = has_mem { + if has_mem.is_none() { break; } let mem_row = has_mem.unwrap(); diff --git a/pbf-craft-cli/src/db/paging_cursor.rs b/pbf-craft-cli/src/db/paging_cursor.rs index 1e81f6b..5b91bfc 100644 --- a/pbf-craft-cli/src/db/paging_cursor.rs +++ b/pbf-craft-cli/src/db/paging_cursor.rs @@ -1,4 +1,3 @@ -use std::mem; use std::vec::IntoIter; use postgres::{Client, Portal, Row, Transaction}; @@ -29,21 +28,21 @@ impl<'client> PagingCursor<'client> { pub fn new(sql: &str, client: &'client mut Client) -> PagingCursor<'client> { let mut transaction = client.transaction().unwrap(); let portal = transaction.bind(sql, &[]).unwrap(); - let cursor = Self { + + Self { transaction: Some(transaction), portal, limit: 32000, eof: false, cache: Vec::with_capacity(0).into_iter(), - }; - return cursor; + } } fn fetch_next(&mut self) -> anyhow::Result> { if let Some(trans) = &mut self.transaction { let rows = trans.query_portal(&self.portal, self.limit as i32)?; if rows.len() < self.limit { - let trans = mem::replace(&mut self.transaction, None); + let trans = self.transaction.take(); trans.unwrap().commit()?; self.eof = true; } diff --git a/pbf-craft-cli/src/main.rs b/pbf-craft-cli/src/main.rs index 91be782..e2cdd35 100644 --- a/pbf-craft-cli/src/main.rs +++ b/pbf-craft-cli/src/main.rs @@ -1,7 +1,6 @@ mod commands; mod db; -use env_logger; use std::time::Instant; use clap::Parser; diff --git a/pbf-craft/src/codecs/block_builder.rs b/pbf-craft/src/codecs/block_builder.rs index 9413cad..8e11c4f 100644 --- a/pbf-craft/src/codecs/block_builder.rs +++ b/pbf-craft/src/codecs/block_builder.rs @@ -325,6 +325,5 @@ mod tests { builder.block.get_granularity(), builder.block.get_date_granularity() ); - assert!(true); } } diff --git a/pbf-craft/src/codecs/field.rs b/pbf-craft/src/codecs/field.rs index 62ba32e..fda5339 100644 --- a/pbf-craft/src/codecs/field.rs +++ b/pbf-craft/src/codecs/field.rs @@ -26,7 +26,7 @@ impl FieldCodec { Vec::with_capacity(0) } else { bytes_array - .into_iter() + .iter() .map(|bytes| match String::from_utf8(bytes.clone()) { Ok(str) => str, Err(err) => { @@ -67,7 +67,7 @@ impl FieldCodec { pub fn decode_timestamp(&self, raw_timestamp: i64) -> DateTime { let timestamp = self.date_granularity as i64 * raw_timestamp; - return DateTime::from_timestamp_millis(timestamp).expect("invalid timestamp"); + DateTime::from_timestamp_millis(timestamp).expect("invalid timestamp") } pub fn decode_string(&self, string_id: usize) -> String { diff --git a/pbf-craft/src/readers/indexed_reader.rs b/pbf-craft/src/readers/indexed_reader.rs index 2d617e7..32e4194 100644 --- a/pbf-craft/src/readers/indexed_reader.rs +++ b/pbf-craft/src/readers/indexed_reader.rs @@ -17,7 +17,8 @@ fn get_index_path_from_pbf_path(pbf_path: &str) -> String { let mut index_path = pbf_path.to_owned(); let last_dot_index = index_path.rfind('.').unwrap(); index_path.replace_range(last_dot_index..pbf_path.len(), ".pif"); - return index_path; + + index_path } struct PbfIndex { @@ -97,15 +98,15 @@ impl PbfIndex { let mut reader = PbfReader::from_path(pbf_file_path)?; while let Some(blob_data) = reader.read_next_blob() { - if blob_data.nodes.len() > 0 { + if !blob_data.nodes.is_empty() { let last = blob_data.nodes.last().unwrap(); node_index.insert(last.id, blob_data.offset); } - if blob_data.ways.len() > 0 { + if !blob_data.ways.is_empty() { let last = blob_data.ways.last().unwrap(); way_index.insert(last.id, blob_data.offset); } - if blob_data.relations.len() > 0 { + if !blob_data.relations.is_empty() { let last = blob_data.relations.last().unwrap(); relation_index.insert(last.id, blob_data.offset); } @@ -278,9 +279,9 @@ impl IndexedReader { E: BasicElement, F: Fn(&BlobData) -> &Vec, { - let id_sets: HashSet = element_ids.iter().map(|id| *id).collect(); + let id_sets: HashSet = element_ids.iter().copied().collect(); let offsets: HashSet = element_ids - .into_iter() + .iter() .filter_map(|id| self.pbf_index.get_offset(element_type, *id)) .collect(); let result: Vec = offsets @@ -414,7 +415,7 @@ impl IndexedReader { let nodes = self.find_nodes(&node_ids)?; let mut result: Vec = vec![Element::Way(way)]; - result.extend(nodes.into_iter().map(|node| Element::Node(node))); + result.extend(nodes.into_iter().map(Element::Node)); Ok(result) } diff --git a/pbf-craft/src/readers/raw_reader.rs b/pbf-craft/src/readers/raw_reader.rs index eb3bbe2..1a931fe 100644 --- a/pbf-craft/src/readers/raw_reader.rs +++ b/pbf-craft/src/readers/raw_reader.rs @@ -55,23 +55,22 @@ impl PbfReader { let offset = self.blob_reader.offset; match self.blob_reader.next() { Some(blob) => match blob.decode().expect("Failed to decode block.") { - DecodedBlob::OsmHeader(_) => { - return Some(BlobData { - nodes: Vec::with_capacity(0), - ways: Vec::with_capacity(0), - relations: Vec::with_capacity(0), - offset, - }) - } + DecodedBlob::OsmHeader(_) => Some(BlobData { + nodes: Vec::with_capacity(0), + ways: Vec::with_capacity(0), + relations: Vec::with_capacity(0), + offset, + }), DecodedBlob::OsmData(data) => { let decorator = PrimitiveReader::new(data); let (nodes, ways, relations) = decorator.get_all_elements(); - return Some(BlobData { + + Some(BlobData { nodes, ways, relations, offset, - }); + }) } }, None => None, @@ -186,19 +185,19 @@ impl PbfReader { ElementType::Node => p .get_nodes() .into_iter() - .map(|i| Element::Node(i)) + .map(Element::Node) .filter(&callback) .collect::>(), ElementType::Way => p .get_ways() .into_iter() - .map(|i| Element::Way(i)) + .map(Element::Way) .filter(&callback) .collect::>(), ElementType::Relation => p .get_relations() .into_iter() - .map(|i| Element::Relation(i)) + .map(Element::Relation) .filter(&callback) .collect::>(), }; @@ -207,17 +206,17 @@ impl PbfReader { let (nodes, ways, relations) = p.get_all_elements(); let mut filterd_nodes: Vec = nodes .into_iter() - .map(|i| Element::Node(i)) + .map(Element::Node) .filter(&callback) .collect(); let mut filterd_ways: Vec = ways .into_iter() - .map(|i| Element::Way(i)) + .map(Element::Way) .filter(&callback) .collect(); let mut filterd_relations: Vec = relations .into_iter() - .map(|i| Element::Relation(i)) + .map(Element::Relation) .filter(&callback) .collect(); @@ -226,13 +225,10 @@ impl PbfReader { Some(filterd_nodes) } }) - .reduce( - || Vec::new(), - |mut a, mut b| { - a.append(&mut b); - a - }, - ); + .reduce(Vec::new, |mut a, mut b| { + a.append(&mut b); + a + }); Ok(result) } diff --git a/pbf-craft/src/writers/raw_writer.rs b/pbf-craft/src/writers/raw_writer.rs index 096e0cb..ff1397d 100644 --- a/pbf-craft/src/writers/raw_writer.rs +++ b/pbf-craft/src/writers/raw_writer.rs @@ -148,7 +148,7 @@ impl PbfWriter { self.write_header()?; } let block_builder = PrimitiveBuilder::new(); - let cache = mem::replace(&mut self.cache, Vec::new()); + let cache = mem::take(&mut self.cache); let block = block_builder.build(cache, self.use_dense); let blob = self.build_raw_blob(block.write_to_bytes()?)?; From 5e6d8708a9044a552a97ac7e6b33b943ff021492 Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Wed, 29 Oct 2025 01:04:50 +0800 Subject: [PATCH 4/6] refactor: fix lint --- pbf-craft-cli/src/db/db_reader.rs | 60 ++++++++++-------------- pbf-craft/build.rs | 2 +- pbf-craft/src/codecs/block_builder.rs | 10 ++-- pbf-craft/src/codecs/block_decorators.rs | 32 ++++++------- 4 files changed, 48 insertions(+), 56 deletions(-) diff --git a/pbf-craft-cli/src/db/db_reader.rs b/pbf-craft-cli/src/db/db_reader.rs index 3512e65..1ad3f81 100644 --- a/pbf-craft-cli/src/db/db_reader.rs +++ b/pbf-craft-cli/src/db/db_reader.rs @@ -93,25 +93,20 @@ impl DatabaseReader { let mut current_tag_id = 0; let mut current_tag: Option = None; for node_row in node_cursor { - let mut node = Node::default(); - node.id = node_row.get(0); - let latitude: i32 = node_row.get(1); - let longitude: i32 = node_row.get(2); - node.latitude = latitude as i64 * 100; - node.longitude = longitude as i64 * 100; - node.changeset_id = node_row.get(3); - let timestamp: NaiveDateTime = node_row.get(4); - let utc_timestamp: DateTime = DateTime::from_naive_utc_and_offset(timestamp, Utc); - node.timestamp = Some(utc_timestamp); - let version: i64 = node_row.get(5); - node.version = version as i32; - node.visible = node_row.get(6); - let user_id: i64 = node_row.get(7); - let user_name: String = node_row.get(8); - node.user = Some(OsmUser { - id: user_id as i32, - name: user_name, - }); + let mut node = Node { + id: node_row.get(0), + latitude: node_row.get::<_, i32>(1) as i64 * 100, + longitude: node_row.get::<_, i32>(2) as i64 * 100, + changeset_id: node_row.get(3), + timestamp: Some(DateTime::from_naive_utc_and_offset(node_row.get(4), Utc)), + version: node_row.get::<_, i64>(5) as i32, + visible: node_row.get(6), + user: Some(OsmUser { + id: node_row.get::<_, i64>(7) as i32, + name: node_row.get(8), + }), + ..Default::default() + }; if node.id == current_tag_id && current_tag.is_some() { node.tags.push(current_tag.unwrap()); @@ -173,21 +168,18 @@ impl DatabaseReader { let mut current_mem_id = 0; let mut current_mem: Option = None; for el_row in el_cursor { - let mut way = Way::default(); - way.id = el_row.get(0); - way.changeset_id = el_row.get(1); - let timestamp: NaiveDateTime = el_row.get(2); - let utc_timestamp: DateTime = DateTime::from_naive_utc_and_offset(timestamp, Utc); - way.timestamp = Some(utc_timestamp); - let version: i64 = el_row.get(3); - way.version = version as i32; - way.visible = el_row.get(4); - let user_id: i64 = el_row.get(5); - let user_name: String = el_row.get(6); - way.user = Some(OsmUser { - id: user_id as i32, - name: user_name, - }); + let mut way = Way { + id: el_row.get(0), + changeset_id: el_row.get(1), + timestamp: Some(DateTime::from_naive_utc_and_offset(el_row.get(2), Utc)), + version: el_row.get::<_, i64>(3) as i32, + visible: el_row.get(4), + user: Some(OsmUser { + id: el_row.get::<_, i64>(5) as i32, + name: el_row.get(6), + }), + ..Default::default() + }; if current_tag_id == way.id && current_tag.is_some() { way.tags.push(current_tag.unwrap()); diff --git a/pbf-craft/build.rs b/pbf-craft/build.rs index c25c65a..ca475e7 100644 --- a/pbf-craft/build.rs +++ b/pbf-craft/build.rs @@ -11,7 +11,7 @@ fn main() -> Result<(), Box> { protobuf_codegen_pure::Codegen::new() .out_dir(&out_dir) - .inputs(&proto_files) + .inputs(proto_files) .include("src/proto") .run()?; diff --git a/pbf-craft/src/codecs/block_builder.rs b/pbf-craft/src/codecs/block_builder.rs index 8e11c4f..ec58d92 100644 --- a/pbf-craft/src/codecs/block_builder.rs +++ b/pbf-craft/src/codecs/block_builder.rs @@ -28,7 +28,7 @@ impl StringTableBuilder { id as i32 } - pub fn to_string_table(self) -> osmformat::StringTable { + pub fn into_string_table(self) -> osmformat::StringTable { let string_bytes: Vec> = self .strings .into_iter() @@ -297,18 +297,18 @@ impl PrimitiveBuilder { Element::Relation(relation) => relations.push(relation), } } - if nodes.len() > 0 { + if !nodes.is_empty() { self.add_nodes(nodes, use_dense); } - if ways.len() > 0 { + if !ways.is_empty() { self.add_ways(ways); } - if relations.len() > 0 { + if !relations.is_empty() { self.add_relations(relations); } self.block - .set_stringtable(self.string_table.to_string_table()); + .set_stringtable(self.string_table.into_string_table()); self.block } } diff --git a/pbf-craft/src/codecs/block_decorators.rs b/pbf-craft/src/codecs/block_decorators.rs index aab31bb..3ab7537 100644 --- a/pbf-craft/src/codecs/block_decorators.rs +++ b/pbf-craft/src/codecs/block_decorators.rs @@ -25,7 +25,7 @@ impl HeaderReader { unsupported.push(feature.to_owned()); } } - if unsupported.len() > 0 { + if !unsupported.is_empty() { panic!( "PBF file contains unsupported features: {}", unsupported.join(", ") @@ -155,11 +155,11 @@ impl PrimitiveReader { fn process_dense(&self, dense: &osmformat::DenseNodes) -> Vec { let mut dense_info_iter = DenseInfoIterator::new(dense.get_denseinfo()); - let mut id_iter = dense.get_id().into_iter(); - let mut lat_iter = dense.get_lat().into_iter(); - let mut lon_iter = dense.get_lon().into_iter(); + let mut id_iter = dense.get_id().iter(); + let mut lat_iter = dense.get_lat().iter(); + let mut lon_iter = dense.get_lon().iter(); - let mut kv_iter = dense.get_keys_vals().into_iter(); + let mut kv_iter = dense.get_keys_vals().iter(); let mut result = Vec::with_capacity(dense.id.len()); let mut node_id: i64 = 0; @@ -233,8 +233,8 @@ impl PrimitiveReader { } fn process_tags(&self, keys: &[u32], vals: &[u32]) -> Vec { - let mut key_iter = keys.into_iter(); - let mut val_iter = vals.into_iter(); + let mut key_iter = keys.iter(); + let mut val_iter = vals.iter(); let mut tags: Vec = Vec::new(); loop { match (key_iter.next(), val_iter.next()) { @@ -252,7 +252,7 @@ impl PrimitiveReader { fn process_nodes(&self, nodes: &[osmformat::Node]) -> Vec { nodes - .into_iter() + .iter() .map(|elm| { let tags = self.process_tags(elm.get_keys(), elm.get_vals()); let base_el = if elm.has_info() { @@ -270,7 +270,7 @@ impl PrimitiveReader { } fn process_ways(&self, ways: &[osmformat::Way]) -> Vec { - ways.into_iter() + ways.iter() .map(|elm| { let tags = self.process_tags(elm.get_keys(), elm.get_vals()); let base_el = if elm.has_info() { @@ -284,9 +284,9 @@ impl PrimitiveReader { let mut node_id: i64 = 0; let mut lat: i64 = 0; let mut lon: i64 = 0; - let mut ref_iter = elm.get_refs().into_iter(); - let mut lat_iter = elm.get_lat().into_iter(); - let mut lon_iter = elm.get_lon().into_iter(); + let mut ref_iter = elm.get_refs().iter(); + let mut lat_iter = elm.get_lat().iter(); + let mut lon_iter = elm.get_lon().iter(); loop { match (ref_iter.next(), lat_iter.next(), lon_iter.next()) { (Some(&ref_delta), Some(&lat_delta), Some(&lon_delta)) => { @@ -315,7 +315,7 @@ impl PrimitiveReader { fn process_relations(&self, relations: &[osmformat::Relation]) -> Vec { relations - .into_iter() + .iter() .map(|elm| { let tags = self.process_tags(elm.get_keys(), elm.get_vals()); let base_el = if elm.has_info() { @@ -341,9 +341,9 @@ impl PrimitiveReader { member_types: &[Relation_MemberType], member_roles: &[i32], ) -> Vec { - let mut mid_iter = member_ids.into_iter(); - let mut role_iter = member_roles.into_iter(); - let mut type_iter = member_types.into_iter(); + let mut mid_iter = member_ids.iter(); + let mut role_iter = member_roles.iter(); + let mut type_iter = member_types.iter(); let mut result: Vec = Vec::new(); let mut member_id: i64 = 0; From 0088128ee3e2346520a5732e4d31f636f18bce23 Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Wed, 29 Oct 2025 01:14:13 +0800 Subject: [PATCH 5/6] refactor: fix lint --- pbf-craft/src/lib.rs | 3 +++ pbf-craft/src/readers/indexed_reader.rs | 10 +++++----- pbf-craft/src/readers/raw_reader.rs | 4 ++-- pbf-craft/src/writers/raw_writer.rs | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pbf-craft/src/lib.rs b/pbf-craft/src/lib.rs index 0efc52b..abd665a 100644 --- a/pbf-craft/src/lib.rs +++ b/pbf-craft/src/lib.rs @@ -55,6 +55,9 @@ mod utils; pub mod writers; mod proto { + #![allow(renamed_and_removed_lints)] + #![allow(mismatched_lifetime_syntaxes)] + #![allow(clippy::all)] include!(concat!(env!("OUT_DIR"), "/mod.rs")); } diff --git a/pbf-craft/src/readers/indexed_reader.rs b/pbf-craft/src/readers/indexed_reader.rs index 32e4194..ee1371c 100644 --- a/pbf-craft/src/readers/indexed_reader.rs +++ b/pbf-craft/src/readers/indexed_reader.rs @@ -170,8 +170,8 @@ impl PbfIndex { /// /// # Type Parameters /// -/// * `T` - A type that implements the `PbfRandomRead` trait, providing methods for random access -/// reading of PBF data. +/// * `T` - A type that implements the `PbfRandomRead` trait and provides random access reading of +/// PBF data. /// /// # Fields /// @@ -225,9 +225,9 @@ impl IndexedReader { /// # Parameters /// /// * pbf_file - A path to the PBF file. - /// * cache_capacity - The capacity of the cache. The cache is used to store the parsed Blob from the PBF file. - /// By default, a Blob contains about 8000 elements. Please decide the appropriate capacity - /// according to your memory size. + /// * cache_capacity - The capacity of the cache. The cache stores the parsed Blob from the PBF + /// file. A Blob contains about 8000 elements on average, so choose a capacity that fits your + /// available memory. /// pub fn from_path_with_cache( pbf_file: &str, diff --git a/pbf-craft/src/readers/raw_reader.rs b/pbf-craft/src/readers/raw_reader.rs index 1a931fe..e05e9e9 100644 --- a/pbf-craft/src/readers/raw_reader.rs +++ b/pbf-craft/src/readers/raw_reader.rs @@ -137,9 +137,9 @@ impl PbfReader { /// # Arguments /// /// * `inclination` - An optional reference to an `ElementType` that specifies the type of elements to find. - /// If `None`, all element types are considered. + /// If `None`, all element types are considered. /// * `callback` - A closure that takes a reference to an `Element` and returns a boolean indicating - /// whether the element should be included in the result. The closure must be `Send` and `Sync`. + /// whether the element should be included in the result. The closure must be `Send` and `Sync`. /// /// # Returns /// diff --git a/pbf-craft/src/writers/raw_writer.rs b/pbf-craft/src/writers/raw_writer.rs index ff1397d..c59d378 100644 --- a/pbf-craft/src/writers/raw_writer.rs +++ b/pbf-craft/src/writers/raw_writer.rs @@ -68,7 +68,7 @@ impl PbfWriter { /// # Parameters /// /// * `writer` - The writer to use for writing the PBF data. It should implement the `Write` - /// trait, which is used to write the PBF data + /// trait, which is used to write the PBF data. /// * `use_dense` - A boolean value indicating whether to use dense format for writing nodes. /// pub fn new(writer: W, use_dense: bool) -> PbfWriter { From 09669c31955712bef8d993157a51920ef2b68e8a Mon Sep 17 00:00:00 2001 From: "cloverzero@gmail.com" Date: Wed, 29 Oct 2025 01:19:48 +0800 Subject: [PATCH 6/6] cicd: remove the clippy step --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1f940a4..5026027 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,8 +27,8 @@ jobs: - name: Check formatting run: cargo fmt --all -- --check - - name: Run clippy - run: cargo clippy --all-targets --all-features -- -D warnings + # - name: Run clippy + # run: cargo clippy --all-targets --all-features -- -D warnings - name: Run tests run: cargo test --workspace --all-features --verbose