use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImageContent {
pub url: String,
}
impl ImageContent {
pub fn from_url(url: impl Into<String>) -> Self {
Self { url: url.into() }
}
pub fn from_base64(data: impl Into<String>) -> Self {
Self {
url: format!("data:image/png;base64,{}", data.into()),
}
}
pub fn from_base64_with_mime(data: impl Into<String>, mime: &str) -> Self {
Self {
url: format!("data:{};base64,{}", mime, data.into()),
}
}
pub fn is_base64(&self) -> bool {
self.url.starts_with("data:")
}
pub fn base64_data(&self) -> Option<&str> {
self.url
.split_once(',')
.filter(|(prefix, _)| prefix.contains("base64"))
.map(|(_, data)| data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_url() {
let img = ImageContent::from_url("https://example.com/image.jpg");
assert_eq!(img.url, "https://example.com/image.jpg");
assert!(!img.is_base64());
}
#[test]
fn test_from_base64() {
let img = ImageContent::from_base64("abc123");
assert!(img.is_base64());
assert_eq!(img.base64_data(), Some("abc123"));
}
#[test]
fn test_from_base64_with_mime() {
let img = ImageContent::from_base64_with_mime("xyz", "image/jpeg");
assert!(img.url.starts_with("data:image/jpeg;base64,"));
assert_eq!(img.base64_data(), Some("xyz"));
}
#[test]
fn test_url_not_base64() {
let img = ImageContent::from_url("https://example.com/img.png");
assert_eq!(img.base64_data(), None);
}
}