From 2c20aad7c16c24a3886b0f57fdd49257f864b83e Mon Sep 17 00:00:00 2001 From: British Werewolf Date: Tue, 26 Mar 2024 22:56:16 +0000 Subject: [PATCH] Add argument parser to directives. Currently only supports a single argument. --- src/main.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 77e983b..995ca47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ struct Cli { #[derive(Clone, Debug, PartialEq)] pub enum Directive { + Date(String), Uppercase, Lowercase, Markdown, @@ -350,12 +351,14 @@ fn parse_variable(input: Span) -> IResult { /// let (_, directive) = parse_placeholder_directive_key(input).unwrap(); /// assert_eq!(directive, Some(Directive::Uppercase)); /// ``` -fn parse_placeholder_directive_key(input: Span) -> IResult> { +fn parse_placeholder_directive_enum(input: Span) -> IResult> { let (input, name) = take_while(|c| is_alphabetic(c))(input)?; let (input, _) = opt(tuple((multispace0, tag(":"), multispace0)))(input)?; - // TODO Implement parsing arguments. - let (input, _arg) = opt(take_while(|c| is_alphabetic(c)))(input)?; + + let (input, arg) = opt(take_while(|c| c != '|' && c != '}'))(input)?; + let directive = match name.to_ascii_lowercase().as_str() { + "date" => arg.and_then(|arg| Some(Directive::Date(arg.to_string()))), "lowercase" => Some(Directive::Lowercase), "uppercase" => Some(Directive::Uppercase), _ => None, @@ -375,7 +378,7 @@ fn parse_placeholder_directive_key(input: Span) -> IResult IResult> { preceded( tuple((multispace0, tag("|"), multispace0)), - parse_placeholder_directive_key + parse_placeholder_directive_enum )(input) } @@ -920,6 +923,15 @@ mod tests { assert_eq!(placeholders[0].directives, vec![Directive::Uppercase, Directive::Lowercase]); } + #[test] + fn can_parse_placeholder_directive_with_arg() { + let input = Span::new("date: dd-mmm-yyyy"); + let (_, directive) = parse_placeholder_directive_enum(input).expect("to parse directive"); + assert!(directive.is_some()); + let directive = directive.unwrap(); + assert_eq!(directive, Directive::Date("dd-mmm-yyyy".to_string())); + } + #[test] fn can_replace_placeholder_from_meta() { let input = Span::new("\ntitle = Meta title\nĀ£author = John Doe\n\n# Markdown title\nThis is my content");