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
use chumsky::{
    prelude::{choice, just},
    select, Parser,
};

use crate::{parser, TagType};

#[derive(Debug, Default, Clone)]
pub struct Prefix {
    pub left: Option<String>,
    pub right: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Kind {
    Dot,
    Colon,
    Local,
}

impl Kind {
    pub fn as_char(&self) -> char {
        match self {
            Self::Dot => '.',
            Self::Colon => ':',
            Self::Local => unreachable!(), // This should never happens
        }
    }
}

#[derive(Debug, Clone)]
pub struct Usage {
    pub code: String,
}

parser!(Usage, {
    choice((
        select! { TagType::Comment(x) => x }
            .repeated()
            .delimited_by(just(TagType::UsageStart), just(TagType::UsageEnd))
            .map(|x| Self { code: x.join("\n") }),
        select! { TagType::Usage(code) => Self { code } },
    ))
});

impl std::fmt::Display for Usage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        crate::description!(f, "Usage: ~")?;
        writeln!(f, "{:>9}", ">")?;
        writeln!(f, "{}", textwrap::indent(&self.code, "            "))?;
        writeln!(f, "{:>9}", "<")
    }
}

#[derive(Debug, Clone)]
pub struct See {
    pub refs: Vec<String>,
}

parser!(See, {
    select! { TagType::See(x) => x }
        .repeated()
        .map(|refs| Self { refs })
});

impl std::fmt::Display for See {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        crate::description!(f, "See: ~")?;
        for s in &self.refs {
            writeln!(f, "        |{s}|")?;
        }
        Ok(())
    }
}