use pyo3::prelude::*;
use pyo3::types::PyDict;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OEmbedEndpoint {
pub href: String,
pub format: OEmbedFormat,
pub title: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OEmbedFormat {
Json,
Xml,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OEmbedDiscovery {
pub json_endpoints: Vec<OEmbedEndpoint>,
pub xml_endpoints: Vec<OEmbedEndpoint>,
}
impl OEmbedDiscovery {
pub fn has_endpoints(&self) -> bool {
!self.json_endpoints.is_empty() || !self.xml_endpoints.is_empty()
}
pub fn preferred_json(&self) -> Option<&OEmbedEndpoint> {
self.json_endpoints.first()
}
pub fn preferred_xml(&self) -> Option<&OEmbedEndpoint> {
self.xml_endpoints.first()
}
}
impl OEmbedEndpoint {
pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
let dict = PyDict::new_bound(py);
dict.set_item("href", &self.href).unwrap();
dict.set_item(
"format",
match self.format {
OEmbedFormat::Json => "json",
OEmbedFormat::Xml => "xml",
},
)
.unwrap();
if let Some(ref title) = self.title {
dict.set_item("title", title).unwrap();
}
dict.unbind()
}
}
impl OEmbedDiscovery {
pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
let dict = PyDict::new_bound(py);
if !self.json_endpoints.is_empty() {
let json_eps: Vec<_> = self.json_endpoints.iter().map(|ep| ep.to_py_dict(py)).collect();
dict.set_item("json_endpoints", json_eps).unwrap();
}
if !self.xml_endpoints.is_empty() {
let xml_eps: Vec<_> = self.xml_endpoints.iter().map(|ep| ep.to_py_dict(py)).collect();
dict.set_item("xml_endpoints", xml_eps).unwrap();
}
dict.unbind()
}
}