moved logic to a better place

This commit is contained in:
2024-09-07 18:13:14 -04:00
parent 3f063eb88b
commit 15d4a1e563
2 changed files with 11 additions and 9 deletions

View File

@@ -33,13 +33,17 @@ impl StateOwner {
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 {
match modifier {
//ReplaceFull will always be valid
StateModifier::ReplaceFull(_) => {},
StateModifier::ReplaceAt(pos, c) => {
if pos > &state_keeper.state.len() {
if pos > &self.state.len() {
return false;
}
if !c.is_ascii_alphanumeric() {
@@ -51,9 +55,9 @@ impl StateOwner {
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 {
print!("Applying modifier: ");
@@ -76,10 +80,8 @@ impl StateOwner {
println!("After: {state}");
}
}
}
#[derive(Debug)]
pub enum StateModifier {
ReplaceFull(String),

View File

@@ -24,7 +24,7 @@ fn create_and_modify_state_owner() {
//check individually
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}");
assert!(is_valid);
@@ -32,13 +32,13 @@ fn create_and_modify_state_owner() {
//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}");
assert!(is_valid);
example.close_transaction(keeper, modifiers);
keeper.close_transaction(modifiers);
println!("Current State? {}", example.get_state());