1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Deserialize)]
10pub struct Provider {
11 pub provider_name: String,
12 pub provider_url: String,
13 pub endpoints: Vec<Endpoint>,
14}
15
16#[derive(Debug, Deserialize)]
18pub struct Endpoint {
19 #[serde(default)]
20 pub schemes: Vec<String>,
21 pub url: String,
22 #[serde(default)]
23 pub discovery: bool,
24}
25
26#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
28#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
29#[serde(tag = "type")]
30pub enum EmbedType {
31 #[serde(rename = "photo")]
35 Photo(Photo),
36 #[serde(rename = "video")]
40 Video(Video),
41 #[serde(rename = "link")]
45 Link,
46 #[serde(rename = "rich")]
50 Rich(Rich),
51}
52
53#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
57#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
58pub struct Video {
59 pub html: String,
60 pub width: Option<i32>,
61 pub height: Option<i32>,
62}
63
64#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
68#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
69pub struct Photo {
70 pub url: String,
71 pub width: Option<i32>,
72 pub height: Option<i32>,
73}
74
75#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
79#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
80pub struct Rich {
81 pub html: String,
82 pub width: Option<i32>,
83 pub height: Option<i32>,
84}
85
86#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
90#[derive(Debug, Serialize, Deserialize, PartialEq)]
91pub struct EmbedResponse {
92 #[serde(flatten)]
93 pub oembed_type: EmbedType,
94 pub version: String,
95 pub title: Option<String>,
96 pub author_name: Option<String>,
97 pub author_url: Option<String>,
98 pub provider_name: Option<String>,
99 pub provider_url: Option<String>,
100 pub cache_age: Option<String>,
101 pub thumbnail_url: Option<String>,
102 pub thumbnail_width: Option<i32>,
103 pub thumbnail_height: Option<i32>,
104 #[serde(flatten)]
105 pub extra: HashMap<String, Value>,
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn test_photo() {
114 let input = r#"{
115 "type": "photo",
116 "version": "1.0",
117 "title": "photo",
118 "width": 100,
119 "height": 50,
120 "url": "https://example.com/photo.jpg"
121 }"#;
122 let response: EmbedResponse = serde_json::from_str(input).unwrap();
123
124 assert_eq!(response.title, Some("photo".to_string()));
125 assert_eq!(
126 response.oembed_type,
127 EmbedType::Photo(Photo {
128 url: "https://example.com/photo.jpg".to_string(),
129 width: Some(100),
130 height: Some(50)
131 })
132 )
133 }
134}