#[derive(Debug, Clone, Default)]
pub struct ImageOptions {
pub size: Option<String>,
pub quality: Option<String>,
pub response_format: Option<String>,
pub n: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct ImageResult {
pub url: Option<String>,
pub b64_json: Option<String>,
}
impl ImageResult {
pub fn bytes(&self) -> Option<Vec<u8>> {
self.b64_json.as_ref().and_then(|encoded| {
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, encoded.trim()).ok()
})
}
}
#[derive(Debug, Clone)]
pub struct ImageResponse {
pub data: Vec<ImageResult>,
}
impl ImageResponse {
pub fn first(&self) -> Option<&ImageResult> {
self.data.first()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_image_result_bytes_decodes_b64() {
let result = ImageResult {
url: None,
b64_json: Some(base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
[1, 2, 3],
)),
};
assert_eq!(result.bytes().unwrap(), vec![1, 2, 3]);
assert!(ImageResult {
url: Some("https://example.test/a.png".into()),
b64_json: None,
}
.bytes()
.is_none());
}
#[test]
fn test_image_response_first() {
let response = ImageResponse {
data: vec![ImageResult {
url: Some("https://example.test/a.png".into()),
b64_json: None,
}],
};
assert_eq!(
response.first().unwrap().url.as_deref(),
Some("https://example.test/a.png")
);
assert!(ImageResponse { data: vec![] }.first().is_none());
}
}