Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commands] Add CommandStack #331 #332

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
agdb_db_error = { path = "../db_error", version = "<=0.1.0" }
15 changes: 15 additions & 0 deletions crates/commands/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::Commands;

pub(crate) struct Command {
pub(crate) commands: Commands,
pub(crate) executed: bool,
}

impl Command {
pub(crate) fn new(commands: Commands) -> Self {
Self {
commands,
executed: false,
}
}
}
7 changes: 7 additions & 0 deletions crates/commands/src/command_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::Commands;
use agdb_db_error::DbError;

pub trait CommandExecutor {
fn redo(&mut self, command: &mut Commands) -> Result<(), DbError>;
fn undo(&mut self, command: &mut Commands) -> Result<(), DbError>;
}
51 changes: 51 additions & 0 deletions crates/commands/src/command_stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::command::Command;
use crate::command_executor::CommandExecutor;
use crate::commands::Commands;
use agdb_db_error::DbError;

#[derive(Default)]
pub struct CommandStack {
stack: Vec<Command>,
}

impl CommandStack {
pub fn new() -> Self {
Self::default()
}

pub fn clear(&mut self) {
self.stack.clear();
}

pub fn push(&mut self, command: Commands) {
self.stack.push(Command::new(command));
}

pub fn redo<Executor: CommandExecutor>(
&mut self,
executor: &mut Executor,
) -> Result<(), DbError> {
for command in self.stack.iter_mut() {
if !command.executed {
executor.redo(&mut command.commands)?;
command.executed = true;
}
}

Ok(())
}

pub fn undo<Executor: CommandExecutor>(
&mut self,
executor: &mut Executor,
) -> Result<(), DbError> {
for command in self.stack.iter_mut().rev() {
if command.executed {
executor.undo(&mut command.commands)?;
command.executed = false;
}
}

Ok(())
}
}
5 changes: 5 additions & 0 deletions crates/commands/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
mod command;
mod command_executor;
mod command_stack;
mod commands;

pub use command_executor::CommandExecutor;
pub use command_stack::CommandStack;
pub use commands::Commands;
106 changes: 106 additions & 0 deletions crates/commands/tests/command_stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use agdb_commands::CommandExecutor;
use agdb_commands::CommandStack;
use agdb_commands::Commands;
use agdb_db_error::DbError;

#[derive(Default)]
struct Executor {
redo_result: Vec<Result<(), DbError>>,
undo_result: Vec<Result<(), DbError>>,
}

impl CommandExecutor for Executor {
fn redo(&mut self, _command: &mut Commands) -> Result<(), DbError> {
self.redo_result.pop().unwrap()
}

fn undo(&mut self, _command: &mut Commands) -> Result<(), DbError> {
self.undo_result.pop().unwrap()
}
}

#[test]
fn clear() {
let mut stack = CommandStack::new();
stack.clear();
stack.push(Commands::InsertNode);
stack.clear();

let mut executor = Executor {
redo_result: vec![],
undo_result: vec![],
};

assert_eq!(stack.redo(&mut executor), Ok(()));
}

#[test]
fn derived_from_default() {
let mut _stack = CommandStack::default();
}

#[test]
fn empty() {
let mut stack = CommandStack::new();

let mut executor = Executor {
redo_result: vec![],
undo_result: vec![],
};

assert_eq!(stack.redo(&mut executor), Ok(()));
assert_eq!(stack.undo(&mut executor), Ok(()));
}

#[test]
fn redo() {
let mut stack = CommandStack::new();
stack.push(Commands::InsertNode);
stack.push(Commands::InsertNode);
stack.push(Commands::InsertEdge);

let mut executor = Executor {
redo_result: vec![Ok(()), Ok(()), Ok(())],
undo_result: vec![],
};

assert_eq!(stack.redo(&mut executor), Ok(()));
assert_eq!(stack.redo(&mut executor), Ok(()));

assert!(executor.redo_result.is_empty());
}

#[test]
fn undo() {
let mut stack = CommandStack::new();
stack.push(Commands::InsertNode);
stack.push(Commands::InsertNode);
stack.push(Commands::InsertEdge);

let mut executor = Executor {
redo_result: vec![Err(DbError::from("error")), Ok(()), Ok(())],
undo_result: vec![Ok(()), Ok(())],
};

assert_eq!(stack.redo(&mut executor), Err(DbError::from("error")));
assert_eq!(stack.undo(&mut executor), Ok(()));
assert_eq!(stack.undo(&mut executor), Ok(()));

assert!(executor.redo_result.is_empty());
assert!(executor.undo_result.is_empty());
}

#[test]
fn undo_not_redone() {
let mut stack = CommandStack::new();
stack.push(Commands::InsertNode);
stack.push(Commands::InsertNode);
stack.push(Commands::InsertEdge);

let mut executor = Executor {
redo_result: vec![],
undo_result: vec![],
};

assert_eq!(stack.undo(&mut executor), Ok(()));
}
7 changes: 0 additions & 7 deletions crates/commands/tests/commands_test.rs

This file was deleted.