mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use geo_types::{CoordNum, Geometry};

use super::value::{Value, ValueObject};

/// A feature's id: none, a number or a string.
#[derive(Clone, Debug, Default, PartialEq, mln_wire::Wire)]
pub enum Identifier {
    #[default]
    Null,
    Uint(u64),
    Int(i64),
    Double(f64),
    String(String),
}

impl Identifier {
    /// The id a feature takes from the value of its property that a source's
    /// `promoteId` names: a string or a number as it is, a boolean as 1 or 0,
    /// and none for anything else.
    #[must_use]
    pub fn promoted(value: Option<&Value>) -> Identifier {
        match value {
            Some(Value::String(s)) => Identifier::String(s.clone()),
            Some(Value::Uint(u)) => Identifier::Uint(*u),
            Some(Value::Int(i)) => Identifier::Int(*i),
            Some(Value::Double(d)) => Identifier::Double(*d),
            Some(Value::Bool(b)) => Identifier::Uint(u64::from(*b)),
            Some(Value::Null | Value::Array(_) | Value::Object(_)) | None => Identifier::Null,
        }
    }
}

pub type PropertyMap = ValueObject;

/// A feature: its geometry, properties and id.
///
/// `geometry` is `None` for a feature without one, as for a GeoJSON feature
/// whose geometry is `null`.
///
/// `P` holds the properties. Features that share one property map, as the
/// features a GeoJSON source's tiles cut from one source feature do, hold it
/// as an [`Arc<PropertyMap>`](std::sync::Arc).
#[derive(Clone, Debug, PartialEq, mln_wire::Wire)]
pub struct Feature<T: CoordNum = f64, P = PropertyMap> {
    pub geometry: Option<Geometry<T>>,
    pub properties: P,
    pub id: Identifier,
}

impl<T: CoordNum, P: Default> Default for Feature<T, P> {
    fn default() -> Self {
        Feature { geometry: None, properties: P::default(), id: Identifier::Null }
    }
}

impl<T: CoordNum> Feature<T> {
    pub fn new(geometry: impl Into<Geometry<T>>) -> Self {
        Feature { geometry: Some(geometry.into()), properties: PropertyMap::new(), id: Identifier::Null }
    }
}