Skip to content

Commit

Permalink
core: move Property from module builder to lrs
Browse files Browse the repository at this point in the history
Signed-off-by: Tristram Gräbener <[email protected]>
  • Loading branch information
Tristramg committed Dec 2, 2024
1 parent 9ce2dbd commit 23b173a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use std::path::PathBuf;

use liblrs::lrs::LrmHandle;
use liblrs::lrs::{LrsBase, Properties};
use liblrs::lrs_ext::*;
use liblrs::{builder::Properties, lrs::LrsBase};
use pyo3::{exceptions::PyTypeError, prelude::*};

/// Holds the whole Linear Referencing System.
Expand Down
25 changes: 2 additions & 23 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,11 @@ use geo::{Coord, Distance};

use crate::curves::{Curve, CurveError, CurveProjection, SphericalLineStringCurve};

use crate::lrs::Properties;
use crate::lrs_ext::ExtLrs;
use crate::lrs_generated::{self, *};
use crate::osm_helpers::sort_edges;

/// A key-value `HashMap` to add metadata to the objects.
pub type Properties = std::collections::HashMap<String, String>;

#[macro_export]
/// Build a properties map:
/// `properties!("source" => "openstreetmap", "license" => "ODbL")`.
macro_rules! properties {
($($k:expr => $v:expr),* $(,)?) => {{
core::convert::From::from([$(($k.to_owned(), $v.to_owned()),)*])
}};
}

/// Builds a [`Properties`] from a FlatBuffer vector of Property
///
/// Implementation note: as [`Properties`] is just an alias, we cannot `impl` for it (e.g. Into)
pub fn from_fb(properties: Option<Vector<ForwardsUOffset<lrs_generated::Property>>>) -> Properties {
properties
.unwrap_or_default()
.iter()
.map(|property| (property.key().to_string(), property.value().to_string()))
.collect()
}
use crate::properties;

/// The linear position of an [`Anchor`] doesn’t always match the measured distance.
/// For example if a road was transformed into a bypass, resulting in a longer road,
Expand Down
2 changes: 1 addition & 1 deletion src/lrm_scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use geo::Point;
use thiserror::Error;

use crate::builder::Properties;
use crate::lrs::Properties;

/// Measurement along the `Curve`. Typically in meters.
pub type CurvePosition = f64;
Expand Down
25 changes: 24 additions & 1 deletion src/lrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ extern crate flatbuffers;

use std::cmp::Ordering;

use flatbuffers::{ForwardsUOffset, Vector};
use geo::orient::Direction;
use thiserror::Error;

use crate::builder::{from_fb, Properties};
use crate::curves::{Curve, CurveError};
use crate::lrm_scale::{
Anchor, CurvePosition, LrmScale, LrmScaleError, LrmScaleMeasure, ScalePosition,
Expand Down Expand Up @@ -620,6 +620,29 @@ impl<CurveImpl: Curve> Lrs<CurveImpl> {
}
}

/// A key-value `HashMap` to add metadata to the objects.
pub type Properties = std::collections::HashMap<String, String>;

#[macro_export]
/// Build a properties map:
/// `properties!("source" => "openstreetmap", "licence" => "ODbL")`.
macro_rules! properties {
($($k:expr => $v:expr),* $(,)?) => {{
core::convert::From::from([$(($k.to_owned(), $v.to_owned()),)*])
}};
}

/// Builds a [`Properties`] from a FlatBuffer vector of Property
///
/// Implementation note: as [`Properties`] is just an alias, we cannot `impl` for it (e.g. Into)
pub fn from_fb(properties: Option<Vector<ForwardsUOffset<lrs_generated::Property>>>) -> Properties {
properties
.unwrap_or_default()
.iter()
.map(|property| (property.key().to_string(), property.value().to_string()))
.collect()
}

#[cfg(test)]
mod tests {
use approx::assert_relative_eq;
Expand Down
2 changes: 1 addition & 1 deletion src/lrs_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use geo::{Coord, Point};

use crate::builder::Properties;
use crate::curves::{Curve, SphericalLineStringCurve};
use crate::lrm_scale::Anchor;
use crate::lrm_scale::LrmScaleMeasure;
use crate::lrs::Properties;
use crate::lrs::{self, TraversalPosition};
use crate::lrs::{LrsBase, LrsError};

Expand Down

0 comments on commit 23b173a

Please sign in to comment.