1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/*
 * tree/list.rs
 *
 * ftml - Library to parse Wikidot text
 * Copyright (C) 2019-2021 Wikijump Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

use super::attribute::AttributeMap;
use super::clone::elements_to_owned;
use super::Element;
use strum_macros::IntoStaticStr;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case", tag = "item-type")]
pub enum ListItem<'t> {
    /// This item is a series of elements.
    ///
    /// It's just an item in the list, which may have multiple elements
    /// similar to any other container.
    Elements {
        attributes: AttributeMap<'t>,
        elements: Vec<Element<'t>>,
    },

    /// This item in the list is a sub-list.
    ///
    /// That is, it's another, deeper list within the list.
    SubList {
        #[serde(flatten)]
        element: Box<Element<'t>>,
    },
}

impl ListItem<'_> {
    pub fn to_owned(&self) -> ListItem<'static> {
        match self {
            ListItem::Elements {
                attributes,
                elements,
            } => ListItem::Elements {
                attributes: attributes.to_owned(),
                elements: elements_to_owned(elements),
            },
            ListItem::SubList { element } => {
                let element: &Element = &*element;

                ListItem::SubList {
                    element: Box::new(element.to_owned()),
                }
            }
        }
    }
}

#[derive(
    Serialize, Deserialize, IntoStaticStr, Debug, Copy, Clone, Hash, PartialEq, Eq,
)]
#[serde(rename_all = "kebab-case")]
pub enum ListType {
    /// Bullet lists, or "unordered lists" in HTML.
    ///
    /// Corresponds to the tag `<ul>`.
    Bullet,

    /// Numbered lists, or "ordered lists" in HTML.
    ///
    /// Corresponds to the tag `<ol>`.
    Numbered,

    /// Generic list, which does not have a preferred
    ///
    /// This can be implemented in HTML with either
    /// `<ul>` or `<ol>`, as these should not have any
    /// list items that are not sub-lists.
    Generic,
}

impl ListType {
    #[inline]
    pub fn name(self) -> &'static str {
        self.into()
    }

    #[inline]
    pub fn html_tag(self) -> &'static str {
        match self {
            ListType::Bullet | ListType::Generic => "ul",
            ListType::Numbered => "ol",
        }
    }
}