diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-07-24 09:17:13 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-07-24 09:17:13 +0300 |
commit | 6869df7dedd8b434cb676fb25e9631b7723f0f28 (patch) | |
tree | 8843df4ad4c38b13740b1422fab5590cca1ba21a | |
parent | e4e3c0c56857567395adf2e982292dc9b3d29e73 (diff) | |
download | riki-6869df7dedd8b434cb676fb25e9631b7723f0f28.tar.gz |
feat: allow a directive to have any number of unnamed arguments
Sponsored-by: author
-rw-r--r-- | src/directive/img.rs | 3 | ||||
-rw-r--r-- | src/wikitext.rs | 40 |
2 files changed, 14 insertions, 29 deletions
diff --git a/src/directive/img.rs b/src/directive/img.rs index 4a56e6b..f81f8a9 100644 --- a/src/directive/img.rs +++ b/src/directive/img.rs @@ -31,6 +31,7 @@ impl Img { impl From<ParsedDirective> for Img { fn from(p: ParsedDirective) -> Self { - Img::new(p.unnamed_arg().expect("img must have filename argument").into()) + let unnamed = p.unnamed_args().pop().unwrap(); + Img::new(unnamed.into()) } } diff --git a/src/wikitext.rs b/src/wikitext.rs index 77d6b58..39fbd32 100644 --- a/src/wikitext.rs +++ b/src/wikitext.rs @@ -201,39 +201,14 @@ impl WikiLink { pub struct ParsedDirective { name: String, args: HashMap<String, String>, - unnamed_arg: Option<String>, } impl ParsedDirective { - pub fn new(name: &str, mut args: HashMap<String, String>) -> Result<Self, SiteError> { + pub fn new(name: &str, args: HashMap<String, String>) -> Result<Self, SiteError> { trace!("ParsedDirective::new: name={:?} args={:?}", name, args); - let unnamed_args: Vec<String> = args - .iter() - .filter_map(|(k, v)| { - if v.is_empty() { - Some(k.to_string()) - } else { - None - } - }) - .collect(); - trace!("ParsedDirective::new: unnamed_args={:?}", unnamed_args); - - let unnamed_arg = match unnamed_args.len() { - 0 => None, - 1 => { - let key = unnamed_args.get(0).unwrap(); - args.remove(key); - Some(key.to_string()) - } - _ => return Err(SiteError::TooManyUnnamedArgs(name.into())), - }; - trace!("ParsedDirective::new: unnamed_arg={:?}", unnamed_arg); - Ok(Self { name: name.into(), args, - unnamed_arg, }) } @@ -248,8 +223,17 @@ impl ParsedDirective { .collect() } - pub fn unnamed_arg(&self) -> Option<&str> { - self.unnamed_arg.as_deref() + pub fn unnamed_args(&self) -> Vec<&str> { + self.args + .iter() + .filter_map(|(k, v)| { + if v.is_empty() { + Some(k.as_str()) + } else { + None + } + }) + .collect() } } |