1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
use crate::block::IMAGE_TYPE; use crate::composition::text::Text::Plain; use crate::composition::text::{PlainText, Text}; use serde::Serialize; #[derive(Debug, Serialize)] pub struct ImageBlock { #[serde(rename = "type")] type_name: &'static str, image_url: String, alt_text: String, #[serde(skip_serializing_if = "Option::is_none")] title: Option<Text>, #[serde(skip_serializing_if = "Option::is_none")] block_id: Option<String>, } impl ImageBlock { pub fn new(image_url: impl Into<String>, alt_text: impl Into<String>) -> Self { ImageBlock { type_name: IMAGE_TYPE, image_url: image_url.into(), alt_text: alt_text.into(), title: Option::default(), block_id: Option::default(), } } pub fn title(mut self, title: impl Into<PlainText>) -> Self { self.title = Some(Plain(title.into())); self } pub fn block_id(mut self, block_id: impl Into<String>) -> Self { self.block_id = Some(block_id.into()); self } } #[cfg(test)] mod test { use super::*; #[test] fn test_ser_new() { let image = ImageBlock::new("url", "alt"); let json = serde_json::to_string_pretty(&image).unwrap_or("".to_string()); let expected = r#"{ "type": "image", "image_url": "url", "alt_text": "alt" }"#; assert_eq!(json, expected); } #[test] fn test_ser_title() { let image = ImageBlock::new("url", "alt").title(PlainText::new("title")); let json = serde_json::to_string_pretty(&image).unwrap_or("".to_string()); let expected = r#"{ "type": "image", "image_url": "url", "alt_text": "alt", "title": { "type": "plain_text", "text": "title" } }"#; assert_eq!(json, expected); } #[test] fn test_ser_block_id() { let image = ImageBlock::new("url", "alt").block_id("id"); let json = serde_json::to_string_pretty(&image).unwrap_or("".to_string()); let expected = r#"{ "type": "image", "image_url": "url", "alt_text": "alt", "block_id": "id" }"#; assert_eq!(json, expected); } #[test] fn test_ser_all() { let image = ImageBlock::new("url", "alt") .title(PlainText::new("title")) .block_id("id"); let json = serde_json::to_string_pretty(&image).unwrap_or("".to_string()); let expected = r#"{ "type": "image", "image_url": "url", "alt_text": "alt", "title": { "type": "plain_text", "text": "title" }, "block_id": "id" }"#; assert_eq!(json, expected); } }