moved logic to a better place
This commit is contained in:
14
src/lib.rs
14
src/lib.rs
@@ -33,13 +33,17 @@ impl StateOwner {
|
|||||||
StateKeeper { state: self.state.write().unwrap() }
|
StateKeeper { state: self.state.write().unwrap() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_transactions(&self, state_keeper: &StateKeeper, change_queue: &[StateModifier]) -> bool {
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl StateKeeper<'_> {
|
||||||
|
pub fn check_transactions(&self, change_queue: &[StateModifier]) -> bool {
|
||||||
for modifier in change_queue {
|
for modifier in change_queue {
|
||||||
match modifier {
|
match modifier {
|
||||||
//ReplaceFull will always be valid
|
//ReplaceFull will always be valid
|
||||||
StateModifier::ReplaceFull(_) => {},
|
StateModifier::ReplaceFull(_) => {},
|
||||||
StateModifier::ReplaceAt(pos, c) => {
|
StateModifier::ReplaceAt(pos, c) => {
|
||||||
if pos > &state_keeper.state.len() {
|
if pos > &self.state.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if !c.is_ascii_alphanumeric() {
|
if !c.is_ascii_alphanumeric() {
|
||||||
@@ -51,9 +55,9 @@ impl StateOwner {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close_transaction(&self, state_keeper: StateKeeper, change_queue: Vec<StateModifier>) {
|
pub fn close_transaction(self, change_queue: Vec<StateModifier>) {
|
||||||
|
|
||||||
let mut state = state_keeper.state;
|
let mut state = self.state;
|
||||||
|
|
||||||
for modifier in change_queue {
|
for modifier in change_queue {
|
||||||
print!("Applying modifier: ");
|
print!("Applying modifier: ");
|
||||||
@@ -76,10 +80,8 @@ impl StateOwner {
|
|||||||
println!("After: {state}");
|
println!("After: {state}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum StateModifier {
|
pub enum StateModifier {
|
||||||
ReplaceFull(String),
|
ReplaceFull(String),
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ fn create_and_modify_state_owner() {
|
|||||||
|
|
||||||
//check individually
|
//check individually
|
||||||
for modifier in &modifiers {
|
for modifier in &modifiers {
|
||||||
let is_valid = example.check_transactions(&keeper, std::slice::from_ref(modifier));
|
let is_valid = keeper.check_transactions(std::slice::from_ref(modifier));
|
||||||
println!("{modifier:?} - Valid? {is_valid}");
|
println!("{modifier:?} - Valid? {is_valid}");
|
||||||
|
|
||||||
assert!(is_valid);
|
assert!(is_valid);
|
||||||
@@ -32,13 +32,13 @@ fn create_and_modify_state_owner() {
|
|||||||
|
|
||||||
//check all at once
|
//check all at once
|
||||||
|
|
||||||
let is_valid = example.check_transactions(&keeper, modifiers.as_slice());
|
let is_valid = keeper.check_transactions(modifiers.as_slice());
|
||||||
|
|
||||||
println!("All valid in one shot? {is_valid}");
|
println!("All valid in one shot? {is_valid}");
|
||||||
|
|
||||||
assert!(is_valid);
|
assert!(is_valid);
|
||||||
|
|
||||||
example.close_transaction(keeper, modifiers);
|
keeper.close_transaction(modifiers);
|
||||||
|
|
||||||
println!("Current State? {}", example.get_state());
|
println!("Current State? {}", example.get_state());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user