Skip to main content

mln_style/expression/
image.rs

1use crate::conversion::{Convertible, Error};
2use crate::feature::{Value as FeatureValue, ValueObject};
3
4/// An image ID plus whether it is available.
5#[derive(Clone, Debug, Default, PartialEq, Eq, mln_wire::Wire)]
6pub struct Image {
7    image_id: String,
8    available: bool,
9}
10
11impl Image {
12    pub fn new(image_id: impl Into<String>) -> Self {
13        Image::with_available(image_id, false)
14    }
15
16    pub(crate) fn with_available(image_id: impl Into<String>, available: bool) -> Self {
17        Image { image_id: image_id.into(), available }
18    }
19
20    #[must_use]
21    pub fn to_value(&self) -> FeatureValue {
22        let mut object = ValueObject::new();
23        object.insert("name".to_owned(), FeatureValue::String(self.image_id.clone()));
24        object.insert("available".to_owned(), FeatureValue::Bool(self.available));
25        FeatureValue::Object(object)
26    }
27
28    #[must_use]
29    pub fn id(&self) -> &str {
30        &self.image_id
31    }
32
33    #[must_use]
34    pub fn is_available(&self) -> bool {
35        self.available
36    }
37
38    #[must_use]
39    pub fn is_empty(&self) -> bool {
40        self.image_id.is_empty()
41    }
42}
43
44/// Converts an image ID. An array is an expression, which the property value
45/// conversion parses before this.
46pub fn convert_image(value: &Convertible<'_>, error: &mut Error) -> Option<Image> {
47    if let Some(result) = value.to_string() {
48        Some(Image::new(result))
49    } else {
50        error.message = "value must be a string".to_owned();
51        None
52    }
53}
54
55/// Images don't interpolate: the result is `self`.
56impl crate::interpolate::Interpolate for Image {
57    fn interpolate(&self, _to: &Self, _t: f64) -> Self {
58        self.clone()
59    }
60}