use serde::{Deserialize, Serialize};
use crate::types::{Base64ImageSource, CacheControlEphemeral, FileSource, UrlImageSource};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ImageSource {
#[serde(rename = "base64")]
Base64(Base64ImageSource),
#[serde(rename = "url")]
Url(UrlImageSource),
#[serde(rename = "file")]
File(FileSource),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImageBlock {
pub source: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_control: Option<CacheControlEphemeral>,
}
impl ImageBlock {
pub fn new(source: ImageSource) -> Self {
Self { source, cache_control: None }
}
pub fn new_with_base64(source: Base64ImageSource) -> Self {
Self::new(ImageSource::Base64(source))
}
pub fn new_with_url(source: UrlImageSource) -> Self {
Self::new(ImageSource::Url(source))
}
pub fn with_cache_control(mut self, cache_control: CacheControlEphemeral) -> Self {
self.cache_control = Some(cache_control);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::base64_image_source::ImageMediaType;
use serde_json::{json, to_value};
#[test]
fn image_block_with_base64() {
let base64_source = Base64ImageSource::new(
"data:image/jpeg;base64,SGVsbG8gd29ybGQ=".to_string(),
ImageMediaType::Jpeg,
);
let image_block = ImageBlock::new_with_base64(base64_source);
let json = to_value(&image_block).unwrap();
assert_eq!(
json,
json!({
"source": {
"type": "base64",
"data": "data:image/jpeg;base64,SGVsbG8gd29ybGQ=",
"media_type": "image/jpeg"
}
})
);
}
#[test]
fn image_block_with_url() {
let url_source = UrlImageSource::new("https://example.com/image.jpg".to_string());
let image_block = ImageBlock::new_with_url(url_source);
let json = to_value(&image_block).unwrap();
assert_eq!(
json,
json!({
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
}
})
);
}
#[test]
fn image_block_with_cache_control() {
let url_source = UrlImageSource::new("https://example.com/image.jpg".to_string());
let cache_control = CacheControlEphemeral::new();
let image_block = ImageBlock::new_with_url(url_source).with_cache_control(cache_control);
let json = to_value(&image_block).unwrap();
assert_eq!(
json,
json!({
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
},
"cache_control": {
"type": "ephemeral"
}
})
);
}
}