Skip to content

Commit

Permalink
feat: make it possible to pass integers as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiPashkin committed Feb 10, 2025
1 parent 80a7382 commit 6a6612b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Docs/tests fixes.
- Added "functions" functionality that allows to apply functions over arguments.
- Made it possible to pass integers as arguments.
- Added "upper()", "lower()", "snake_case()" and "camel_case()" functions for case-manipulation.

### Changed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use compose_idents::compose_idents;

compose_idents!(
my_fn_1 = [foo, _, "baz"];
my_fn_2 = [spam, _, eggs];
my_fn_2 = [spam, _, 1, _, eggs];
my_const = [upper(foo), _, lower(BAR)];
my_static = [upper(lower(BAR))];
MY_SNAKE_CASE_STATIC = [snake_case(snakeCase)];
Expand Down Expand Up @@ -66,7 +66,7 @@ macro_rules! outer_macro {
outer_macro!(foo);

assert_eq!(foo_baz(), 123);
assert_eq!(spam_eggs(), 321);
assert_eq!(spam_1_eggs(), 321);
assert_eq!(nested_foo(), 42);
assert_eq!(FOO_bar, 42);
assert_eq!(BAR, 42);
Expand Down
4 changes: 2 additions & 2 deletions snippets/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use compose_idents::compose_idents;

compose_idents!(
my_fn_1 = [foo, _, "baz"];
my_fn_2 = [spam, _, eggs];
my_fn_2 = [spam, _, 1, _, eggs];
my_const = [upper(foo), _, lower(BAR)];
my_static = [upper(lower(BAR))];
MY_SNAKE_CASE_STATIC = [snake_case(snakeCase)];
Expand Down Expand Up @@ -34,7 +34,7 @@ macro_rules! outer_macro {
outer_macro!(foo);

assert_eq!(foo_baz(), 123);
assert_eq!(spam_eggs(), 321);
assert_eq!(spam_1_eggs(), 321);
assert_eq!(nested_foo(), 42);
assert_eq!(FOO_bar, 42);
assert_eq!(BAR, 42);
Expand Down
9 changes: 7 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ impl Parse for Arg {
if input.peek(syn::Ident) && !input.peek2(syn::token::Paren) {
let ident = input.parse::<syn::Ident>()?;
value = ident.to_string();
} else if input.parse::<syn::Token![_]>().is_ok() {
} else if input.peek(syn::Token![_]) {
input.parse::<syn::Token![_]>()?;
value = "_".to_string();
} else if let Ok(lit_str) = input.parse::<syn::LitStr>() {
} else if input.peek(syn::LitStr) {
let lit_str = input.parse::<syn::LitStr>()?;
value = lit_str.value();
} else if input.peek(syn::LitInt) {
let lit_int = input.parse::<syn::LitInt>()?;
value = lit_int.base10_digits().to_string();
} else {
return Err(input.error("Expected identifier or _"));
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ impl Parse for IdentSpec {
bracketed!(content in input);
let mut exprs = Vec::new();
loop {
if let Ok(expr) = content.parse::<Expr>() {
exprs.push(expr);
} else {
return Err(content.error("Invalid expression."));
match content.parse::<Expr>() {
Ok(expr) => exprs.push(expr),
Err(err) => return Err(err),
}
if content.is_empty() {
break;
Expand Down
11 changes: 11 additions & 0 deletions tests/compile/num_compose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use compose_idents::compose_idents;

compose_idents!(my_fn = [foo, _, 1, _, bar]; {
fn my_fn() -> u32 {
42
}
});

fn main() {
assert_eq!(foo_1_bar(), 42);
}
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn compile_tests() {
t.pass("tests/compile/multi_compose.rs");
t.pass("tests/compile/const_var_compose.rs");
t.pass("tests/compile/generic_param_compose.rs");
t.pass("tests/compile/num_compose.rs");
t.pass("tests/compile/funcs/upper.rs");
t.pass("tests/compile/funcs/lower.rs");
t.pass("tests/compile/funcs/nested.rs");
Expand Down

0 comments on commit 6a6612b

Please sign in to comment.