-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Add
Rule
type + module to durin_primitives
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! This module contains the [Rule] type as well as several helper macros for applying | ||
//! rules on top of one another. | ||
type Rule<T> = Box<dyn Fn(T) -> anyhow::Result<T>>; | ||
|
||
#[macro_export] | ||
macro_rules! chain_rules { | ||
($state:expr, $($rule:expr),+) => {{ | ||
let mut result = Ok($state); | ||
|
||
$( | ||
result = match result { | ||
Ok(val) => $rule(val), | ||
err @ Err(_) => err, | ||
}; | ||
)+ | ||
|
||
result | ||
}}; | ||
} | ||
|
||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn apply_sequential_rules() { | ||
let state = 5; | ||
|
||
let rule_lt_10: Rule<u32> = Box::new(|state: u32| { | ||
if state < 10 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be less than 10")) | ||
} | ||
}); | ||
let rule_double_10: Rule<u32> = Box::new(|state: u32| { | ||
if state * 2 == 10 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be half of 10")) | ||
} | ||
}); | ||
let rule_bitwise: Rule<u32> = Box::new(|state: u32| { | ||
if state & 0xF == 0b0101 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be 5")) | ||
} | ||
}); | ||
|
||
let result = chain_rules!(state, rule_lt_10, rule_double_10, rule_bitwise); | ||
assert!(result.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn fail_sequential_rules() { | ||
let state = 5; | ||
|
||
let rule_lt_10: Rule<u32> = Box::new(|state: u32| { | ||
if state < 10 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be less than 10")) | ||
} | ||
}); | ||
let rule_double_11: Rule<u32> = Box::new(|state: u32| { | ||
if state * 2 == 11 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be half of 11")) | ||
} | ||
}); | ||
let rule_bitwise: Rule<u32> = Box::new(|state: u32| { | ||
if state & 0xF == 0b0101 { | ||
Ok(state) | ||
} else { | ||
Err(anyhow::anyhow!("state must be 5")) | ||
} | ||
}); | ||
|
||
let result = chain_rules!(state, rule_lt_10, rule_double_11, rule_bitwise); | ||
assert!(result.is_err()); | ||
} | ||
} |