ftml/tree/
embed.rs

1/*
2 * tree/embed.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2025 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21use 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}