diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-11-09 13:44:54 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-11-09 13:44:54 +0200 |
commit | 3421ef66def1b0ad140a11fa35f101d8eaf4934d (patch) | |
tree | 7d3a1da1b79035970c2032a0dc8adfab920635d1 | |
parent | 8db80a826f401f680fb78e45a53371ebfe2f4459 (diff) | |
download | riki-3421ef66def1b0ad140a11fa35f101d8eaf4934d.tar.gz |
refactor: use trait for all directive implementations
Sponsored-by: author
-rw-r--r-- | src/directive/format.rs | 19 | ||||
-rw-r--r-- | src/directive/graph.rs | 20 | ||||
-rw-r--r-- | src/directive/img.rs | 199 | ||||
-rw-r--r-- | src/directive/inline.rs | 31 | ||||
-rw-r--r-- | src/directive/map.rs | 19 | ||||
-rw-r--r-- | src/directive/meta.rs | 48 | ||||
-rw-r--r-- | src/directive/mod.rs | 33 | ||||
-rw-r--r-- | src/directive/pagestats.rs | 19 | ||||
-rw-r--r-- | src/directive/shortcut.rs | 31 | ||||
-rw-r--r-- | src/directive/sidebar.rs | 19 | ||||
-rw-r--r-- | src/directive/table.rs | 21 | ||||
-rw-r--r-- | src/directive/tag.rs | 23 | ||||
-rw-r--r-- | src/directive/toc.rs | 19 | ||||
-rw-r--r-- | src/directive/traillink.rs | 19 |
14 files changed, 255 insertions, 265 deletions
diff --git a/src/directive/format.rs b/src/directive/format.rs index ee9c7ec..f76ba9a 100644 --- a/src/directive/format.rs +++ b/src/directive/format.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,18 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct Format {} -impl Format { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &[]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Format { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &[]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { - Err(SiteError::UnimplementedDirective("format".into())) + fn from_parsed(_: &ParsedDirective) -> Self { + Self::default() } -} -impl From<&ParsedDirective> for Format { - fn from(_: &ParsedDirective) -> Self { - Format::default() + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + Err(SiteError::UnimplementedDirective("format".into())) } } diff --git a/src/directive/graph.rs b/src/directive/graph.rs index 8234ed2..e7ae5fe 100644 --- a/src/directive/graph.rs +++ b/src/directive/graph.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,18 +7,17 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct Graph {} -impl Graph { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["src", "type"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Graph { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["src", "type"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { - Err(SiteError::UnimplementedDirective("graph".into())) + fn from_parsed(_: &ParsedDirective) -> Self { + Self::default() } -} -impl From<&ParsedDirective> for Graph { - fn from(_: &ParsedDirective) -> Self { - Graph::default() + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + Err(SiteError::UnimplementedDirective("graph".into())) } + } diff --git a/src/directive/img.rs b/src/directive/img.rs index 3ee6501..29efe0c 100644 --- a/src/directive/img.rs +++ b/src/directive/img.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -21,70 +22,67 @@ pub struct Img { width: Option<usize>, } -impl Img { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &[ +impl DirectiveImplementation for Img { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &[ "align", "alt", "class", "hspace", "id", "link", "size", "title", "vspace", ]; - pub const ALLOW_ANY_UNNAMED: bool = true; + const ALLOW_ANY_UNNAMED: bool = true; - fn new(src: String) -> Self { - Self { - src, - link: true, - align: None, - alt: None, - class: None, - height: None, - hspace: None, - id: None, - title: None, - vspace: None, - width: None, - } - } + fn from_parsed(p: &ParsedDirective) -> Self { + let unnamed = p.unnamed_args().pop().unwrap(); + let mut img = Img::new(unnamed.into()); + let args = p.args(); - fn link(&mut self, link: bool) { - self.link = link; - } + if let Some(link) = args.get("link") { + if *link == "no" { + img.link(false); + } + } - fn align(&mut self, align: String) { - self.align = Some(align); - } + if let Some(size) = args.get("size") { + if let Some((w, h)) = size.split_once('x') { + if let Ok(w) = w.parse() { + img.width(w); + } + if let Ok(h) = h.parse() { + img.height(h); + } + } + } - fn alt(&mut self, alt: String) { - self.alt = Some(alt); - } + if let Some(align) = args.get("align") { + img.align(align.to_string()); + } - fn class(&mut self, class: String) { - self.class = Some(class); - } + if let Some(alt) = args.get("alt") { + img.alt(alt.to_string()); + } - fn height(&mut self, h: usize) { - self.height = Some(h); - } + if let Some(class) = args.get("class") { + img.class(class.to_string()); + } - fn hspace(&mut self, hspace: String) { - self.hspace = Some(hspace); - } + if let Some(hspace) = args.get("hspace") { + img.hspace(hspace.to_string()); + } - fn id(&mut self, id: String) { - self.id = Some(id); - } + if let Some(id) = args.get("id") { + img.id(id.to_string()); + } - fn title(&mut self, title: String) { - self.title = Some(title); - } + if let Some(title) = args.get("title") { + img.title(title.to_string()); + } - fn vspace(&mut self, vspace: String) { - self.vspace = Some(vspace); - } + if let Some(vspace) = args.get("vspace") { + img.vspace(vspace.to_string()); + } - fn width(&mut self, w: usize) { - self.width = Some(w); + img } - pub fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { trace!( "verify image exists: {} on {}", self.src, @@ -127,67 +125,70 @@ impl Img { } } -fn push_attr(s: &mut String, name: &str, value: &Option<String>) { - if let Some(v) = value { - s.push_str(&format!( - " {}=\"{}\"", - name, - encode_double_quoted_attribute(v) - )); +impl Img { + fn new(src: String) -> Self { + Self { + src, + link: true, + align: None, + alt: None, + class: None, + height: None, + hspace: None, + id: None, + title: None, + vspace: None, + width: None, + } } -} -impl From<&ParsedDirective> for Img { - fn from(p: &ParsedDirective) -> Self { - let unnamed = p.unnamed_args().pop().unwrap(); - let mut img = Img::new(unnamed.into()); - let args = p.args(); + fn link(&mut self, link: bool) { + self.link = link; + } - if let Some(link) = args.get("link") { - if *link == "no" { - img.link(false); - } - } + fn align(&mut self, align: String) { + self.align = Some(align); + } - if let Some(size) = args.get("size") { - if let Some((w, h)) = size.split_once('x') { - if let Ok(w) = w.parse() { - img.width(w); - } - if let Ok(h) = h.parse() { - img.height(h); - } - } - } + fn alt(&mut self, alt: String) { + self.alt = Some(alt); + } - if let Some(align) = args.get("align") { - img.align(align.to_string()); - } + fn class(&mut self, class: String) { + self.class = Some(class); + } - if let Some(alt) = args.get("alt") { - img.alt(alt.to_string()); - } + fn height(&mut self, h: usize) { + self.height = Some(h); + } - if let Some(class) = args.get("class") { - img.class(class.to_string()); - } + fn hspace(&mut self, hspace: String) { + self.hspace = Some(hspace); + } - if let Some(hspace) = args.get("hspace") { - img.hspace(hspace.to_string()); - } + fn id(&mut self, id: String) { + self.id = Some(id); + } - if let Some(id) = args.get("id") { - img.id(id.to_string()); - } + fn title(&mut self, title: String) { + self.title = Some(title); + } - if let Some(title) = args.get("title") { - img.title(title.to_string()); - } + fn vspace(&mut self, vspace: String) { + self.vspace = Some(vspace); + } - if let Some(vspace) = args.get("vspace") { - img.vspace(vspace.to_string()); - } + fn width(&mut self, w: usize) { + self.width = Some(w); + } +} - img +fn push_attr(s: &mut String, name: &str, value: &Option<String>) { + if let Some(v) = value { + s.push_str(&format!( + " {}=\"{}\"", + name, + encode_double_quoted_attribute(v) + )); } } diff --git a/src/directive/inline.rs b/src/directive/inline.rs index e6577b2..0dafa69 100644 --- a/src/directive/inline.rs +++ b/src/directive/inline.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::pagespec::PageSpec; @@ -11,9 +12,9 @@ pub struct Inline { pages: String, } -impl Inline { - pub const REQUIRED: &'static [&'static str] = &["pages"]; - pub const ALLOWED: &'static [&'static str] = &[ +impl DirectiveImplementation for Inline { + const REQUIRED: &'static [&'static str] = &["pages"]; + const ALLOWED: &'static [&'static str] = &[ "actions", "archive", "description", @@ -29,13 +30,15 @@ impl Inline { "template", "trail", ]; - pub const ALLOW_ANY_UNNAMED: bool = true; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new(pages: String) -> Self { - Self { pages } + fn from_parsed(p: &ParsedDirective) -> Self { + let args = p.args(); + let pages = args.get("pages").unwrap(); + Inline::new(pages.to_string()) } - pub fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { let pagespec = PageSpec::new(meta.path(), &self.pages)?; let matches: Vec<String> = site .markdown_pages() @@ -45,17 +48,15 @@ impl Inline { .collect(); Ok(matches.join("")) } +} + +impl Inline { + pub fn new(pages: String) -> Self { + Self { pages } + } fn link(container: &Path, meta: &PageMeta) -> String { let link = make_relative_link(container, meta.path()); format!("[{}]({})", meta.title(), link.display()) } } - -impl From<&ParsedDirective> for Inline { - fn from(p: &ParsedDirective) -> Self { - let args = p.args(); - let pages = args.get("pages").unwrap(); - Inline::new(pages.to_string()) - } -} diff --git a/src/directive/map.rs b/src/directive/map.rs index 960b339..009bf64 100644 --- a/src/directive/map.rs +++ b/src/directive/map.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,18 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct Map {} -impl Map { - pub const REQUIRED: &'static [&'static str] = &["pages"]; - pub const ALLOWED: &'static [&'static str] = &["show"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Map { + const REQUIRED: &'static [&'static str] = &["pages"]; + const ALLOWED: &'static [&'static str] = &["show"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { - Err(SiteError::UnimplementedDirective("map".into())) + fn from_parsed(_: &ParsedDirective) -> Self { + Self::default() } -} -impl From<&ParsedDirective> for Map { - fn from(_: &ParsedDirective) -> Self { - Map::default() + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + Err(SiteError::UnimplementedDirective("map".into())) } } diff --git a/src/directive/meta.rs b/src/directive/meta.rs index 490ffb1..1e8ec8c 100644 --- a/src/directive/meta.rs +++ b/src/directive/meta.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -10,20 +11,24 @@ pub struct Meta { date: Option<String>, } -impl Meta { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["date", "link", "title", "author"]; - pub const ALLOW_ANY_UNNAMED: bool = false; - - fn set_date(&mut self, date: &str) { - self.date = Some(date.into()); - } +impl DirectiveImplementation for Meta { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["date", "link", "title", "author"]; + const ALLOW_ANY_UNNAMED: bool = false; - fn set_title(&mut self, title: &str) { - self.title = Some(title.into()); + fn from_parsed(p: &ParsedDirective) -> Self { + let mut meta = Self::default(); + let args = p.args(); + if let Some(title) = args.get("title") { + meta.set_title(title); + } + if let Some(date) = args.get("date") { + meta.set_date(date); + } + meta } - pub fn process(&self, _site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { if let Some(title) = &self.title { meta.set_title(title.into()); } @@ -34,19 +39,12 @@ impl Meta { } } -impl From<&ParsedDirective> for Meta { - fn from(p: &ParsedDirective) -> Self { - let mut meta = Meta::default(); - let args = p.args(); - if let Some(title) = args.get("title") { - meta.set_title(title); - } - if let Some(date) = args.get("date") { - meta.set_date(date); - } - meta +impl Meta { + fn set_date(&mut self, date: &str) { + self.date = Some(date.into()); } -} -#[cfg(test)] -mod test {} + fn set_title(&mut self, title: &str) { + self.title = Some(title.into()); + } +} diff --git a/src/directive/mod.rs b/src/directive/mod.rs index 40def2f..e885404 100644 --- a/src/directive/mod.rs +++ b/src/directive/mod.rs @@ -10,12 +10,17 @@ pub enum Processed { Markdown(String), } -trait DirectiveImplementation { +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<String, SiteError>; + + fn prepare(&self, _site: &mut Site) -> Result<(), SiteError> { + Ok(()) + } } #[derive(Debug, Eq, PartialEq)] @@ -92,15 +97,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( @@ -109,15 +114,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( @@ -126,7 +131,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( @@ -135,7 +140,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( @@ -144,19 +149,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( @@ -165,7 +170,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())), }; diff --git a/src/directive/pagestats.rs b/src/directive/pagestats.rs index 89ba57e..08df39d 100644 --- a/src/directive/pagestats.rs +++ b/src/directive/pagestats.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,22 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct PageStats {} -impl PageStats { - pub const REQUIRED: &'static [&'static str] = &["pages"]; - pub const ALLOWED: &'static [&'static str] = &["among", "style"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for PageStats { + const REQUIRED: &'static [&'static str] = &["pages"]; + const ALLOWED: &'static [&'static str] = &["among", "style"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new() -> Self { + fn from_parsed(_: &ParsedDirective) -> Self { Self::default() } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { Err(SiteError::UnimplementedDirective("pagestat".into())) } } - -impl From<&ParsedDirective> for PageStats { - fn from(_p: &ParsedDirective) -> Self { - Self::new() - } -} diff --git a/src/directive/shortcut.rs b/src/directive/shortcut.rs index 7b397d4..fa3f783 100644 --- a/src/directive/shortcut.rs +++ b/src/directive/shortcut.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::{Shortcut as S, Site}; @@ -10,32 +11,32 @@ pub struct Shortcut { shortcut: S, } -impl Shortcut { - pub const REQUIRED: &'static [&'static str] = &["name", "url"]; - pub const ALLOWED: &'static [&'static str] = &["desc"]; - pub const ALLOW_ANY_UNNAMED: bool = false; +impl DirectiveImplementation for Shortcut { + const REQUIRED: &'static [&'static str] = &["name", "url"]; + const ALLOWED: &'static [&'static str] = &["desc"]; + const ALLOW_ANY_UNNAMED: bool = false; - pub fn new(shortcut: S) -> Self { - Self { shortcut } + fn from_parsed(p: &ParsedDirective) -> Self { + let args = p.args(); + let name = args.get("name").unwrap(); + let desc = args.get("desc").unwrap_or(&""); + let url = args.get("url").unwrap(); + Self::new(S::new(name, desc, url)) } - pub fn prepare(&self, site: &mut Site) -> Result<(), SiteError> { + fn prepare(&self, site: &mut Site) -> Result<(), SiteError> { trace!("shortcut: prepare"); site.add_shortcut(self.shortcut.clone()); Ok(()) } - pub fn process(&self, _site: &mut Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { Ok("".into()) } } -impl From<&ParsedDirective> for Shortcut { - fn from(p: &ParsedDirective) -> Self { - let args = p.args(); - let name = args.get("name").unwrap(); - let desc = args.get("desc").unwrap_or(&""); - let url = args.get("url").unwrap(); - Self::new(S::new(name, desc, url)) +impl Shortcut { + pub fn new(shortcut: S) -> Self { + Self { shortcut } } } diff --git a/src/directive/sidebar.rs b/src/directive/sidebar.rs index 5148086..a13292c 100644 --- a/src/directive/sidebar.rs +++ b/src/directive/sidebar.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,18 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct Sidebar {} -impl Sidebar { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["content"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Sidebar { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["content"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { - Err(SiteError::UnimplementedDirective("sidebar".into())) + fn from_parsed(_: &ParsedDirective) -> Self { + Self::default() } -} -impl From<&ParsedDirective> for Sidebar { - fn from(_: &ParsedDirective) -> Self { - Sidebar::default() + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + Err(SiteError::UnimplementedDirective("sidebar".into())) } } diff --git a/src/directive/table.rs b/src/directive/table.rs index 7610a6b..b11a39f 100644 --- a/src/directive/table.rs +++ b/src/directive/table.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -9,16 +10,16 @@ pub struct Table { data: String, } -impl Table { - pub const REQUIRED: &'static [&'static str] = &["data"]; - pub const ALLOWED: &'static [&'static str] = &[]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Table { + const REQUIRED: &'static [&'static str] = &["data"]; + const ALLOWED: &'static [&'static str] = &[]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new(data: String) -> Self { - Self { data } + fn from_parsed(p: &ParsedDirective) -> Self { + Self::new(p.args().get("data").unwrap().to_string()) } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { let mut table = String::new(); let mut lines = self.data.trim().lines(); if let Some(first) = lines.next() { @@ -40,8 +41,8 @@ impl Table { } } -impl From<&ParsedDirective> for Table { - fn from(p: &ParsedDirective) -> Self { - Table::new(p.args().get("data").unwrap().to_string()) +impl Table { + pub fn new(data: String) -> Self { + Self { data } } } diff --git a/src/directive/tag.rs b/src/directive/tag.rs index c64acfc..07c92ea 100644 --- a/src/directive/tag.rs +++ b/src/directive/tag.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -8,23 +9,23 @@ pub struct Tag { tags: Vec<String>, } -impl Tag { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["class"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Tag { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["class"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new(tags: Vec<String>) -> Self { - Self { tags } + fn from_parsed(p: &ParsedDirective) -> Self { + let tags = p.unnamed_args().iter().map(|s| s.to_string()).collect(); + Tag::new(tags) } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { Ok("".into()) } } -impl From<&ParsedDirective> for Tag { - fn from(p: &ParsedDirective) -> Self { - let tags = p.unnamed_args().iter().map(|s| s.to_string()).collect(); - Tag::new(tags) +impl Tag { + pub fn new(tags: Vec<String>) -> Self { + Self { tags } } } diff --git a/src/directive/toc.rs b/src/directive/toc.rs index af1bba9..ff03b0b 100644 --- a/src/directive/toc.rs +++ b/src/directive/toc.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,22 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct Toc {} -impl Toc { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["levels"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for Toc { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["levels"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new() -> Self { + fn from_parsed(_: &ParsedDirective) -> Self { Self::default() } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { Err(SiteError::UnimplementedDirective("toc".into())) } } - -impl From<&ParsedDirective> for Toc { - fn from(_p: &ParsedDirective) -> Self { - Self::new() - } -} diff --git a/src/directive/traillink.rs b/src/directive/traillink.rs index 9acfa6b..492f009 100644 --- a/src/directive/traillink.rs +++ b/src/directive/traillink.rs @@ -1,3 +1,4 @@ +use crate::directive::DirectiveImplementation; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -6,22 +7,16 @@ use crate::wikitext::ParsedDirective; #[derive(Debug, Default, Eq, PartialEq)] pub struct TrailLink {} -impl TrailLink { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &["text"]; - pub const ALLOW_ANY_UNNAMED: bool = true; +impl DirectiveImplementation for TrailLink { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &["text"]; + const ALLOW_ANY_UNNAMED: bool = true; - pub fn new() -> Self { + fn from_parsed(_: &ParsedDirective) -> Self { Self::default() } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> { Err(SiteError::UnimplementedDirective("traillink".into())) } } - -impl From<&ParsedDirective> for TrailLink { - fn from(_p: &ParsedDirective) -> Self { - Self::new() - } -} |