use serde_json::Value;
use crate::core::voiceflow::dialog_blocks::traits::FromValue;
use crate::errors::{VoiceflousionError, VoiceflousionResult};
#[derive(Debug, Clone)]
pub struct VoiceflowImage {
url: String,
height: Option<u64>,
width: Option<u64>,
}
impl VoiceflowImage {
pub fn new(url: String, height: Option<u64>, width: Option<u64>) -> Self {
Self {
url,
height,
width,
}
}
pub fn url(&self) -> &String {
&self.url
}
pub fn height(&self) -> Option<u64> {
self.height
}
pub fn width(&self) -> Option<u64> {
self.width
}
}
impl FromValue for VoiceflowImage {
fn from_value(value: &Value) -> VoiceflousionResult<Option<Self>> {
let payload = value["trace"].get("payload")
.ok_or_else(|| VoiceflousionError::VoiceflowBlockConvertationError(
"VoiceflowImage image payload".to_string(),
value.clone()
))?;
let url = payload.get("image")
.and_then(|image| image.as_str())
.ok_or_else(|| VoiceflousionError::VoiceflowBlockConvertationError(
"VoiceflowImage image url".to_string(),
value.clone()
))?
.to_string();
let height = payload["dimensions"].get("height")
.and_then(|height| height.as_u64());
let width = payload["dimensions"].get("width")
.and_then(|width| width.as_u64());
if url.is_empty() {
return Ok(None);
}
Ok(Some(Self::new(url, height, width)))
}
}