diff --git a/CHANGELOG b/CHANGELOG index 6b01b79..7d44613 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +0.6.2 +----- +Make decorators work like functions + 0.6.1 ----- Remove debug output diff --git a/Cargo.lock b/Cargo.lock index 64a7349..51e24d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -518,7 +518,7 @@ dependencies = [ [[package]] name = "lavendeux-parser" -version = "0.6.1" +version = "0.6.2" dependencies = [ "chrono", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index 2d7263c..347d93e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ categories = ["parser-implementations", "development-tools", "command-line-utili homepage = "https://rscarson.github.io/Lavendeux/" repository = "https://github.com/rscarson/lavendeux-parser" readme = "readme.md" -version = "0.6.1" +version = "0.6.2" edition = "2021" [features] diff --git a/examples/adding_functionality.rs b/examples/adding_functionality.rs index a1db10d..0fd449b 100644 --- a/examples/adding_functionality.rs +++ b/examples/adding_functionality.rs @@ -1,26 +1,34 @@ use lavendeux_parser::{ParserState, ParserError, Token, Value}; -use lavendeux_parser::{FunctionDefinition, FunctionArgument}; +use lavendeux_parser::{DecoratorDefinition, FunctionDefinition, FunctionArgument}; use lavendeux_parser::errors::*; -const ECHO : FunctionDefinition = FunctionDefinition { - name: "echo", - description: "Echo back the provided input", - arguments: || vec![ - FunctionArgument::new_required("input", ExpectedTypes::String), - ], - handler: |_, args: &[Value]| { - Ok(Value::String(args[0].as_string())) - } -}; - fn main() -> Result<(), ParserError> { // Load the extensions into our parser let mut state : ParserState = ParserState::new(); - state.functions.register(ECHO); + + // Register a new function + state.functions.register(FunctionDefinition { + name: "echo", + description: "Echo back the provided input", + arguments: || vec![ + FunctionArgument::new_required("input", ExpectedTypes::String), + ], + handler: |_, args: &[Value]| { + Ok(Value::String(args[0].as_string())) + } + }); + + // Register a new decorator + state.decorators.register(DecoratorDefinition { + name: &["upper", "uppercase"], + description: "Outputs an uppercase version of the input", + argument: ExpectedTypes::Any, + handler: |_, input| Ok(input.as_string().to_uppercase()) + }); // Now we can use the new functions and @decorators - let token = Token::new("echo(5)", &mut state)?; - assert_eq!(token.text(), "5"); + let token = Token::new("echo('test') @upper", &mut state)?; + assert_eq!(token.text(), "TEST"); Ok(()) } \ No newline at end of file diff --git a/src/decorators.rs b/src/decorators.rs index 703f5c0..7ae26eb 100644 --- a/src/decorators.rs +++ b/src/decorators.rs @@ -116,8 +116,7 @@ impl DecoratorDefinition { /// Return the decorator's signature pub fn signature(&self) -> String { - let name = self.name.into_iter().map(|n|format!("@{n}")).collect::>().join("/"); - format!("{}", name) + self.name.iter().map(|n|format!("@{n}")).collect::>().join("/") } /// Return the decorator's signature diff --git a/src/functions/math.rs b/src/functions/math.rs index 449011c..cd421f2 100644 --- a/src/functions/math.rs +++ b/src/functions/math.rs @@ -9,7 +9,7 @@ const MIN : FunctionDefinition = FunctionDefinition { FunctionArgument::new_plural("n", ExpectedTypes::IntOrFloat, false), ], handler: |_, args: &[Value]| { - let mut valid_args = args.into_iter().filter(|a|!a.as_float().unwrap().is_nan()).map(|a|a.clone()).collect::>(); + let mut valid_args = args.iter().filter(|a|!a.as_float().unwrap().is_nan()).cloned().collect::>(); valid_args.sort_by(|a,b| a.as_float().unwrap().partial_cmp(&b.as_float().unwrap()).unwrap()); if valid_args.is_empty() { Ok(args[0].clone()) @@ -26,7 +26,7 @@ const MAX : FunctionDefinition = FunctionDefinition { FunctionArgument::new_plural("n", ExpectedTypes::IntOrFloat, false), ], handler: |_, args: &[Value]| { - let mut valid_args = args.into_iter().filter(|a|!a.as_float().unwrap().is_nan()).map(|a|a.clone()).collect::>(); + let mut valid_args = args.iter().filter(|a|!a.as_float().unwrap().is_nan()).cloned().collect::>(); valid_args.sort_by(|a,b| b.as_float().unwrap().partial_cmp(&a.as_float().unwrap()).unwrap()); if valid_args.is_empty() { Ok(args[0].clone()) diff --git a/src/lib.rs b/src/lib.rs index e0d5ae1..ad194b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,7 @@ //! } //! ``` //! Extensions give a more flexible way of adding functionality at runtime. Extensions are written in javascript. -#![doc(html_root_url = "https://docs.rs/lavendeux-parser/0.6.1")] +#![doc(html_root_url = "https://docs.rs/lavendeux-parser/0.6.2")] #![warn(missing_docs)] #![warn(rustdoc::missing_doc_code_examples)] diff --git a/src/token.rs b/src/token.rs index bdbfc77..45102e4 100644 --- a/src/token.rs +++ b/src/token.rs @@ -513,7 +513,6 @@ mod test_token { )); let t = Token::new("help()", &mut state).unwrap(); - assert_eq!(t.text(), "".to_string()); assert_eq!(true, t.text.contains("Built-in Functions")); assert_eq!(true, t.text.contains("Built-in Decorators"));