Skip to content

Commit

Permalink
Input file can be any AsRef<Path> to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristramg committed Sep 12, 2024
1 parent 4afc385 commit 166a589
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "osm4routing"
edition = "2021"
version = "0.6.5"
version = "0.7.0"
authors = ["Tristram Gräbener <[email protected]>"]
description = "Convert OpenStreetMap data into routing friendly CSV"
homepage = "https://github.com/Tristramg/osm4routing2"
Expand Down
36 changes: 19 additions & 17 deletions src/osm4routing/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::categorize::*;
use super::models::*;
use osmpbfreader::objects::{NodeId, WayId};
use std::collections::{HashMap, HashSet};
use std::path::Path;

// Way as represented in OpenStreetMap
struct Way {
Expand Down Expand Up @@ -259,11 +260,10 @@ impl Reader {
.collect()
}

pub fn read(&mut self, filename: &str) -> Result<(Vec<Node>, Vec<Edge>), String> {
let path = std::path::Path::new(filename);
let file = std::fs::File::open(path).map_err(|e| e.to_string())?;
pub fn read<P: AsRef<Path>>(&mut self, filename: &P) -> Result<(Vec<Node>, Vec<Edge>), String> {
let file = std::fs::File::open(filename).map_err(|e| e.to_string())?;
self.read_ways(file);
let file_nodes = std::fs::File::open(path).map_err(|e| e.to_string())?;
let file_nodes = std::fs::File::open(filename).map_err(|e| e.to_string())?;
self.read_nodes(file_nodes);
self.count_nodes_uses();

Expand All @@ -277,13 +277,13 @@ impl Reader {
}

// Read all the nodes and ways of the osm.pbf file
pub fn read(filename: &str) -> Result<(Vec<Node>, Vec<Edge>), String> {
pub fn read<P: AsRef<Path>>(filename: &P) -> Result<(Vec<Node>, Vec<Edge>), String> {
Reader::new().read(filename)
}

#[test]
fn test_real_all() {
let (nodes, ways) = read("src/osm4routing/test_data/minimal.osm.pbf").unwrap();
let (nodes, ways) = read(&"src/osm4routing/test_data/minimal.osm.pbf").unwrap();
assert_eq!(2, nodes.len());
assert_eq!(1, ways.len());
}
Expand Down Expand Up @@ -341,15 +341,15 @@ fn test_split() {

#[test]
fn test_wrong_file() {
let r = read("i hope you have no file name like this one");
let r = read(&"i hope you have no file name like this one");
assert!(r.is_err());
}

#[test]
fn forbidden_values() {
let (_, ways) = Reader::new()
.reject("highway", "secondary")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(0, ways.len());
}
Expand All @@ -358,15 +358,17 @@ fn forbidden_values() {
fn forbidden_wildcard() {
let (_, ways) = Reader::new()
.reject("highway", "*")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(0, ways.len());
}

#[test]
fn way_of_node() {
let mut r = Reader::new();
let (_nodes, edges) = r.read("src/osm4routing/test_data/minimal.osm.pbf").unwrap();
let (_nodes, edges) = r
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();

assert_eq!(2, edges[0].nodes.len());
}
Expand All @@ -375,7 +377,7 @@ fn way_of_node() {
fn read_tags() {
let (_nodes, edges) = Reader::new()
.read_tag("highway")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();

assert_eq!("secondary", edges[0].tags.get("highway").unwrap());
Expand All @@ -385,7 +387,7 @@ fn read_tags() {
fn require_value_ok() {
let (_, ways) = Reader::new()
.require("highway", "secondary")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(1, ways.len());
}
Expand All @@ -394,7 +396,7 @@ fn require_value_ok() {
fn require_value_missing() {
let (_, ways) = Reader::new()
.require("highway", "primary")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(0, ways.len());
}
Expand All @@ -403,7 +405,7 @@ fn require_value_missing() {
fn require_wildcart() {
let (_, ways) = Reader::new()
.require("highway", "*")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(1, ways.len());
}
Expand All @@ -413,21 +415,21 @@ fn require_multiple_tags() {
let (_, ways) = Reader::new()
.require("highway", "primary")
.require("highway", "secondary")
.read("src/osm4routing/test_data/minimal.osm.pbf")
.read(&"src/osm4routing/test_data/minimal.osm.pbf")
.unwrap();
assert_eq!(1, ways.len());
}

#[test]
fn merging_edges() {
let (_nodes, edges) = Reader::new()
.read("src/osm4routing/test_data/ways_to_merge.osm.pbf")
.read(&"src/osm4routing/test_data/ways_to_merge.osm.pbf")
.unwrap();
assert_eq!(2, edges.len());

let (_nodes, edges) = Reader::new()
.merge_ways()
.read("src/osm4routing/test_data/ways_to_merge.osm.pbf")
.read(&"src/osm4routing/test_data/ways_to_merge.osm.pbf")
.unwrap();
assert_eq!(1, edges.len());
}

0 comments on commit 166a589

Please sign in to comment.