-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(funcs): introduce hash() function
- Loading branch information
1 parent
6a6612b
commit fd60d2b
Showing
9 changed files
with
154 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
use crate::core::{Arg, Expr, Func}; | ||
use crate::funcs::{to_camel_case, to_snake_case}; | ||
use crate::core::{Arg, Expr, Func, State}; | ||
use crate::funcs::{hash, to_camel_case, to_snake_case}; | ||
|
||
/// A syntactic structure that can be evaluated. | ||
pub trait Eval { | ||
fn eval(&self) -> String; | ||
fn eval(&self, state: &State) -> String; | ||
} | ||
|
||
impl Eval for Arg { | ||
fn eval(&self) -> String { | ||
fn eval(&self, _: &State) -> String { | ||
self.value.clone() | ||
} | ||
} | ||
|
||
impl Eval for Func { | ||
fn eval(&self) -> String { | ||
fn eval(&self, state: &State) -> String { | ||
match self { | ||
Func::Upper(expr) => expr.eval().to_uppercase(), | ||
Func::Lower(expr) => expr.eval().to_lowercase(), | ||
Func::SnakeCase(expr) => to_snake_case(expr.eval().as_str()), | ||
Func::CamelCase(expr) => to_camel_case(expr.eval().as_str()), | ||
Func::Upper(expr) => expr.eval(state).to_uppercase(), | ||
Func::Lower(expr) => expr.eval(state).to_lowercase(), | ||
Func::SnakeCase(expr) => to_snake_case(expr.eval(state).as_str()), | ||
Func::CamelCase(expr) => to_camel_case(expr.eval(state).as_str()), | ||
Func::Hash(expr) => hash(expr.eval(state).as_str(), state), | ||
} | ||
} | ||
} | ||
|
||
impl Eval for Expr { | ||
fn eval(&self) -> String { | ||
fn eval(&self, state: &State) -> String { | ||
match self { | ||
Expr::ArgExpr { value } => value.eval(), | ||
Expr::FuncCallExpr { value } => value.eval(), | ||
Expr::ArgExpr { value } => value.eval(state), | ||
Expr::FuncCallExpr { value } => value.eval(state), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use compose_idents::compose_idents; | ||
|
||
compose_idents!( | ||
my_unique_var = [foo, _, hash(1)]; | ||
my_var = [SPAM, _, EGGS]; { | ||
const my_unique_var: u32 = 42; | ||
const my_var: u32 = my_unique_var; | ||
}); | ||
|
||
compose_idents!( | ||
my_unique_var = [foo, _, hash(1)]; | ||
my_var = [BORK, _, GORK]; { | ||
const my_unique_var: u32 = 42; | ||
const my_var: u32 = my_unique_var; | ||
}); | ||
|
||
macro_rules! my_macro { | ||
() => { | ||
compose_idents!( | ||
my_local = [foo, _, hash(1)]; | ||
my_same_local = [foo, _, hash(1)]; | ||
my_other_local = [foo, _, hash(2)]; { | ||
let my_local: u32 = 42; | ||
let my_other_local: u32 = my_same_local; | ||
let comparison: bool = my_local == my_other_local; | ||
|
||
assert!(comparison); | ||
}); | ||
}; | ||
} | ||
|
||
fn main() { | ||
assert_eq!(SPAM_EGGS, 42); | ||
assert_eq!(BORK_GORK, 42); | ||
|
||
my_macro!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters