summaryrefslogtreecommitdiff
path: root/src/directive/mod.rs
blob: e33f572ea599812625db8928fda4af1178744964 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate::error::SiteError;
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;

use log::{debug, trace};
use std::collections::HashSet;

#[derive(Debug, Eq, PartialEq)]
pub enum Directive {
    Simple,
    UnnamedArg,
    SimpleArg,
    QuotedArg,
    MultilineArg,

    Calendar(Calendar),
    Format(Format),
    Graph(Graph),
    Img(Img),
    Inline(Inline),
    Map(Map),
    Meta(Meta),
    PageStats(PageStats),
    Shortcut(Shortcut),
    Sidebar(Sidebar),
    Table(Table),
    Tag(Tag),
    Toc(Toc),
    TrailLink(TrailLink),
}

impl TryFrom<ParsedDirective> for Directive {
    type Error = SiteError;

    fn try_from(p: ParsedDirective) -> Result<Self, Self::Error> {
        Self::try_from(&p)
    }
}

impl TryFrom<&ParsedDirective> for Directive {
    type Error = SiteError;

    fn try_from(p: &ParsedDirective) -> Result<Self, Self::Error> {
        let d = match p.name() {
            "simple" => {
                Self::check_args(p, &[], &[], false)?;
                Directive::Simple
            }
            "unnamedarg" => {
                Self::check_args(p, &[], &[], true)?;
                Directive::UnnamedArg
            }
            "simplearg" => {
                Self::check_args(p, &["foo"], &[], false)?;
                Directive::SimpleArg
            }
            "quotedarg" => {
                Self::check_args(p, &["bar"], &[], false)?;
                Directive::QuotedArg
            }
            "multilinearg" => {
                Self::check_args(p, &["yo"], &["then", "else"], false)?;
                Directive::MultilineArg
            }

            "calendar" => {
                Self::check_args(
                    p,
                    Calendar::REQUIRED,
                    Calendar::ALLOWED,
                    Calendar::ALLOW_ANY_UNNAMED,
                )?;
                Directive::Calendar(Calendar::from(p))
            }
            "format" => {
                Self::check_args(
                    p,
                    Format::REQUIRED,
                    Format::ALLOWED,
                    Format::ALLOW_ANY_UNNAMED,
                )?;
                Directive::Format(Format::from(p))
            }
            "graph" => {
                Self::check_args(p, Graph::REQUIRED, Graph::ALLOWED, Graph::ALLOW_ANY_UNNAMED)?;
                Directive::Graph(Graph::from(p))
            }
            "img" => {
                Self::check_args(p, Img::REQUIRED, Img::ALLOWED, Img::ALLOW_ANY_UNNAMED)?;
                Directive::Img(Img::from(p))
            }
            "inline" => {
                Self::check_args(
                    p,
                    Inline::REQUIRED,
                    Inline::ALLOWED,
                    Inline::ALLOW_ANY_UNNAMED,
                )?;
                Directive::Inline(Inline::from(p))
            }
            "map" => {
                Self::check_args(p, Map::REQUIRED, Map::ALLOWED, Map::ALLOW_ANY_UNNAMED)?;
                Directive::Map(Map::from(p))
            }
            "meta" => {
                Self::check_args(p, Meta::REQUIRED, Meta::ALLOWED, Meta::ALLOW_ANY_UNNAMED)?;
                Directive::Meta(Meta::from(p))
            }
            "pagestats" => {
                Self::check_args(
                    p,
                    PageStats::REQUIRED,
                    PageStats::ALLOWED,
                    PageStats::ALLOW_ANY_UNNAMED,
                )?;
                Directive::PageStats(PageStats::from(p))
            }
            "shortcut" => {
                Self::check_args(
                    p,
                    Shortcut::REQUIRED,
                    Shortcut::ALLOWED,
                    Shortcut::ALLOW_ANY_UNNAMED,
                )?;
                Directive::Shortcut(Shortcut::from(p))
            }
            "sidebar" => {
                Self::check_args(
                    p,
                    Sidebar::REQUIRED,
                    Sidebar::ALLOWED,
                    Sidebar::ALLOW_ANY_UNNAMED,
                )?;
                Directive::Sidebar(Sidebar::from(p))
            }
            "tag" => {
                Self::check_args(p, Tag::REQUIRED, Tag::ALLOWED, Tag::ALLOW_ANY_UNNAMED)?;
                Directive::Tag(Tag::from(p))
            }
            "table" => {
                Self::check_args(p, Table::REQUIRED, Table::ALLOWED, Table::ALLOW_ANY_UNNAMED)?;
                Directive::Table(Table::from(p))
            }
            "toc" => {
                Self::check_args(p, Toc::REQUIRED, Toc::ALLOWED, Toc::ALLOW_ANY_UNNAMED)?;
                Directive::Toc(Toc::from(p))
            }
            "traillink" => {
                Self::check_args(
                    p,
                    TrailLink::REQUIRED,
                    TrailLink::ALLOWED,
                    TrailLink::ALLOW_ANY_UNNAMED,
                )?;
                Directive::TrailLink(TrailLink::from(p))
            }
            _ => return Err(SiteError::UnknownDirective(p.name().into())),
        };
        Ok(d)
    }
}

impl Directive {
    fn check_args(
        p: &ParsedDirective,
        required: &[&str],
        allowed: &[&str],
        allow_any_unnamed: bool,
    ) -> Result<(), SiteError> {
        let args = p.args();
        for arg in required.iter() {
            if !args.contains_key(arg) {
                return Err(SiteError::DirectiveMissingArg(
                    p.name().into(),
                    arg.to_string(),
                ));
            }
        }
        let required: HashSet<String> = required.iter().map(|arg| arg.to_string()).collect();
        let allowed: HashSet<String> = allowed.iter().map(|arg| arg.to_string()).collect();
        let allowed: HashSet<String> = required.union(&allowed).cloned().collect();
        for (arg, value) in p.args().iter() {
            if value.is_empty() {
                if !allow_any_unnamed {
                    return Err(SiteError::UnknownArgsNotAllowed(p.name().into()));
                }
            } else if !allowed.contains(*arg) {
                debug!("parsed directive {:?}", p);
                return Err(SiteError::DirectiveUnknownArg(
                    p.name().into(),
                    arg.to_string(),
                ));
            }
        }
        Ok(())
    }

    pub fn prepare(&self, site: &mut Site) -> Result<(), SiteError> {
        trace!("prepare directive {:?}", self);
        if let Self::Shortcut(x) = self {
            x.prepare(site)?;
        }
        Ok(())
    }

    pub fn process(&self, site: &mut Site, meta: &mut PageMeta) -> Result<String, SiteError> {
        match self {
            Self::Simple
            | Self::UnnamedArg
            | Self::SimpleArg
            | Self::QuotedArg
            | Self::MultilineArg => {
                panic!("directive {:?} may only be used in parsing tests", self)
            }
            Self::Calendar(x) => x.process(site, meta),
            Self::Format(x) => x.process(site, meta),
            Self::Graph(x) => x.process(site, meta),
            Self::Img(x) => x.process(site, meta),
            Self::Inline(x) => x.process(site, meta),
            Self::Map(x) => x.process(site, meta),
            Self::Meta(x) => x.process(site, meta),
            Self::PageStats(x) => x.process(site, meta),
            Self::Shortcut(x) => x.process(site, meta),
            Self::Sidebar(x) => x.process(site, meta),
            Self::Table(x) => x.process(site, meta),
            Self::Tag(x) => x.process(site, meta),
            Self::Toc(x) => x.process(site, meta),
            Self::TrailLink(x) => x.process(site, meta),
        }
    }
}

mod format;
use format::Format;

mod meta;
use meta::Meta;

mod map;
use map::Map;

mod graph;
use graph::Graph;

mod img;
pub use img::Img;

mod inline;
pub use inline::Inline;

mod pagestats;
pub use pagestats::PageStats;

mod shortcut;
pub use shortcut::Shortcut;

mod table;
use table::Table;

mod tag;
use tag::Tag;

mod toc;
use toc::Toc;

mod traillink;
use traillink::TrailLink;

mod calendar;
use calendar::Calendar;

mod sidebar;
use sidebar::Sidebar;