Rearrange code

This commit is contained in:
Dennis 2024-05-30 16:39:51 +02:00
parent 797db7a097
commit 699ac31383
No known key found for this signature in database
GPG Key ID: 6937EAEA33A3FA5D
4 changed files with 90 additions and 89 deletions

View File

@ -46,6 +46,6 @@ impl Iterator for LexicographicFileWalker {
temp_dirs.sort(); temp_dirs.sort();
self.dirs.extend(temp_dirs.into_iter()); self.dirs.extend(temp_dirs.into_iter());
} }
return self.files.pop_front(); self.files.pop_front()
} }
} }

2
tests/common/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod lexicographic_file_walker;
pub mod osm;

View File

@ -7,7 +7,7 @@ use std::collections::HashMap;
use xml_builder::{XMLBuilder, XMLElement, XMLVersion}; use xml_builder::{XMLBuilder, XMLElement, XMLVersion};
static OSM_USER: &str = "osrm"; static OSM_USER: &str = "osrm";
static OSM_TIMESTAMP :&str = "2000-01-01T00:00:00Z"; static OSM_TIMESTAMP: &str = "2000-01-01T00:00:00Z";
static OSM_UID: &str = "1"; static OSM_UID: &str = "1";
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -19,25 +19,25 @@ pub struct OSMNode {
} }
impl OSMNode { impl OSMNode {
pub fn add_tag(&mut self, key: &str, value: &str) { // pub fn add_tag(&mut self, key: &str, value: &str) {
self.tags.insert(key.into(), value.into()); // self.tags.insert(key.into(), value.into());
} // }
pub fn set_id_(&mut self, id: u64) { // pub fn set_id_(&mut self, id: u64) {
self.id = id; // self.id = id;
} // }
pub fn set_tags(&mut self, tags: HashMap<String, String>) { // pub fn set_tags(&mut self, tags: HashMap<String, String>) {
self.tags = tags // self.tags = tags
} // }
pub fn to_xml(&self) -> XMLElement { pub fn to_xml(&self) -> XMLElement {
let mut node = XMLElement::new("node"); let mut node = XMLElement::new("node");
node.add_attribute("id", &self.id.to_string()); node.add_attribute("id", &self.id.to_string());
node.add_attribute("version", "1"); node.add_attribute("version", "1");
node.add_attribute("uid", &OSM_UID); node.add_attribute("uid", OSM_UID);
node.add_attribute("user", &OSM_USER); node.add_attribute("user", OSM_USER);
node.add_attribute("timestamp", &OSM_TIMESTAMP); node.add_attribute("timestamp", OSM_TIMESTAMP);
node.add_attribute("lon", &format!("{:?}", self.lon)); node.add_attribute("lon", &format!("{:?}", self.lon));
node.add_attribute("lat", &format!("{:?}", self.lat)); node.add_attribute("lat", &format!("{:?}", self.lat));
@ -67,17 +67,17 @@ impl OSMWay {
self.nodes.push(node); self.nodes.push(node);
} }
pub fn set_tags(&mut self, tags: HashMap<String, String>) { // pub fn set_tags(&mut self, tags: HashMap<String, String>) {
self.tags = tags; // self.tags = tags;
} // }
pub fn to_xml(&self) -> XMLElement { pub fn to_xml(&self) -> XMLElement {
let mut way = XMLElement::new("way"); let mut way = XMLElement::new("way");
way.add_attribute("id", &self.id.to_string()); way.add_attribute("id", &self.id.to_string());
way.add_attribute("version", "1"); way.add_attribute("version", "1");
way.add_attribute("uid", &OSM_UID); way.add_attribute("uid", OSM_UID);
way.add_attribute("user", &OSM_USER); way.add_attribute("user", OSM_USER);
way.add_attribute("timestamp", &OSM_TIMESTAMP); way.add_attribute("timestamp", OSM_TIMESTAMP);
assert!(self.nodes.len() >= 2); assert!(self.nodes.len() >= 2);
@ -104,71 +104,72 @@ impl OSMWay {
} }
} }
#[derive(Clone, Debug)] // #[derive(Clone, Debug)]
struct Member { // struct Member {
id: u64, // id: u64,
member_type: String, // member_type: String,
member_role: String, // member_role: String,
} // }
#[derive(Clone, Debug)] // #[derive(Clone, Debug)]
struct OSMRelation { // struct OSMRelation {
id: u64, // id: u64,
osm_user: String, // osm_user: String,
osm_time_stamp: String, // osm_time_stamp: String,
osm_uid: String, // osm_uid: String,
members: Vec<Member>, // members: Vec<Member>,
tags: HashMap<String, String>, // tags: HashMap<String, String>,
} // }
impl OSMRelation { // impl OSMRelation {
fn add_member(&mut self, member_type: String, id: u64, member_role: String) { // fn add_member(&mut self, member_type: String, id: u64, member_role: String) {
self.members.push(Member { // self.members.push(Member {
id, // id,
member_type, // member_type,
member_role, // member_role,
}); // });
} // }
pub fn add_tag(&mut self, key: &str, value: &str) { // pub fn add_tag(&mut self, key: &str, value: &str) {
self.tags.insert(key.into(), value.into()); // self.tags.insert(key.into(), value.into());
} // }
} // }
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct OSMDb { pub struct OSMDb {
nodes: Vec<(char, OSMNode)>, nodes: Vec<(char, OSMNode)>,
ways: Vec<OSMWay>, ways: Vec<OSMWay>,
relations: Vec<OSMRelation>, // relations: Vec<OSMRelation>,
} }
impl OSMDb { impl OSMDb {
pub fn add_node(&mut self, node: OSMNode) { pub fn add_node(&mut self, node: OSMNode) {
let name = node.tags.get("name").unwrap(); let name = node.tags.get("name").unwrap();
assert!(name.len() == 1, "name needs to be of length 1, but was \"{name}\""); assert!(
self.nodes.push((name.chars().next().unwrap() as char, node)); name.len() == 1,
"name needs to be of length 1, but was \"{name}\""
);
self.nodes.push((name.chars().next().unwrap(), node));
} }
pub fn find_node(&self, search_name: char) -> Option<&(char, OSMNode)> { pub fn find_node(&self, search_name: char) -> Option<&(char, OSMNode)> {
// TODO: this is a linear search. // TODO: this is a linear search.
self.nodes.iter().find(|(name, _node)| { self.nodes.iter().find(|(name, _node)| search_name == *name)
search_name == *name
})
} }
pub fn add_way(&mut self, way: OSMWay) { pub fn add_way(&mut self, way: OSMWay) {
self.ways.push(way); self.ways.push(way);
} }
pub fn add_relation(&mut self, relation: OSMRelation) { // pub fn add_relation(&mut self, relation: OSMRelation) {
self.relations.push(relation); // self.relations.push(relation);
} // }
pub fn clear(&mut self) { // pub fn clear(&mut self) {
self.nodes.clear(); // self.nodes.clear();
self.ways.clear(); // self.ways.clear();
self.relations.clear(); // // self.relations.clear();
} // }
pub fn to_xml(&self) -> String { pub fn to_xml(&self) -> String {
let mut xml = XMLBuilder::new() let mut xml = XMLBuilder::new()
@ -195,32 +196,33 @@ impl OSMDb {
String::from_utf8(writer).unwrap() String::from_utf8(writer).unwrap()
} }
pub fn node_len(&self) -> usize { // pub fn node_len(&self) -> usize {
self.nodes.len() // self.nodes.len()
} // }
pub fn way_len(&self) -> usize { // pub fn way_len(&self) -> usize {
self.ways.len() // self.ways.len()
} // }
pub fn relation_len(&self) -> usize { // pub fn relation_len(&self) -> usize {
self.relations.len() // self.relations.len()
} // }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod tests {
use super::{OSMNode, OSMWay};
#[test] #[test]
fn empty_db_by_default() { fn empty_db_by_default() {
let osm_db = crate::OSMDb::default(); use super::*;
let osm_db = OSMDb::default();
assert_eq!(0, osm_db.node_len()); assert_eq!(0, osm_db.node_len());
assert_eq!(0, osm_db.way_len()); assert_eq!(0, osm_db.way_len());
assert_eq!(0, osm_db.relation_len()); // assert_eq!(0, osm_db.relation_len());
} }
#[test] #[test]
fn osm_db_with_single_node() { fn osm_db_with_single_node() {
let mut osm_db = crate::OSMDb::default(); use super::*;
let mut osm_db = OSMDb::default();
let mut node1 = OSMNode { let mut node1 = OSMNode {
id: 123, id: 123,

View File

@ -1,7 +1,6 @@
extern crate clap; extern crate clap;
mod lexicographic_file_walker; mod common;
mod osm;
use core::panic; use core::panic;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -12,11 +11,11 @@ use std::{env, fs};
use cheap_ruler::CheapRuler; use cheap_ruler::CheapRuler;
use clap::Parser; use clap::Parser;
use common::lexicographic_file_walker::LexicographicFileWalker;
use common::osm::{OSMDb, OSMNode, OSMWay};
use cucumber::{self, gherkin::Step, given, when, World}; use cucumber::{self, gherkin::Step, given, when, World};
use futures::{future, FutureExt}; use futures::{future, FutureExt};
use geo_types::{point, Point}; use geo_types::{point, Point};
use lexicographic_file_walker::LexicographicFileWalker;
use osm::{OSMDb, OSMNode, OSMWay};
#[derive(Debug, Default, World)] #[derive(Debug, Default, World)]
struct OSRMWorld { struct OSRMWorld {
@ -35,10 +34,10 @@ struct OSRMWorld {
impl OSRMWorld { impl OSRMWorld {
fn set_scenario_specific_paths_and_digests(&mut self, path: Option<PathBuf>) { fn set_scenario_specific_paths_and_digests(&mut self, path: Option<PathBuf>) {
self.feature_path = path.clone(); self.feature_path.clone_from(&path);
let file = File::open(path.clone().unwrap()) let file = File::open(path.clone().unwrap())
.expect(&format!("filesystem broken? can't open file {:?}", path)); .unwrap_or_else(|_| panic!("filesystem broken? can't open file {:?}", path));
self.feature_digest = chksum_md5::chksum(file) self.feature_digest = chksum_md5::chksum(file)
.expect("md5 could not be computed") .expect("md5 could not be computed")
.to_hex_lowercase(); .to_hex_lowercase();
@ -227,8 +226,8 @@ fn request_nearest(world: &mut OSRMWorld, step: &Step) {
// TODO: move to different file // TODO: move to different file
fn get_file_as_byte_vec(path: &PathBuf) -> Vec<u8> { fn get_file_as_byte_vec(path: &PathBuf) -> Vec<u8> {
println!("opening {path:?}"); println!("opening {path:?}");
let mut f = File::open(&path).expect("no file found"); let mut f = File::open(path).expect("no file found");
let metadata = fs::metadata(&path).expect("unable to read metadata"); let metadata = fs::metadata(path).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize]; let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow"); f.read(&mut buffer).expect("buffer overflow");
@ -306,9 +305,8 @@ fn main() {
"osrm_partition", "osrm_partition",
]; ];
let files: Vec<PathBuf> = fs::read_dir(&build_path) let files: Vec<PathBuf> = fs::read_dir(build_path)
.unwrap() .unwrap()
.into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
.map(|dir_entry| dir_entry.path()) .map(|dir_entry| dir_entry.path())
.collect(); .collect();
@ -323,8 +321,7 @@ fn main() {
.to_str() .to_str()
.unwrap() .unwrap()
.contains(name) .contains(name)
}) }).cloned()
.map(|e| e.clone())
.expect("file exists and is usable") .expect("file exists and is usable")
}); });
@ -346,7 +343,7 @@ fn main() {
for path_buf in dependencies { for path_buf in dependencies {
let data = get_file_as_byte_vec(&path_buf); let data = get_file_as_byte_vec(&path_buf);
if data.len() == 0 { if data.is_empty() {
continue; continue;
} }
md5.update(data); md5.update(data);
@ -374,7 +371,7 @@ fn main() {
_ => x, _ => x,
}) })
.collect::<String>() .collect::<String>()
.replace(r"\", "_") .replace('\\', "_")
.replace("__", "_") .replace("__", "_")
.replace("..", "."); .replace("..", ".");
s.truncate(64); s.truncate(64);