Skip to main content

mln_style/conversion/
convertible.rs

1// A `Convertible` is a style value from any front end (serde_json values, a
2// borrowed JSON tree, feature values, ...): it borrows a
3// `dyn ConversionTraits`, one impl per front end, and a front end's members
4// are views into their parent, as serde_json's are.
5
6use crate::feature::Value;
7
8#[derive(Clone, Debug, Default, PartialEq, Eq, thiserror::Error)]
9#[error("{message}")]
10pub struct Error {
11    pub message: String,
12}
13
14impl Error {
15    pub fn new(message: impl Into<String>) -> Self {
16        Error { message: message.into() }
17    }
18}
19
20/// The per-front-end operations. `array_length`/`array_member` are meant
21/// for arrays, `object_member`/`each_member` for objects.
22pub(crate) trait ConversionTraits {
23    /// Whether the value is undefined or a JSON null.
24    fn is_undefined(&self) -> bool;
25
26    fn is_array(&self) -> bool;
27    fn array_length(&self) -> usize;
28    fn array_member(&self, i: usize) -> Convertible<'_>;
29
30    fn is_object(&self) -> bool;
31    fn object_member(&self, name: &str) -> Option<Convertible<'_>>;
32    /// Calls `f` once per key and value; stops at, and returns, the first
33    /// error `f` returns.
34    fn each_member(&self, f: &mut dyn FnMut(&str, &Convertible<'_>) -> Option<Error>) -> Option<Error>;
35
36    fn to_bool(&self) -> Option<bool>;
37    /// A JSON number as `float`.
38    fn to_number(&self) -> Option<f32>;
39    fn to_double(&self) -> Option<f64>;
40    fn to_string(&self) -> Option<String>;
41    /// A null, boolean, number or string as a `Value`; numbers prefer `u64`, then
42    /// `i64`, then `f64`.
43    fn to_value(&self) -> Option<Value>;
44
45    /// Converts the value to GeoJSON. The JSON front end deserializes it
46    /// with the `geojson` crate and reports its error message in `error`. A front
47    /// end without a GeoJSON conversion reports an error (added by `sources`
48    /// for the GeoJSON source).
49    fn to_geojson(&self, error: &mut Error) -> Option<geojson::GeoJson> {
50        error.message = "GeoJSON conversion is not supported by this value type".to_owned();
51        None
52    }
53}
54
55/// A type-erased, read-only view of a JSON-like value.
56pub struct Convertible<'a> {
57    value: &'a dyn ConversionTraits,
58}
59
60impl<'a> Convertible<'a> {
61    /// Wraps a borrowed front-end value.
62    pub(crate) fn new(value: &'a dyn ConversionTraits) -> Self {
63        Convertible { value }
64    }
65
66    fn traits(&self) -> &dyn ConversionTraits {
67        self.value
68    }
69
70    #[must_use]
71    pub fn is_undefined(&self) -> bool {
72        self.traits().is_undefined()
73    }
74
75    #[must_use]
76    pub fn is_array(&self) -> bool {
77        self.traits().is_array()
78    }
79
80    #[must_use]
81    pub fn array_length(&self) -> usize {
82        self.traits().array_length()
83    }
84
85    #[must_use]
86    pub fn array_member(&self, i: usize) -> Convertible<'_> {
87        self.traits().array_member(i)
88    }
89
90    #[must_use]
91    pub fn is_object(&self) -> bool {
92        self.traits().is_object()
93    }
94
95    #[must_use]
96    pub fn object_member(&self, name: &str) -> Option<Convertible<'_>> {
97        self.traits().object_member(name)
98    }
99
100    pub fn each_member(&self, mut f: impl FnMut(&str, &Convertible<'_>) -> Option<Error>) -> Option<Error> {
101        self.traits().each_member(&mut f)
102    }
103
104    #[must_use]
105    pub fn to_bool(&self) -> Option<bool> {
106        self.traits().to_bool()
107    }
108
109    #[must_use]
110    pub fn to_number(&self) -> Option<f32> {
111        self.traits().to_number()
112    }
113
114    #[must_use]
115    pub fn to_double(&self) -> Option<f64> {
116        self.traits().to_double()
117    }
118
119    #[must_use]
120    pub fn to_string(&self) -> Option<String> {
121        self.traits().to_string()
122    }
123
124    #[must_use]
125    pub fn to_value(&self) -> Option<Value> {
126        self.traits().to_value()
127    }
128
129    pub fn to_geojson(&self, error: &mut Error) -> Option<geojson::GeoJson> {
130        self.traits().to_geojson(error)
131    }
132}
133
134impl std::fmt::Debug for Convertible<'_> {
135    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136        f.write_str("Convertible")
137    }
138}
139
140impl<'a, T: ConversionTraits> From<&'a T> for Convertible<'a> {
141    fn from(value: &'a T) -> Self {
142        Convertible::new(value)
143    }
144}
145
146pub fn to_geojson(v: &Convertible<'_>, error: &mut Error) -> Option<geojson::GeoJson> {
147    v.to_geojson(error)
148}