use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WireContentBlock {
#[serde(default, rename = "type")]
pub content_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub uri: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resource: Option<Value>,
#[serde(flatten)]
pub extra: Value,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_wire_content_block_text() {
let raw = json!({
"type": "text",
"text": "Hello, world!"
});
let block: WireContentBlock =
serde_json::from_value(raw).expect("deserialize text block");
assert_eq!(block.content_type, "text");
assert_eq!(block.text.as_deref(), Some("Hello, world!"));
assert!(block.data.is_none());
assert!(block.mime_type.is_none());
let back = serde_json::to_value(&block).expect("serialize text block");
assert_eq!(back["type"], "text");
assert_eq!(back["text"], "Hello, world!");
}
#[test]
fn test_wire_content_block_image() {
let raw = json!({
"type": "image",
"data": "iVBORw0KGgo=",
"mimeType": "image/png"
});
let block: WireContentBlock =
serde_json::from_value(raw).expect("deserialize image block");
assert_eq!(block.content_type, "image");
assert_eq!(block.data.as_deref(), Some("iVBORw0KGgo="));
assert_eq!(block.mime_type.as_deref(), Some("image/png"));
assert!(block.text.is_none());
let back = serde_json::to_value(&block).expect("serialize image block");
assert_eq!(back["type"], "image");
assert_eq!(back["data"], "iVBORw0KGgo=");
assert_eq!(back["mimeType"], "image/png");
}
#[test]
fn test_wire_content_block_unknown_type_preserved() {
let raw = json!({
"type": "future_type",
"someNewField": 42
});
let block: WireContentBlock =
serde_json::from_value(raw.clone()).expect("deserialize unknown block");
assert_eq!(block.content_type, "future_type");
let back = serde_json::to_value(&block).expect("serialize unknown block");
assert_eq!(back["type"], "future_type");
assert_eq!(back["someNewField"], 42, "extra fields must be preserved");
}
#[test]
fn test_wire_content_block_default_is_empty() {
let block = WireContentBlock::default();
assert_eq!(block.content_type, "");
assert!(block.text.is_none());
assert!(block.data.is_none());
}
}