Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed May 5, 2022
1 parent c4996a6 commit 00632d6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.6.2
-----
Make decorators work like functions

0.6.1
-----
Remove debug output
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
38 changes: 23 additions & 15 deletions examples/adding_functionality.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
3 changes: 1 addition & 2 deletions src/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>().join("/");
format!("{}", name)
self.name.iter().map(|n|format!("@{n}")).collect::<Vec<String>>().join("/")
}

/// Return the decorator's signature
Expand Down
4 changes: 2 additions & 2 deletions src/functions/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<Value>>();
let mut valid_args = args.iter().filter(|a|!a.as_float().unwrap().is_nan()).cloned().collect::<Vec<Value>>();
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())
Expand All @@ -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::<Vec<Value>>();
let mut valid_args = args.iter().filter(|a|!a.as_float().unwrap().is_nan()).cloned().collect::<Vec<Value>>();
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())
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
1 change: 0 additions & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down

0 comments on commit 00632d6

Please sign in to comment.