1use maplibre_native_base::{Value as MbglValue, ValueObject};
5use mln_conversion::{Convertible, Error};
6
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
11pub struct Image {
12 image_id: String,
13 available: bool,
14}
15
16impl Image {
17 pub fn new(image_id: impl Into<String>) -> Self {
18 Image::with_available(image_id, false)
19 }
20
21 pub fn with_available(image_id: impl Into<String>, available: bool) -> Self {
22 Image {
23 image_id: image_id.into(),
24 available,
25 }
26 }
27
28 pub fn to_value(&self) -> MbglValue {
29 let mut object = ValueObject::new();
30 object.insert("name".to_owned(), MbglValue::String(self.image_id.clone()));
31 object.insert("available".to_owned(), MbglValue::Bool(self.available));
32 MbglValue::Object(object)
33 }
34
35 pub fn id(&self) -> &str {
36 &self.image_id
37 }
38
39 pub fn is_available(&self) -> bool {
40 self.available
41 }
42
43 pub fn empty(&self) -> bool {
44 self.image_id.is_empty()
45 }
46}
47
48pub fn convert_image(value: &Convertible<'_>, error: &mut Error) -> Option<Image> {
50 if value.is_array() {
51 let image_parameters = value.array_member(0);
52 let image_parameters_length = image_parameters.array_length();
53 if image_parameters_length < 1 {
54 error.message = "Image has to contain an ID.".to_owned();
55 return None;
56 }
57
58 let Some(image_id) = image_parameters.array_member(0).to_string() else {
59 error.message = "Image has to contain an ID.".to_owned();
60 return None;
61 };
62
63 Some(Image::with_available(image_id, false))
64 } else if let Some(result) = value.to_string() {
65 Some(Image::with_available(result, false))
66 } else {
67 error.message = "Image must be plain string or array type.".to_owned();
68 None
69 }
70}