2024-04-29 23:34:04 +02:00
|
|
|
use std::collections::BTreeMap;
|
2024-04-29 21:48:01 +02:00
|
|
|
|
2024-05-11 23:23:00 +02:00
|
|
|
use hyperlog_core::log::{GraphItem, ItemState};
|
2024-04-29 21:48:01 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2024-05-11 23:23:00 +02:00
|
|
|
use crate::{events::Events, shared_engine::SharedEngine, storage::Storage};
|
2024-04-29 21:48:01 +02:00
|
|
|
|
|
|
|
#[derive(Serialize, PartialEq, Eq, Debug, Clone)]
|
|
|
|
pub enum Command {
|
|
|
|
CreateRoot {
|
|
|
|
root: String,
|
|
|
|
},
|
|
|
|
CreateSection {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
},
|
|
|
|
CreateItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
title: String,
|
|
|
|
description: String,
|
|
|
|
state: ItemState,
|
|
|
|
},
|
2024-05-10 12:03:38 +02:00
|
|
|
UpdateItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
title: String,
|
|
|
|
description: String,
|
|
|
|
state: ItemState,
|
|
|
|
},
|
2024-05-09 17:02:06 +02:00
|
|
|
ToggleItem {
|
|
|
|
root: String,
|
|
|
|
path: Vec<String>,
|
|
|
|
},
|
2024-04-29 21:48:01 +02:00
|
|
|
Move {
|
|
|
|
root: String,
|
|
|
|
src: Vec<String>,
|
|
|
|
dest: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Commander {
|
2024-04-29 23:34:04 +02:00
|
|
|
engine: SharedEngine,
|
|
|
|
storage: Storage,
|
2024-04-29 21:48:01 +02:00
|
|
|
events: Events,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Commander {
|
2024-04-29 23:34:04 +02:00
|
|
|
pub fn new(engine: SharedEngine, storage: Storage, events: Events) -> anyhow::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
engine,
|
|
|
|
storage,
|
|
|
|
events,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:48:01 +02:00
|
|
|
pub fn execute(&self, cmd: Command) -> anyhow::Result<()> {
|
|
|
|
tracing::debug!("executing event: {}", serde_json::to_string(&cmd)?);
|
|
|
|
|
2024-04-29 23:34:04 +02:00
|
|
|
match cmd.clone() {
|
2024-04-29 21:48:01 +02:00
|
|
|
Command::CreateRoot { root } => {
|
2024-04-29 23:34:04 +02:00
|
|
|
self.engine.create_root(&root)?;
|
2024-04-29 21:48:01 +02:00
|
|
|
}
|
|
|
|
Command::CreateSection { root, path } => {
|
2024-04-29 23:34:04 +02:00
|
|
|
self.engine.create(
|
2024-04-29 21:48:01 +02:00
|
|
|
&root,
|
|
|
|
&path.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
|
|
|
GraphItem::Section(BTreeMap::default()),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Command::CreateItem {
|
|
|
|
root,
|
|
|
|
path,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
state,
|
2024-04-29 23:34:04 +02:00
|
|
|
} => self.engine.create(
|
2024-04-29 21:48:01 +02:00
|
|
|
&root,
|
|
|
|
&path.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
|
|
|
GraphItem::Item {
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
state,
|
|
|
|
},
|
|
|
|
)?,
|
2024-04-29 23:34:04 +02:00
|
|
|
Command::Move { root, src, dest } => self.engine.section_move(
|
2024-04-29 21:48:01 +02:00
|
|
|
&root,
|
|
|
|
&src.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
|
|
|
&dest.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
|
|
|
)?,
|
2024-05-09 17:02:06 +02:00
|
|
|
Command::ToggleItem { root, path } => self
|
|
|
|
.engine
|
|
|
|
.toggle_item(&root, &path.iter().map(|p| p.as_str()).collect::<Vec<_>>())?,
|
2024-05-10 12:03:38 +02:00
|
|
|
Command::UpdateItem {
|
|
|
|
root,
|
|
|
|
path,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
state,
|
|
|
|
} => self.engine.update_item(
|
|
|
|
&root,
|
|
|
|
&path.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
|
|
|
GraphItem::Item {
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
state,
|
|
|
|
},
|
|
|
|
)?,
|
2024-04-29 21:48:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 23:34:04 +02:00
|
|
|
self.storage.store(&self.engine)?;
|
|
|
|
|
|
|
|
self.events.enque_command(cmd)?;
|
|
|
|
|
2024-04-29 21:48:01 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|