1use super::clone::string_to_owned;
22use std::borrow::Cow;
23
24#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
25#[serde(rename_all = "kebab-case", tag = "embed", content = "data")]
26pub enum Embed<'t> {
27 #[serde(rename_all = "kebab-case")]
28 Youtube { video_id: Cow<'t, str> },
29
30 #[serde(rename_all = "kebab-case")]
31 Vimeo { video_id: Cow<'t, str> },
32
33 GithubGist {
34 username: Cow<'t, str>,
35 hash: Cow<'t, str>,
36 },
37
38 #[serde(rename_all = "kebab-case")]
39 GitlabSnippet { snippet_id: Cow<'t, str> },
40}
41
42impl Embed<'_> {
43 pub fn name(&self) -> &'static str {
44 match self {
45 Embed::Youtube { .. } => "YouTube",
46 Embed::Vimeo { .. } => "Vimeo",
47 Embed::GithubGist { .. } => "GithubGist",
48 Embed::GitlabSnippet { .. } => "GitlabSnippet",
49 }
50 }
51
52 pub fn direct_url(&self) -> String {
53 match self {
54 Embed::Youtube { video_id } => format!("https://youtu.be/{video_id}"),
55 Embed::Vimeo { video_id } => format!("https://vimeo.com/{video_id}"),
56 Embed::GithubGist { username, hash } => {
57 format!("https://gist.github.com/{username}/{hash}")
58 }
59 Embed::GitlabSnippet { snippet_id } => {
60 format!("https://gitlab.com/-/snippets/{snippet_id}")
61 }
62 }
63 }
64
65 pub fn to_owned(&self) -> Embed<'static> {
66 match self {
67 Embed::Youtube { video_id } => Embed::Youtube {
68 video_id: string_to_owned(video_id),
69 },
70
71 Embed::Vimeo { video_id } => Embed::Vimeo {
72 video_id: string_to_owned(video_id),
73 },
74
75 Embed::GithubGist { username, hash } => Embed::GithubGist {
76 username: string_to_owned(username),
77 hash: string_to_owned(hash),
78 },
79
80 Embed::GitlabSnippet { snippet_id } => Embed::GitlabSnippet {
81 snippet_id: string_to_owned(snippet_id),
82 },
83 }
84 }
85}