mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
// A `Convertible` is a style value from any front end (serde_json values, a
// borrowed JSON tree, feature values, ...): it borrows a
// `dyn ConversionTraits`, one impl per front end, and a front end's members
// are views into their parent, as serde_json's are.

use crate::feature::Value;

#[derive(Clone, Debug, Default, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct Error {
    pub message: String,
}

impl Error {
    pub fn new(message: impl Into<String>) -> Self {
        Error { message: message.into() }
    }
}

/// The per-front-end operations. `array_length`/`array_member` are meant
/// for arrays, `object_member`/`each_member` for objects.
pub(crate) trait ConversionTraits {
    /// Whether the value is undefined or a JSON null.
    fn is_undefined(&self) -> bool;

    fn is_array(&self) -> bool;
    fn array_length(&self) -> usize;
    fn array_member(&self, i: usize) -> Convertible<'_>;

    fn is_object(&self) -> bool;
    fn object_member(&self, name: &str) -> Option<Convertible<'_>>;
    /// Calls `f` once per key and value; stops at, and returns, the first
    /// error `f` returns.
    fn each_member(&self, f: &mut dyn FnMut(&str, &Convertible<'_>) -> Option<Error>) -> Option<Error>;

    fn to_bool(&self) -> Option<bool>;
    /// A JSON number as `float`.
    fn to_number(&self) -> Option<f32>;
    fn to_double(&self) -> Option<f64>;
    fn to_string(&self) -> Option<String>;
    /// A null, boolean, number or string as a `Value`; numbers prefer `u64`, then
    /// `i64`, then `f64`.
    fn to_value(&self) -> Option<Value>;

    /// Converts the value to GeoJSON. The JSON front end deserializes it
    /// with the `geojson` crate and reports its error message in `error`. A front
    /// end without a GeoJSON conversion reports an error (added by `sources`
    /// for the GeoJSON source).
    fn to_geojson(&self, error: &mut Error) -> Option<geojson::GeoJson> {
        error.message = "GeoJSON conversion is not supported by this value type".to_owned();
        None
    }
}

/// A type-erased, read-only view of a JSON-like value.
pub struct Convertible<'a> {
    value: &'a dyn ConversionTraits,
}

impl<'a> Convertible<'a> {
    /// Wraps a borrowed front-end value.
    pub(crate) fn new(value: &'a dyn ConversionTraits) -> Self {
        Convertible { value }
    }

    fn traits(&self) -> &dyn ConversionTraits {
        self.value
    }

    #[must_use]
    pub fn is_undefined(&self) -> bool {
        self.traits().is_undefined()
    }

    #[must_use]
    pub fn is_array(&self) -> bool {
        self.traits().is_array()
    }

    #[must_use]
    pub fn array_length(&self) -> usize {
        self.traits().array_length()
    }

    #[must_use]
    pub fn array_member(&self, i: usize) -> Convertible<'_> {
        self.traits().array_member(i)
    }

    #[must_use]
    pub fn is_object(&self) -> bool {
        self.traits().is_object()
    }

    #[must_use]
    pub fn object_member(&self, name: &str) -> Option<Convertible<'_>> {
        self.traits().object_member(name)
    }

    pub fn each_member(&self, mut f: impl FnMut(&str, &Convertible<'_>) -> Option<Error>) -> Option<Error> {
        self.traits().each_member(&mut f)
    }

    #[must_use]
    pub fn to_bool(&self) -> Option<bool> {
        self.traits().to_bool()
    }

    #[must_use]
    pub fn to_number(&self) -> Option<f32> {
        self.traits().to_number()
    }

    #[must_use]
    pub fn to_double(&self) -> Option<f64> {
        self.traits().to_double()
    }

    #[must_use]
    pub fn to_string(&self) -> Option<String> {
        self.traits().to_string()
    }

    #[must_use]
    pub fn to_value(&self) -> Option<Value> {
        self.traits().to_value()
    }

    pub fn to_geojson(&self, error: &mut Error) -> Option<geojson::GeoJson> {
        self.traits().to_geojson(error)
    }
}

impl std::fmt::Debug for Convertible<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Convertible")
    }
}

impl<'a, T: ConversionTraits> From<&'a T> for Convertible<'a> {
    fn from(value: &'a T) -> Self {
        Convertible::new(value)
    }
}

pub fn to_geojson(v: &Convertible<'_>, error: &mut Error) -> Option<geojson::GeoJson> {
    v.to_geojson(error)
}