Skip to main content

ftml/tree/
module.rs

1/*
2 * tree/module.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2026 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
21//! Representation of Wikidot modules, along with their context.
22
23use super::AttributeMap;
24use super::clone::option_string_to_owned;
25use std::borrow::Cow;
26use std::num::NonZeroU32;
27use strum_macros::IntoStaticStr;
28
29#[derive(Serialize, Deserialize, IntoStaticStr, Debug, Clone, PartialEq, Eq)]
30#[serde(rename_all = "kebab-case", tag = "module", content = "data")]
31pub enum Module<'t> {
32    /// Lists all the backlinks on the given page.
33    ///
34    /// If no page is listed, the backlinks are returned for the current page.
35    Backlinks { page: Option<Cow<'t, str>> },
36
37    /// Lists all categories on the site, along with the pages they contain.
38    #[serde(rename_all = "kebab-case")]
39    Categories { include_hidden: bool },
40
41    /// Allows a user to join a site.
42    #[serde(rename_all = "kebab-case")]
43    Join {
44        button_text: Option<Cow<'t, str>>,
45        attributes: AttributeMap<'t>,
46    },
47
48    /// Lists the structure of pages as connected by parenthood.
49    ///
50    /// Shows the hierarchy of parent relationships present on the given page.
51    /// If no root page is listed, the tree returned is for the current page.
52    #[serde(rename_all = "kebab-case")]
53    PageTree {
54        root: Option<Cow<'t, str>>,
55        show_root: bool,
56        depth: Option<NonZeroU32>,
57    },
58
59    /// A rating module, which can be used to vote on the page.
60    Rate,
61}
62
63impl Module<'_> {
64    #[inline]
65    pub fn name(&self) -> &'static str {
66        self.into()
67    }
68
69    pub fn to_owned(&self) -> Module<'static> {
70        match self {
71            Module::Backlinks { page } => Module::Backlinks {
72                page: option_string_to_owned(page),
73            },
74            Module::Categories { include_hidden } => Module::Categories {
75                include_hidden: *include_hidden,
76            },
77            Module::Join {
78                button_text,
79                attributes,
80            } => Module::Join {
81                button_text: option_string_to_owned(button_text),
82                attributes: attributes.to_owned(),
83            },
84            Module::PageTree {
85                root,
86                show_root,
87                depth,
88            } => Module::PageTree {
89                root: option_string_to_owned(root),
90                show_root: *show_root,
91                depth: *depth,
92            },
93            Module::Rate => Module::Rate,
94        }
95    }
96}