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
use std::fmt::Display;

use chumsky::{select, Parser};

use crate::{child_table, impl_parse, section, Object, TagType};

/// **Grammar**
///
/// ---@class MY_TYPE[:PARENT_TYPE] [@comment]
///
/// **Emmy**
///
/// ---@class CMode Comment modes - Can be manual or computed in operator-pending phase
/// ---@field toggle number Toggle action
/// ---@field comment number Comment action
/// ---@field uncomment number Uncomment action
///
/// **Help**
///
/// CMode                                                                   \*CMode\*
///     Comment modes - Can be manual or computed in operator-pending phase
///
///     Fields: ~
///         {toggle}     (number)    Toggle action
///         {comment}    (number)    Comment action
///         {uncomment}  (number)    Uncomment action
///
#[derive(Debug)]
pub struct Class {
    pub name: String,
    pub desc: Option<String>,
    pub fields: Vec<Object>,
    pub see: Vec<String>,
}

impl_parse!(Class, {
    select! { TagType::Class(name, desc) => (name, desc) }
        .then(select! { TagType::Field(x) => x }.repeated())
        .then(select! { TagType::See(x) => x }.repeated())
        .map(|(((name, desc), fields), see)| Self {
            name,
            desc,
            fields,
            see,
        })
});

impl Display for Class {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut blocks = Vec::with_capacity(2);

        blocks.push(
            child_table!(
                "Fields: ~",
                self.fields.iter().map(|field| {
                    [
                        format!("{{{}}}", field.ty),
                        format!("({})", field.name),
                        field.desc.clone().unwrap_or_default(),
                    ]
                })
            )
            .to_string(),
        );

        if !self.see.is_empty() {
            blocks.push(
                child_table!("See: ~", self.see.iter().map(|s| [format!("|{}|", s)])).to_string(),
            )
        }

        let head = section!(
            self.name.as_str(),
            self.name.as_str(),
            self.desc.clone().unwrap_or_default().as_str(),
            blocks
        );

        write!(f, "{}", head)
    }
}