diff options
Diffstat (limited to 'src/directive/mod.rs')
-rw-r--r-- | src/directive/mod.rs | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/src/directive/mod.rs b/src/directive/mod.rs index e33f572..ae2dc09 100644 --- a/src/directive/mod.rs +++ b/src/directive/mod.rs @@ -6,6 +6,24 @@ use crate::wikitext::ParsedDirective; use log::{debug, trace}; use std::collections::HashSet; +pub enum Processed { + Markdown(String), + Toc(usize), +} + +pub trait DirectiveImplementation { + const REQUIRED: &'static [&'static str]; + const ALLOWED: &'static [&'static str]; + const ALLOW_ANY_UNNAMED: bool; + + fn from_parsed(p: &ParsedDirective) -> Self; + fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<Processed, SiteError>; + + fn prepare(&self, _site: &mut Site) -> Result<(), SiteError> { + Ok(()) + } +} + #[derive(Debug, Eq, PartialEq)] pub enum Directive { Simple, @@ -71,7 +89,7 @@ impl TryFrom<&ParsedDirective> for Directive { Calendar::ALLOWED, Calendar::ALLOW_ANY_UNNAMED, )?; - Directive::Calendar(Calendar::from(p)) + Directive::Calendar(Calendar::from_parsed(p)) } "format" => { Self::check_args( @@ -80,15 +98,15 @@ impl TryFrom<&ParsedDirective> for Directive { Format::ALLOWED, Format::ALLOW_ANY_UNNAMED, )?; - Directive::Format(Format::from(p)) + Directive::Format(Format::from_parsed(p)) } "graph" => { Self::check_args(p, Graph::REQUIRED, Graph::ALLOWED, Graph::ALLOW_ANY_UNNAMED)?; - Directive::Graph(Graph::from(p)) + Directive::Graph(Graph::from_parsed(p)) } "img" => { Self::check_args(p, Img::REQUIRED, Img::ALLOWED, Img::ALLOW_ANY_UNNAMED)?; - Directive::Img(Img::from(p)) + Directive::Img(Img::from_parsed(p)) } "inline" => { Self::check_args( @@ -97,15 +115,15 @@ impl TryFrom<&ParsedDirective> for Directive { Inline::ALLOWED, Inline::ALLOW_ANY_UNNAMED, )?; - Directive::Inline(Inline::from(p)) + Directive::Inline(Inline::from_parsed(p)) } "map" => { Self::check_args(p, Map::REQUIRED, Map::ALLOWED, Map::ALLOW_ANY_UNNAMED)?; - Directive::Map(Map::from(p)) + Directive::Map(Map::from_parsed(p)) } "meta" => { Self::check_args(p, Meta::REQUIRED, Meta::ALLOWED, Meta::ALLOW_ANY_UNNAMED)?; - Directive::Meta(Meta::from(p)) + Directive::Meta(Meta::from_parsed(p)) } "pagestats" => { Self::check_args( @@ -114,7 +132,7 @@ impl TryFrom<&ParsedDirective> for Directive { PageStats::ALLOWED, PageStats::ALLOW_ANY_UNNAMED, )?; - Directive::PageStats(PageStats::from(p)) + Directive::PageStats(PageStats::from_parsed(p)) } "shortcut" => { Self::check_args( @@ -123,7 +141,7 @@ impl TryFrom<&ParsedDirective> for Directive { Shortcut::ALLOWED, Shortcut::ALLOW_ANY_UNNAMED, )?; - Directive::Shortcut(Shortcut::from(p)) + Directive::Shortcut(Shortcut::from_parsed(p)) } "sidebar" => { Self::check_args( @@ -132,19 +150,19 @@ impl TryFrom<&ParsedDirective> for Directive { Sidebar::ALLOWED, Sidebar::ALLOW_ANY_UNNAMED, )?; - Directive::Sidebar(Sidebar::from(p)) + Directive::Sidebar(Sidebar::from_parsed(p)) } "tag" => { Self::check_args(p, Tag::REQUIRED, Tag::ALLOWED, Tag::ALLOW_ANY_UNNAMED)?; - Directive::Tag(Tag::from(p)) + Directive::Tag(Tag::from_parsed(p)) } "table" => { Self::check_args(p, Table::REQUIRED, Table::ALLOWED, Table::ALLOW_ANY_UNNAMED)?; - Directive::Table(Table::from(p)) + Directive::Table(Table::from_parsed(p)) } "toc" => { Self::check_args(p, Toc::REQUIRED, Toc::ALLOWED, Toc::ALLOW_ANY_UNNAMED)?; - Directive::Toc(Toc::from(p)) + Directive::Toc(Toc::from_parsed(p)) } "traillink" => { Self::check_args( @@ -153,7 +171,7 @@ impl TryFrom<&ParsedDirective> for Directive { TrailLink::ALLOWED, TrailLink::ALLOW_ANY_UNNAMED, )?; - Directive::TrailLink(TrailLink::from(p)) + Directive::TrailLink(TrailLink::from_parsed(p)) } _ => return Err(SiteError::UnknownDirective(p.name().into())), }; @@ -204,7 +222,7 @@ impl Directive { Ok(()) } - pub fn process(&self, site: &mut Site, meta: &mut PageMeta) -> Result<String, SiteError> { + pub fn process(&self, site: &mut Site, meta: &mut PageMeta) -> Result<Processed, SiteError> { match self { Self::Simple | Self::UnnamedArg @@ -262,7 +280,7 @@ mod tag; use tag::Tag; mod toc; -use toc::Toc; +pub use toc::Toc; mod traillink; use traillink::TrailLink; |