feat: move core to tui and begin grpc work
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -5,7 +5,7 @@ use ratatui::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
command_parser::CommandParser, commands::IntoCommand,
|
||||
command_parser::CommandParser, commander, commands::IntoCommand,
|
||||
components::graph_explorer::GraphExplorer, state::SharedState, Msg,
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ pub enum Dialog {
|
||||
}
|
||||
|
||||
impl Dialog {
|
||||
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
|
||||
pub fn get_command(&self) -> Option<commander::Command> {
|
||||
match self {
|
||||
Dialog::CreateItem { state } => state.get_command(),
|
||||
Dialog::EditItem { state } => state.get_command(),
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use itertools::Itertools;
|
||||
use ratatui::{prelude::*, widgets::*};
|
||||
|
||||
use crate::models::Msg;
|
||||
use crate::{commander, models::Msg};
|
||||
|
||||
use super::{InputBuffer, InputField};
|
||||
|
||||
@@ -61,7 +61,7 @@ impl CreateItemState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
|
||||
pub fn get_command(&self) -> Option<commander::Command> {
|
||||
let title = self.title.string();
|
||||
let description = self.description.string();
|
||||
|
||||
@@ -69,7 +69,7 @@ impl CreateItemState {
|
||||
let mut path = self.path.clone();
|
||||
path.push(title.replace([' ', '.'], "-"));
|
||||
|
||||
Some(hyperlog_core::commander::Command::CreateItem {
|
||||
Some(commander::Command::CreateItem {
|
||||
root: self.root.clone(),
|
||||
path,
|
||||
title: title.trim().into(),
|
||||
|
@@ -2,7 +2,7 @@ use hyperlog_core::log::GraphItem;
|
||||
use itertools::Itertools;
|
||||
use ratatui::{prelude::*, widgets::*};
|
||||
|
||||
use crate::models::Msg;
|
||||
use crate::{commander, models::Msg};
|
||||
|
||||
use super::{InputBuffer, InputField};
|
||||
|
||||
@@ -82,14 +82,14 @@ impl EditItemState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_command(&self) -> Option<hyperlog_core::commander::Command> {
|
||||
pub fn get_command(&self) -> Option<commander::Command> {
|
||||
let title = self.title.string();
|
||||
let description = self.description.string();
|
||||
|
||||
if !title.is_empty() {
|
||||
let path = self.path.clone();
|
||||
|
||||
Some(hyperlog_core::commander::Command::UpdateItem {
|
||||
Some(commander::Command::UpdateItem {
|
||||
root: self.root.clone(),
|
||||
path,
|
||||
title: title.trim().into(),
|
||||
|
117
crates/hyperlog-tui/src/commander.rs
Normal file
117
crates/hyperlog-tui/src/commander.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use hyperlog_core::log::{GraphItem, ItemState};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{events::Events, shared_engine::SharedEngine, storage::Storage};
|
||||
|
||||
#[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,
|
||||
},
|
||||
UpdateItem {
|
||||
root: String,
|
||||
path: Vec<String>,
|
||||
title: String,
|
||||
description: String,
|
||||
state: ItemState,
|
||||
},
|
||||
ToggleItem {
|
||||
root: String,
|
||||
path: Vec<String>,
|
||||
},
|
||||
Move {
|
||||
root: String,
|
||||
src: Vec<String>,
|
||||
dest: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct Commander {
|
||||
engine: SharedEngine,
|
||||
storage: Storage,
|
||||
events: Events,
|
||||
}
|
||||
|
||||
impl Commander {
|
||||
pub fn new(engine: SharedEngine, storage: Storage, events: Events) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
engine,
|
||||
storage,
|
||||
events,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn execute(&self, cmd: Command) -> anyhow::Result<()> {
|
||||
tracing::debug!("executing event: {}", serde_json::to_string(&cmd)?);
|
||||
|
||||
match cmd.clone() {
|
||||
Command::CreateRoot { root } => {
|
||||
self.engine.create_root(&root)?;
|
||||
}
|
||||
Command::CreateSection { root, path } => {
|
||||
self.engine.create(
|
||||
&root,
|
||||
&path.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)?;
|
||||
}
|
||||
Command::CreateItem {
|
||||
root,
|
||||
path,
|
||||
title,
|
||||
description,
|
||||
state,
|
||||
} => self.engine.create(
|
||||
&root,
|
||||
&path.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
||||
GraphItem::Item {
|
||||
title,
|
||||
description,
|
||||
state,
|
||||
},
|
||||
)?,
|
||||
Command::Move { root, src, dest } => self.engine.section_move(
|
||||
&root,
|
||||
&src.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
||||
&dest.iter().map(|p| p.as_str()).collect::<Vec<_>>(),
|
||||
)?,
|
||||
Command::ToggleItem { root, path } => self
|
||||
.engine
|
||||
.toggle_item(&root, &path.iter().map(|p| p.as_str()).collect::<Vec<_>>())?,
|
||||
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,
|
||||
},
|
||||
)?,
|
||||
}
|
||||
|
||||
self.storage.store(&self.engine)?;
|
||||
|
||||
self.events.enque_command(cmd)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@@ -3,7 +3,7 @@ use hyperlog_core::log::GraphItem;
|
||||
use ratatui::{prelude::*, widgets::*};
|
||||
|
||||
use crate::{
|
||||
command_parser::Commands, components::movement_graph::GraphItemType, models::Msg,
|
||||
command_parser::Commands, commander, components::movement_graph::GraphItemType, models::Msg,
|
||||
state::SharedState,
|
||||
};
|
||||
|
||||
@@ -196,12 +196,12 @@ impl<'a> GraphExplorer<'a> {
|
||||
let mut path = self.get_current_path();
|
||||
path.push(name.replace(" ", "-").replace(".", "-"));
|
||||
|
||||
self.state.commander.execute(
|
||||
hyperlog_core::commander::Command::CreateSection {
|
||||
self.state
|
||||
.commander
|
||||
.execute(commander::Command::CreateSection {
|
||||
root: self.inner.root.clone(),
|
||||
path,
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
}
|
||||
}
|
||||
Commands::Edit => {
|
||||
@@ -247,7 +247,7 @@ impl<'a> GraphExplorer<'a> {
|
||||
|
||||
self.state
|
||||
.commander
|
||||
.execute(hyperlog_core::commander::Command::ToggleItem {
|
||||
.execute(commander::Command::ToggleItem {
|
||||
root: self.inner.root.to_string(),
|
||||
path: self.get_current_path(),
|
||||
})?;
|
||||
|
32
crates/hyperlog-tui/src/core_state.rs
Normal file
32
crates/hyperlog-tui/src/core_state.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use crate::{
|
||||
commander::Commander, events::Events, querier::Querier, shared_engine::SharedEngine,
|
||||
storage::Storage,
|
||||
};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct State {
|
||||
engine: SharedEngine,
|
||||
pub storage: Storage,
|
||||
events: Events,
|
||||
|
||||
pub commander: Commander,
|
||||
pub querier: Querier,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
let storage = Storage::new();
|
||||
let engine = storage.load()?;
|
||||
let events = Events::default();
|
||||
let engine = SharedEngine::from(engine);
|
||||
|
||||
Ok(Self {
|
||||
engine: engine.clone(),
|
||||
storage: storage.clone(),
|
||||
events: events.clone(),
|
||||
|
||||
commander: Commander::new(engine.clone(), storage, events)?,
|
||||
querier: Querier::new(engine),
|
||||
})
|
||||
}
|
||||
}
|
760
crates/hyperlog-tui/src/engine.rs
Normal file
760
crates/hyperlog-tui/src/engine.rs
Normal file
@@ -0,0 +1,760 @@
|
||||
use std::{collections::BTreeMap, fmt::Display};
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use hyperlog_core::log::{Graph, GraphItem, ItemState};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Engine {
|
||||
graph: Graph,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn engine_from_str(input: &str) -> anyhow::Result<Self> {
|
||||
let graph: Graph = serde_json::from_str(input)?;
|
||||
|
||||
Ok(Self { graph })
|
||||
}
|
||||
|
||||
pub fn to_str(&self) -> anyhow::Result<String> {
|
||||
serde_json::to_string_pretty(&self.graph).context("failed to serialize graph")
|
||||
}
|
||||
|
||||
pub fn create_root(&mut self, root: &str) -> anyhow::Result<()> {
|
||||
self.graph
|
||||
.try_insert(root.to_string(), GraphItem::User(BTreeMap::default()))
|
||||
.map_err(|_| anyhow!("entry was already found, aborting"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn create(&mut self, root: &str, path: &[&str], item: GraphItem) -> anyhow::Result<()> {
|
||||
let graph = &mut self.graph;
|
||||
|
||||
let (last, items) = path.split_last().ok_or(anyhow!(
|
||||
"path cannot be empty, must contain at least one item"
|
||||
))?;
|
||||
|
||||
let root = graph
|
||||
.get_mut(root)
|
||||
.ok_or(anyhow!("root was missing a user, aborting"))?;
|
||||
|
||||
let mut current_item = root;
|
||||
for section in items {
|
||||
match current_item {
|
||||
GraphItem::User(u) => match u.get_mut(section.to_owned()) {
|
||||
Some(graph_item) => {
|
||||
current_item = graph_item;
|
||||
}
|
||||
None => anyhow::bail!("path: {} section was not found", section),
|
||||
},
|
||||
GraphItem::Item { .. } => anyhow::bail!("path: {} was already found", section),
|
||||
GraphItem::Section(s) => match s.get_mut(section.to_owned()) {
|
||||
Some(graph_item) => {
|
||||
current_item = graph_item;
|
||||
}
|
||||
None => anyhow::bail!("path: {} section was not found", section),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
match current_item {
|
||||
GraphItem::User(u) => {
|
||||
u.insert(last.to_string(), item);
|
||||
}
|
||||
GraphItem::Section(s) => {
|
||||
s.insert(last.to_string(), item);
|
||||
}
|
||||
GraphItem::Item { .. } => anyhow::bail!("cannot insert an item into an item"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get(&self, root: &str, path: &[&str]) -> Option<&GraphItem> {
|
||||
let root = self.graph.get(root)?;
|
||||
|
||||
root.get(path)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, root: &str, path: &[&str]) -> Option<&mut GraphItem> {
|
||||
let root = self.graph.get_mut(root)?;
|
||||
|
||||
root.get_mut(path)
|
||||
}
|
||||
|
||||
pub fn take(&mut self, root: &str, path: &[&str]) -> Option<GraphItem> {
|
||||
let root = self.graph.get_mut(root)?;
|
||||
|
||||
root.take(path)
|
||||
}
|
||||
|
||||
pub fn section_move(
|
||||
&mut self,
|
||||
root: &str,
|
||||
src_path: &[&str],
|
||||
dest_path: &[&str],
|
||||
) -> anyhow::Result<()> {
|
||||
let src = self
|
||||
.take(root, src_path)
|
||||
.ok_or(anyhow!("failed to find source path"))?;
|
||||
|
||||
let dest = self
|
||||
.get_mut(root, dest_path)
|
||||
.ok_or(anyhow!("failed to find destination"))?;
|
||||
|
||||
let src_item = src_path
|
||||
.last()
|
||||
.ok_or(anyhow!("src path must have at least one item"))?;
|
||||
|
||||
match dest {
|
||||
GraphItem::User(u) => {
|
||||
u.try_insert(src_item.to_string(), src)
|
||||
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
|
||||
}
|
||||
GraphItem::Section(s) => {
|
||||
s.try_insert(src_item.to_string(), src)
|
||||
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
|
||||
}
|
||||
GraphItem::Item { .. } => {
|
||||
anyhow::bail!("failed to insert src at item, item doesn't support arbitrary items")
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, root: &str, path: &[&str]) -> anyhow::Result<()> {
|
||||
self.take(root, path)
|
||||
.map(|_| ())
|
||||
.ok_or(anyhow!("item was not found"))
|
||||
}
|
||||
|
||||
pub fn toggle_item(&mut self, root: &str, path: &[&str]) -> anyhow::Result<()> {
|
||||
if let Some(item) = self.get_mut(root, path) {
|
||||
match item {
|
||||
GraphItem::Item { state, .. } => match state {
|
||||
ItemState::NotDone => *state = ItemState::Done,
|
||||
ItemState::Done => *state = ItemState::NotDone,
|
||||
},
|
||||
_ => {
|
||||
anyhow::bail!("{}.{:?} is not an item", root, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_item(
|
||||
&mut self,
|
||||
root: &str,
|
||||
path: &[&str],
|
||||
item: &GraphItem,
|
||||
) -> anyhow::Result<()> {
|
||||
if let Some((name, dest_last)) = path.split_last() {
|
||||
if let Some(parent) = self.get_mut(root, dest_last) {
|
||||
match parent {
|
||||
GraphItem::User(s) | GraphItem::Section(s) => {
|
||||
if let Some(mut existing) = s.remove(*name) {
|
||||
match (&mut existing, item) {
|
||||
(
|
||||
GraphItem::Item {
|
||||
title: ex_title,
|
||||
description: ex_desc,
|
||||
state: ex_state,
|
||||
},
|
||||
GraphItem::Item {
|
||||
title,
|
||||
description,
|
||||
state,
|
||||
},
|
||||
) => {
|
||||
ex_title.clone_from(title);
|
||||
ex_desc.clone_from(description);
|
||||
ex_state.clone_from(state);
|
||||
|
||||
let title = title.replace(".", "-");
|
||||
s.insert(title, existing.clone());
|
||||
}
|
||||
_ => {
|
||||
anyhow::bail!(
|
||||
"path: {}.{} found is not an item",
|
||||
root,
|
||||
path.join(".")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GraphItem::Item { .. } => {
|
||||
anyhow::bail!("cannot rename when item is placed in an item")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_roots(&self) -> Option<Vec<String>> {
|
||||
let items = self.graph.keys().cloned().collect::<Vec<_>>();
|
||||
if items.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(items)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Engine {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let output = serde_json::to_string_pretty(&self.graph).unwrap();
|
||||
f.write_str(&output)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use hyperlog_core::log::{GraphItem, ItemState};
|
||||
use similar_asserts::assert_eq;
|
||||
|
||||
use super::Engine;
|
||||
|
||||
#[test]
|
||||
fn test_can_create_root() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user"
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_create_section() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_create_subsection() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_create_item() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-item"],
|
||||
GraphItem::Item {
|
||||
title: "some-title".to_string(),
|
||||
description: "some-description".to_string(),
|
||||
state: ItemState::NotDone,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_create_nested_item() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-item"],
|
||||
GraphItem::Item {
|
||||
title: "some-title".to_string(),
|
||||
description: "some-description".to_string(),
|
||||
state: ItemState::NotDone,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_create_deeply_nested_item() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section", "sub-sub-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&[
|
||||
"some-section",
|
||||
"some-sub-section",
|
||||
"sub-sub-section",
|
||||
"some-item",
|
||||
],
|
||||
GraphItem::Item {
|
||||
title: "some-title".to_string(),
|
||||
description: "some-description".to_string(),
|
||||
state: ItemState::NotDone,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_get_user() {
|
||||
let engine = get_complex_graph();
|
||||
|
||||
let res = engine.get("kjuulh", &[]).unwrap();
|
||||
|
||||
let actual = serde_json::to_string_pretty(res).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_get_first_section() {
|
||||
let engine = get_complex_graph();
|
||||
|
||||
let res = engine.get("kjuulh", &["some-section"]).unwrap();
|
||||
|
||||
let actual = serde_json::to_string_pretty(res).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_get_middle_section() {
|
||||
let engine = get_complex_graph();
|
||||
|
||||
let res = engine
|
||||
.get("kjuulh", &["some-section", "some-sub-section"])
|
||||
.unwrap();
|
||||
|
||||
let actual = serde_json::to_string_pretty(res).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_get_last_section() {
|
||||
let engine = get_complex_graph();
|
||||
|
||||
let res = engine
|
||||
.get(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section", "sub-sub-section"],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let actual = serde_json::to_string_pretty(res).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_get_item() {
|
||||
let engine = get_complex_graph();
|
||||
|
||||
let res = engine
|
||||
.get(
|
||||
"kjuulh",
|
||||
&[
|
||||
"some-section",
|
||||
"some-sub-section",
|
||||
"sub-sub-section",
|
||||
"some-item",
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let actual = serde_json::to_string_pretty(res).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_move_item() {
|
||||
let mut engine = get_complex_graph();
|
||||
|
||||
engine
|
||||
.section_move(
|
||||
"kjuulh",
|
||||
&[
|
||||
"some-section",
|
||||
"some-sub-section",
|
||||
"sub-sub-section",
|
||||
"some-item",
|
||||
],
|
||||
&["some-section"],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
},
|
||||
"some-sub-section": {
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, engine.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_move_section() {
|
||||
let mut engine = get_complex_graph();
|
||||
|
||||
engine
|
||||
.section_move(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section", "sub-sub-section"],
|
||||
&["some-section"],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section"
|
||||
},
|
||||
"sub-sub-section": {
|
||||
"type": "section",
|
||||
"some-item": {
|
||||
"type": "item",
|
||||
"title": "some-title",
|
||||
"description": "some-description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
assert_eq!(expected, engine.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_delete_section() {
|
||||
let mut engine = get_complex_graph();
|
||||
|
||||
let res = engine.delete("kjuulh", &["some-section", "some-sub-section"]);
|
||||
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
&engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_delete_item() {
|
||||
let mut engine = get_complex_graph();
|
||||
|
||||
let res = engine.delete(
|
||||
"kjuulh",
|
||||
&[
|
||||
"some-section",
|
||||
"some-sub-section",
|
||||
"sub-sub-section",
|
||||
"some-item",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-section": {
|
||||
"type": "section",
|
||||
"some-sub-section": {
|
||||
"type": "section",
|
||||
"sub-sub-section": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
&engine.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
fn get_complex_graph() -> Engine {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
engine.create_root("kjuulh").unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section", "sub-sub-section"],
|
||||
GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&[
|
||||
"some-section",
|
||||
"some-sub-section",
|
||||
"sub-sub-section",
|
||||
"some-item",
|
||||
],
|
||||
GraphItem::Item {
|
||||
title: "some-title".to_string(),
|
||||
description: "some-description".to_string(),
|
||||
state: ItemState::NotDone,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
engine
|
||||
}
|
||||
}
|
126
crates/hyperlog-tui/src/events.rs
Normal file
126
crates/hyperlog-tui/src/events.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use bus::{Bus, BusReader};
|
||||
|
||||
use crate::commander::Command;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub enum Event {
|
||||
CommandEvent { command: Command },
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Events {
|
||||
bus: Arc<Mutex<Bus<Event>>>,
|
||||
}
|
||||
|
||||
impl Default for Events {
|
||||
fn default() -> Self {
|
||||
let bus = Bus::new(10);
|
||||
|
||||
Self {
|
||||
bus: Arc::new(Mutex::new(bus)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Events {
|
||||
pub fn enque_command(&self, cmd: Command) -> anyhow::Result<()> {
|
||||
self.bus
|
||||
.lock()
|
||||
.unwrap()
|
||||
.broadcast(Event::CommandEvent { command: cmd });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> anyhow::Result<BusReader<Event>> {
|
||||
let rx = self.bus.lock().unwrap().add_rx();
|
||||
|
||||
Ok(rx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use similar_asserts::assert_eq;
|
||||
|
||||
use crate::{commander::Command, events::Event};
|
||||
|
||||
use super::Events;
|
||||
|
||||
#[test]
|
||||
fn can_enque_command() -> anyhow::Result<()> {
|
||||
let events = Events::default();
|
||||
|
||||
events.enque_command(Command::CreateRoot {
|
||||
root: "some-root".into(),
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_deque_command() -> anyhow::Result<()> {
|
||||
let events = Events::default();
|
||||
let mut rx = events.subscribe()?;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
events
|
||||
.enque_command(Command::CreateRoot {
|
||||
root: "some-root".into(),
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
let event = rx.recv()?;
|
||||
|
||||
assert_eq!(
|
||||
Event::CommandEvent {
|
||||
command: Command::CreateRoot {
|
||||
root: "some-root".into()
|
||||
},
|
||||
},
|
||||
event
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_broadcast() -> anyhow::Result<()> {
|
||||
let events = Events::default();
|
||||
let mut rx1 = events.subscribe()?;
|
||||
let mut rx2 = events.subscribe()?;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
events
|
||||
.enque_command(Command::CreateRoot {
|
||||
root: "some-root".into(),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
events
|
||||
.enque_command(Command::CreateRoot {
|
||||
root: "another-event".into(),
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
let event = rx1.recv()?;
|
||||
let same_event = rx2.recv()?;
|
||||
|
||||
assert_eq!(event, same_event);
|
||||
|
||||
assert_eq!(
|
||||
Event::CommandEvent {
|
||||
command: Command::CreateRoot {
|
||||
root: "some-root".into()
|
||||
},
|
||||
},
|
||||
event
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
#![feature(map_try_insert)]
|
||||
#![feature(fn_traits)]
|
||||
|
||||
use std::{io::Stdout, time::Duration};
|
||||
@@ -6,8 +7,8 @@ use anyhow::{Context, Result};
|
||||
use app::{render_app, App};
|
||||
use commands::IntoCommand;
|
||||
use components::graph_explorer::GraphExplorer;
|
||||
use core_state::State;
|
||||
use crossterm::event::{self, Event, KeyCode};
|
||||
use hyperlog_core::state::State;
|
||||
use models::{EditMsg, Msg};
|
||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||
|
||||
@@ -19,7 +20,16 @@ pub(crate) mod app;
|
||||
pub(crate) mod command_parser;
|
||||
pub(crate) mod commands;
|
||||
pub(crate) mod components;
|
||||
pub(crate) mod state;
|
||||
|
||||
pub mod commander;
|
||||
pub mod core_state;
|
||||
pub mod shared_engine;
|
||||
pub mod state;
|
||||
|
||||
mod engine;
|
||||
mod events;
|
||||
mod querier;
|
||||
mod storage;
|
||||
|
||||
mod logging;
|
||||
mod terminal;
|
||||
|
39
crates/hyperlog-tui/src/querier.rs
Normal file
39
crates/hyperlog-tui/src/querier.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use hyperlog_core::log::GraphItem;
|
||||
|
||||
use crate::shared_engine::SharedEngine;
|
||||
|
||||
pub struct Querier {
|
||||
engine: SharedEngine,
|
||||
}
|
||||
|
||||
impl Querier {
|
||||
pub fn new(engine: SharedEngine) -> Self {
|
||||
Self { engine }
|
||||
}
|
||||
|
||||
pub fn get_available_roots(&self) -> Option<Vec<String>> {
|
||||
self.engine.get_roots()
|
||||
}
|
||||
|
||||
pub fn get(
|
||||
&self,
|
||||
root: &str,
|
||||
path: impl IntoIterator<Item = impl Into<String>>,
|
||||
) -> Option<GraphItem> {
|
||||
let path = path
|
||||
.into_iter()
|
||||
.map(|i| i.into())
|
||||
.filter(|i| !i.is_empty())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
tracing::debug!(
|
||||
"quering: root:({}), path:({}), len: ({}))",
|
||||
root,
|
||||
path.join("."),
|
||||
path.len()
|
||||
);
|
||||
|
||||
self.engine
|
||||
.get(root, &path.iter().map(|i| i.as_str()).collect::<Vec<_>>())
|
||||
}
|
||||
}
|
72
crates/hyperlog-tui/src/shared_engine.rs
Normal file
72
crates/hyperlog-tui/src/shared_engine.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use hyperlog_core::log::GraphItem;
|
||||
|
||||
use crate::engine::Engine;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SharedEngine {
|
||||
inner: Arc<RwLock<Engine>>,
|
||||
}
|
||||
|
||||
impl From<Engine> for SharedEngine {
|
||||
fn from(value: Engine) -> Self {
|
||||
SharedEngine {
|
||||
inner: Arc::new(RwLock::new(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SharedEngine {
|
||||
pub fn to_str(&self) -> anyhow::Result<String> {
|
||||
self.inner.read().unwrap().to_str()
|
||||
}
|
||||
|
||||
pub fn create_root(&self, root: &str) -> anyhow::Result<()> {
|
||||
self.inner.write().unwrap().create_root(root)
|
||||
}
|
||||
|
||||
pub fn create(&self, root: &str, path: &[&str], item: GraphItem) -> anyhow::Result<()> {
|
||||
self.inner.write().unwrap().create(root, path, item)
|
||||
}
|
||||
|
||||
pub fn get(&self, root: &str, path: &[&str]) -> Option<GraphItem> {
|
||||
self.inner.read().unwrap().get(root, path).cloned()
|
||||
}
|
||||
|
||||
pub fn section_move(
|
||||
&self,
|
||||
root: &str,
|
||||
src_path: &[&str],
|
||||
dest_path: &[&str],
|
||||
) -> anyhow::Result<()> {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.section_move(root, src_path, dest_path)
|
||||
}
|
||||
|
||||
pub fn toggle_item(&self, root: &str, path: &[&str]) -> anyhow::Result<()> {
|
||||
self.inner.write().unwrap().toggle_item(root, path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn update_item(
|
||||
&self,
|
||||
root: &str,
|
||||
path: &[&str],
|
||||
state: GraphItem,
|
||||
) -> anyhow::Result<()> {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.update_item(root, path, &state)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_roots(&self) -> Option<Vec<String>> {
|
||||
self.inner.read().unwrap().get_roots()
|
||||
}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
use hyperlog_core::state::State;
|
||||
use crate::core_state::State;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SharedState {
|
||||
|
261
crates/hyperlog-tui/src/storage.rs
Normal file
261
crates/hyperlog-tui/src/storage.rs
Normal file
@@ -0,0 +1,261 @@
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::{engine::Engine, shared_engine::SharedEngine};
|
||||
|
||||
pub struct LockFile(PathBuf);
|
||||
|
||||
impl Drop for LockFile {
|
||||
fn drop(&mut self) {
|
||||
tracing::debug!("removing lockfile");
|
||||
std::fs::remove_file(&self.0).expect("to be able to delete lockfile")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PathBuf> for LockFile {
|
||||
fn from(value: PathBuf) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Storage {
|
||||
base: PathBuf,
|
||||
lock_file: Arc<Mutex<Option<LockFile>>>,
|
||||
}
|
||||
|
||||
impl Default for Storage {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage {
|
||||
pub fn new() -> Self {
|
||||
let data_dir = dirs::data_local_dir()
|
||||
.ok_or(anyhow::anyhow!("failed to retrieve the users data dir"))
|
||||
.expect("to be able to find config");
|
||||
|
||||
Self {
|
||||
base: data_dir,
|
||||
lock_file: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_base(&mut self, base: &Path) {
|
||||
self.base = base.to_path_buf();
|
||||
}
|
||||
|
||||
pub fn store(&self, engine: &SharedEngine) -> anyhow::Result<()> {
|
||||
let state_path = self.state()?;
|
||||
|
||||
std::fs::write(state_path, engine.to_str()?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(&self) -> anyhow::Result<Engine> {
|
||||
let mut lock = self.lock_file.lock().unwrap();
|
||||
if lock.is_none() {
|
||||
let lock_file = self.state_lock_file()?;
|
||||
*lock = Some(lock_file);
|
||||
}
|
||||
|
||||
let engine = match self.state_file()? {
|
||||
Some(contents) => Engine::engine_from_str(&contents)?,
|
||||
None => Engine::default(),
|
||||
};
|
||||
|
||||
Ok(engine)
|
||||
}
|
||||
|
||||
pub fn unload(self) -> anyhow::Result<()> {
|
||||
drop(self);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear_lock_file(self) {
|
||||
let mut lock_file = self.lock_file.lock().unwrap();
|
||||
|
||||
if lock_file.is_some() {
|
||||
*lock_file = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn state(&self) -> anyhow::Result<PathBuf> {
|
||||
self.cache().map(|c| c.join("graph.json"))
|
||||
}
|
||||
|
||||
fn state_file(&self) -> anyhow::Result<Option<String>> {
|
||||
let state_path = self.state()?;
|
||||
|
||||
if !state_path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let contents = std::fs::read_to_string(&state_path)?;
|
||||
|
||||
Ok(Some(contents))
|
||||
}
|
||||
|
||||
fn state_lock(&self) -> anyhow::Result<PathBuf> {
|
||||
self.cache().map(|c| c.join("graph.lock"))
|
||||
}
|
||||
|
||||
fn create_lock_file(&self) -> anyhow::Result<()> {
|
||||
let lock_path = self.state_lock()?;
|
||||
|
||||
if let Some(parent) = lock_path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
std::fs::write(lock_path, "hyperlog-lock")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn state_lock_file(&self) -> anyhow::Result<LockFile> {
|
||||
let lock_path = self.state_lock()?;
|
||||
|
||||
if !lock_path.exists() {
|
||||
self.create_lock_file()?;
|
||||
return Ok(LockFile::from(lock_path));
|
||||
}
|
||||
|
||||
if let Ok(modified) = lock_path.metadata()?.modified() {
|
||||
if modified.elapsed()? > Duration::from_secs(86400) {
|
||||
std::fs::remove_file(&lock_path)?;
|
||||
|
||||
self.create_lock_file()?;
|
||||
return Ok(LockFile::from(lock_path));
|
||||
}
|
||||
}
|
||||
|
||||
anyhow::bail!("lock file exists and is valid. Aborting");
|
||||
}
|
||||
|
||||
fn cache(&self) -> anyhow::Result<PathBuf> {
|
||||
Ok(self.base.join("hyperlog"))
|
||||
}
|
||||
|
||||
pub fn info(&self) -> anyhow::Result<String> {
|
||||
Ok(format!("storage:\n\tgraph: {}", self.state()?.display()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use hyperlog_core::log::GraphItem;
|
||||
use similar_asserts::assert_eq;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_create_state() -> anyhow::Result<()> {
|
||||
let tempdir = tempfile::tempdir()?;
|
||||
|
||||
let mut storage = Storage::default();
|
||||
storage.with_base(tempdir.path());
|
||||
|
||||
let engine = SharedEngine::from(storage.load()?);
|
||||
engine.create_root("can_create_state")?;
|
||||
|
||||
storage.store(&engine)?;
|
||||
|
||||
let graph = std::fs::read_to_string(tempdir.path().join("hyperlog").join("graph.json"))?;
|
||||
let lock = std::fs::read_to_string(tempdir.path().join("hyperlog").join("graph.lock"))?;
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"can_create_state": {
|
||||
"type": "user"
|
||||
}
|
||||
}"#
|
||||
.to_string(),
|
||||
graph
|
||||
);
|
||||
assert_eq!(r#"hyperlog-lock"#.to_string(), lock);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_already_exists() -> anyhow::Result<()> {
|
||||
let tempdir = tempfile::tempdir()?;
|
||||
|
||||
let mut storage = Storage::default();
|
||||
storage.with_base(tempdir.path());
|
||||
|
||||
let _engine = storage.load()?;
|
||||
|
||||
let mut storage_should_fail = Storage::default();
|
||||
storage_should_fail.with_base(tempdir.path());
|
||||
|
||||
let engine_should_fail = storage_should_fail.load();
|
||||
|
||||
assert!(engine_should_fail.is_err());
|
||||
if let Err(e) = engine_should_fail {
|
||||
assert_eq!(
|
||||
"lock file exists and is valid. Aborting".to_string(),
|
||||
e.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_is_cleaned_up() -> anyhow::Result<()> {
|
||||
let tempdir = tempfile::tempdir()?;
|
||||
|
||||
let mut storage = Storage::default();
|
||||
storage.with_base(tempdir.path());
|
||||
|
||||
let engine = SharedEngine::from(storage.load()?);
|
||||
engine.create_root("can_create_state")?;
|
||||
|
||||
storage.store(&engine)?;
|
||||
storage.unload()?;
|
||||
|
||||
assert!(!tempdir.path().join("hyperlog").join("graph.lock").exists());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_load_state() -> anyhow::Result<()> {
|
||||
let tempdir = tempfile::tempdir()?;
|
||||
|
||||
let mut storage = Storage::default();
|
||||
storage.with_base(tempdir.path());
|
||||
|
||||
let engine = SharedEngine::from(storage.load()?);
|
||||
engine.create_root("can_create_state")?;
|
||||
|
||||
storage.store(&engine)?;
|
||||
|
||||
let graph = std::fs::read_to_string(tempdir.path().join("hyperlog").join("graph.json"))?;
|
||||
|
||||
assert_eq!(
|
||||
r#"{
|
||||
"can_create_state": {
|
||||
"type": "user"
|
||||
}
|
||||
}"#
|
||||
.to_string(),
|
||||
graph
|
||||
);
|
||||
|
||||
let engine = storage.load()?;
|
||||
|
||||
let res = engine.get("can_create_state", &[]);
|
||||
|
||||
assert_eq!(Some(GraphItem::User(BTreeMap::default())), res.cloned());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user