use crate::{
JsonValue,
ui::{AsyncCx, ImageSize, JsValue, PropError, TsTypes, module},
};
pub(crate) struct Image;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ImageUri(String);
impl JsValue for ImageUri {
fn ts_type(_types: &mut TsTypes) -> String {
"string | { uri: string }".into()
}
fn from_json(value: JsonValue) -> Result<Self, PropError> {
match &value {
JsonValue::String(uri) => Ok(Self(uri.clone())),
_ => value
.get("uri")
.and_then(JsonValue::as_str)
.map(|uri| Self(uri.to_owned()))
.ok_or_else(|| PropError::expected("a URI or { uri }", &value)),
}
}
fn to_json(&self) -> JsonValue {
JsonValue::String(self.0.clone())
}
}
#[module]
impl Image {
pub(crate) async fn get_size(source: ImageUri, cx: AsyncCx) -> Result<ImageSize, String> {
cx.image_size(source.0).await
}
pub(crate) async fn prefetch(source: ImageUri, cx: AsyncCx) -> bool {
cx.prefetch_image(source.0).await
}
}