use feature::{Value as MbglValue, ValueObject};
use mln_conversion::{Convertible, Error};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Image {
image_id: String,
available: bool,
}
impl Image {
pub fn new(image_id: impl Into<String>) -> Self {
Image::with_available(image_id, false)
}
pub fn with_available(image_id: impl Into<String>, available: bool) -> Self {
Image { image_id: image_id.into(), available }
}
pub fn to_value(&self) -> MbglValue {
let mut object = ValueObject::new();
object.insert("name".to_owned(), MbglValue::String(self.image_id.clone()));
object.insert("available".to_owned(), MbglValue::Bool(self.available));
MbglValue::Object(object)
}
pub fn id(&self) -> &str {
&self.image_id
}
pub fn is_available(&self) -> bool {
self.available
}
pub fn empty(&self) -> bool {
self.image_id.is_empty()
}
}
pub fn convert_image(value: &Convertible<'_>, error: &mut Error) -> Option<Image> {
if value.is_array() {
let image_parameters = value.array_member(0);
let image_parameters_length = image_parameters.array_length();
if image_parameters_length < 1 {
error.message = "Image has to contain an ID.".to_owned();
return None;
}
let Some(image_id) = image_parameters.array_member(0).to_string() else {
error.message = "Image has to contain an ID.".to_owned();
return None;
};
Some(Image::with_available(image_id, false))
} else if let Some(result) = value.to_string() {
Some(Image::with_available(result, false))
} else {
error.message = "Image must be plain string or array type.".to_owned();
None
}
}