summaryrefslogtreecommitdiff
path: root/src/directive/mod.rs
blob: 38fb0d35f9fae36fb255c8c720c71b42e5462ad7 (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
use crate::error::SiteError;
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;
use std::collections::HashSet;

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

    Meta(Meta),
    Img(Img),
    Tag(Tag),
}

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
            }
            "img" => {
                Self::check_args(&p, Img::REQUIRED, Img::ALLOWED, Img::ALLOW_ANY_UNNAMED)?;
                Directive::Img(Img::from(p))
            }
            "meta" => {
                Self::check_args(&p, Meta::REQUIRED, Meta::ALLOWED, Meta::ALLOW_ANY_UNNAMED)?;
                Directive::Meta(Meta::from(p))
            }
            "tag" => {
                Self::check_args(&p, Tag::REQUIRED, Tag::ALLOWED, Tag::ALLOW_ANY_UNNAMED)?;
                Directive::Tag(Tag::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 allow_any_unnamed && value.is_empty() {
            } else if !allowed.contains(*arg) {
                return Err(SiteError::DirectiveUnknownArg(
                    p.name().into(),
                    arg.to_string(),
                ));
            }
        }
        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::Img(x) => x.process(site, meta),
            Self::Meta(x) => x.process(site, meta),
            Self::Tag(x) => x.process(site, meta),
        }
    }
}

mod meta;
use meta::Meta;

mod img;
pub use img::Img;

mod tag;
use tag::Tag;