ftml/tree/anchor.rs
1/*
2 * tree/anchor.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 std::convert::TryFrom;
22use strum_macros::IntoStaticStr;
23
24#[derive(
25 Serialize, Deserialize, IntoStaticStr, Debug, Copy, Clone, Hash, PartialEq, Eq,
26)]
27#[serde(rename_all = "kebab-case")]
28pub enum AnchorTarget {
29 /// Open the link in a new tab.
30 /// HTML attribute is `_blank`.
31 NewTab,
32
33 /// Open the link in the parent frame.
34 /// HTML attribute is `_parent`.
35 Parent,
36
37 /// Open the link in the top-most frame.
38 /// HTML attribute is `_top`.
39 Top,
40
41 /// Open the link in the current frame.
42 /// HTML attribute is `_self`.
43 /// This is the default setting, so the "anchor" field does not need to be included.
44 Same,
45}
46
47impl AnchorTarget {
48 #[inline]
49 pub fn name(self) -> &'static str {
50 self.into()
51 }
52
53 #[inline]
54 pub fn html_attr(self) -> &'static str {
55 match self {
56 AnchorTarget::NewTab => "_blank",
57 AnchorTarget::Parent => "_parent",
58 AnchorTarget::Top => "_top",
59 AnchorTarget::Same => "_same",
60 }
61 }
62}
63
64impl<'a> TryFrom<&'a str> for AnchorTarget {
65 type Error = ();
66
67 fn try_from(value: &'a str) -> Result<Self, Self::Error> {
68 const ANCHOR_TARGET_VALUES: [(&str, &str, AnchorTarget); 4] = [
69 ("blank", "_blank", AnchorTarget::NewTab),
70 ("parent", "_parent", AnchorTarget::Parent),
71 ("top", "_top", AnchorTarget::Top),
72 ("self", "_self", AnchorTarget::Same),
73 ];
74
75 for (value1, value2, target) in &ANCHOR_TARGET_VALUES {
76 if value.eq_ignore_ascii_case(value1) || value.eq_ignore_ascii_case(value2) {
77 return Ok(*target);
78 }
79 }
80
81 Err(())
82 }
83}