1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::fmt;
4use std::ops::Deref;
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8pub struct Segment {
9 #[serde(rename = "type")]
10 pub type_: String,
11 pub data: Value,
12}
13
14impl Segment {
15 pub fn new(type_: &str, data: Value) -> Self {
16 Segment {
17 type_: type_.to_string(),
18 data,
19 }
20 }
21
22 pub fn get_str(&self) -> Option<String> {
23 match self.type_.as_str() {
24 "text" => self
25 .data
26 .get("text")
27 .and_then(Value::as_str)
28 .map(String::from),
29 "image" => self
30 .data
31 .get("file")
32 .and_then(Value::as_str)
33 .map(String::from),
34 _ => None,
35 }
36 }
37}
38
39#[derive(Debug, Clone)]
41pub struct Segments(pub Vec<Segment>);
42
43impl Deref for Segments {
44 type Target = Vec<Segment>;
45 fn deref(&self) -> &Self::Target {
46 &self.0
47 }
48}
49
50impl fmt::Display for Segments {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 let str = self
53 .iter()
54 .filter_map(|s| s.get_str())
55 .collect::<Vec<_>>()
56 .join("");
57 write!(f, "{}", str)
58 }
59}