Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 9, 2024
1 parent 2a7b669 commit c0327e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
29 changes: 9 additions & 20 deletions osm2streets-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl PyStreetNetwork {
) -> PyResult<Self> {
SETUP_LOGGER.call_once(|| env_logger::init());

let input: ImportOptions =
serde_json::from_str(input.extract::<&str>(py)?).map_err(|e| err_to_py_value(format!("Failed to parse input: {}", e)))?;
let input: ImportOptions = serde_json::from_str(input.extract::<&str>(py)?)
.map_err(|e| err_to_py_value(format!("Failed to parse input: {}", e)))?;

// Parse clip points if provided
let clip_pts = if clip_pts_geojson.is_empty() {
Expand Down Expand Up @@ -230,19 +230,15 @@ impl PyStreetNetwork {
if let Some(ref way) = self.ways.get(&osm::WayID(id)) {
Ok(serde_json::to_string_pretty(&way.tags).unwrap())
} else {
Err(err_to_py_value(format!(
"unknown way {}",
id
)))
Err(err_to_py_value(format!("unknown way {}", id)))
}
}

/// Converts the entire `StreetNetwork` to a JSON format.
///
/// Returns a JSON string representing the full `StreetNetwork` data structure.
pub fn to_json(&self) -> PyResult<String> {
serde_json::to_string_pretty(&self.inner)
.map_err(err_to_py_runtime)
serde_json::to_string_pretty(&self.inner).map_err(err_to_py_runtime)
}

/// Retrieves the geometry of a way (road or path) as a buffered polygon in GeoJSON format.
Expand Down Expand Up @@ -293,10 +289,7 @@ impl PyStreetNetwork {
/// Returns an XML string for the way, or an error if the way does not exist.
pub fn way_to_xml(&self, id: i64) -> PyResult<String> {
let Some(ref way) = self.ways.get(&osm::WayID(id)) else {
return Err(err_to_py_value(format!(
"unknown way {}",
id
)));
return Err(err_to_py_value(format!("unknown way {}", id)));
};
let mut out = format!(r#"<way id="{}""#, id);
if let Some(version) = way.version {
Expand Down Expand Up @@ -359,8 +352,8 @@ impl PyStreetNetwork {
/// Updates the roads and intersections connected to this way based on the new tags.
pub fn overwrite_osm_tags_for_way(&mut self, id: i64, tags: &str) -> PyResult<()> {
let id = osm::WayID(id);
let tags: Tags = serde_json::from_str(tags).map_err(|e| err_to_py_value(format!("Failed to parse tags: {}", e))
)?;
let tags: Tags = serde_json::from_str(tags)
.map_err(|e| err_to_py_value(format!("Failed to parse tags: {}", e)))?;

let mut intersections = BTreeSet::new();
for road in self.inner.roads.values_mut() {
Expand All @@ -382,10 +375,7 @@ impl PyStreetNetwork {
if let Some(way) = self.ways.get_mut(&id) {
way.tags = tags;
} else {
return Err(err_to_py_value(format!(
"Unknown way ID {}",
id
)));
return Err(err_to_py_value(format!("Unknown way ID {}", id)));
}
Ok(())
}
Expand Down Expand Up @@ -458,9 +448,8 @@ fn err_to_py_runtime<E: std::fmt::Display>(err: E) -> PyErr {
pyo3::exceptions::PyRuntimeError::new_err(err.to_string())
}


/// Converts any error implementing `std::fmt::Display` into a `PyValueError`.
/// Used for invalid inputs or incorrect arguments passed by the user.
fn err_to_py_value<E: std::fmt::Display>(err: E) -> PyErr {
pyo3::exceptions::PyValueError::new_err(err.to_string())
}
}
14 changes: 9 additions & 5 deletions streets_reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ pub fn detect_country_code(streets: &mut StreetNetwork) {
let codes = geocoder.ids(country_boundaries::LatLon::new(pt.y(), pt.x()).unwrap());

// Use the most specific country code for which we know a driving side.
let Some((code, side)) = codes.into_iter().find_map(|code| driving_side(code).map(|s| (code, s))) else {
let Some((code, side)) = codes
.into_iter()
.find_map(|code| driving_side(code).map(|s| (code, s)))
else {
error!("detect_country_code failed -- {:?} didn't match to any country. Driving side may be wrong!", pt);
return;
};
Expand All @@ -85,10 +88,11 @@ fn extract_osm(
timer,
)?;
// If GPSBounds aren't provided, they'll be computed in the Document
// LB 241209 - Added proper error handling due to processing errors, so it doesn't stop processing.
streets.gps_bounds = doc.gps_bounds.clone().ok_or_else(|| {
anyhow::anyhow!("Failed to extract GPS bounds from the OSM input")
})?;
// LB 241209 - Added proper error handling due to processing errors, so it doesn't stop processing.
streets.gps_bounds = doc
.gps_bounds
.clone()
.ok_or_else(|| anyhow::anyhow!("Failed to extract GPS bounds from the OSM input"))?;

if let Some(pts) = clip_pts {
streets.boundary_polygon =
Expand Down

0 comments on commit c0327e9

Please sign in to comment.