25
crates/hyperlog-core/Cargo.toml
Normal file
25
crates/hyperlog-core/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "hyperlog-core"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
axum.workspace = true
|
||||
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
sqlx = { version = "0.7.3", features = ["runtime-tokio", "tls-rustls", "postgres", "uuid", "time"] }
|
||||
uuid = { version = "1.7.0", features = ["v4"] }
|
||||
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
|
||||
serde_json = "1.0.116"
|
||||
bus = "2.4.1"
|
||||
dirs = "5.0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
similar-asserts = "1.5.0"
|
||||
tempfile = "3.10.1"
|
92
crates/hyperlog-core/src/commander.rs
Normal file
92
crates/hyperlog-core/src/commander.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
events::Events,
|
||||
log::{GraphItem, ItemState},
|
||||
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,
|
||||
},
|
||||
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<_>>(),
|
||||
)?,
|
||||
}
|
||||
|
||||
self.storage.store(&self.engine)?;
|
||||
|
||||
self.events.enque_command(cmd)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
686
crates/hyperlog-core/src/engine.rs
Normal file
686
crates/hyperlog-core/src/engine.rs
Normal file
@@ -0,0 +1,686 @@
|
||||
use std::{collections::BTreeMap, fmt::Display};
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
|
||||
use crate::log::{Graph, GraphItem};
|
||||
|
||||
#[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.as_mut();
|
||||
}
|
||||
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(), Box::new(item));
|
||||
}
|
||||
GraphItem::Section(s) => {
|
||||
s.insert(last.to_string(), Box::new(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(), Box::new(src))
|
||||
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
|
||||
}
|
||||
GraphItem::Section(s) => {
|
||||
s.try_insert(src_item.to_string(), Box::new(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"))
|
||||
}
|
||||
}
|
||||
|
||||
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 similar_asserts::assert_eq;
|
||||
|
||||
use crate::log::{GraphItem, ItemState};
|
||||
|
||||
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"],
|
||||
crate::log::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"],
|
||||
crate::log::GraphItem::Section(BTreeMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
engine
|
||||
.create(
|
||||
"kjuulh",
|
||||
&["some-section", "some-sub-section"],
|
||||
crate::log::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-core/src/events.rs
Normal file
126
crates/hyperlog-core/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(())
|
||||
}
|
||||
}
|
10
crates/hyperlog-core/src/lib.rs
Normal file
10
crates/hyperlog-core/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
#![feature(map_try_insert)]
|
||||
pub mod commander;
|
||||
pub mod querier;
|
||||
|
||||
pub mod engine;
|
||||
pub mod events;
|
||||
pub mod log;
|
||||
pub mod shared_engine;
|
||||
pub mod state;
|
||||
pub mod storage;
|
234
crates/hyperlog-core/src/log.rs
Normal file
234
crates/hyperlog-core/src/log.rs
Normal file
@@ -0,0 +1,234 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone, Debug)]
|
||||
pub enum ItemState {
|
||||
#[serde(rename = "not-done")]
|
||||
NotDone,
|
||||
#[serde(rename = "done")]
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum GraphItem {
|
||||
#[serde(rename = "user")]
|
||||
User(BTreeMap<String, Box<GraphItem>>),
|
||||
#[serde(rename = "section")]
|
||||
Section(BTreeMap<String, Box<GraphItem>>),
|
||||
#[serde(rename = "item")]
|
||||
Item {
|
||||
title: String,
|
||||
description: String,
|
||||
state: ItemState,
|
||||
},
|
||||
}
|
||||
|
||||
impl GraphItem {
|
||||
pub fn get(&self, path: &[&str]) -> Option<&GraphItem> {
|
||||
match path.split_first() {
|
||||
Some((first, rest)) => match self {
|
||||
GraphItem::User(u) => u.get(*first)?.get(rest),
|
||||
GraphItem::Section(s) => s.get(*first)?.get(rest),
|
||||
GraphItem::Item { .. } => None,
|
||||
},
|
||||
None => Some(self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, path: &[&str]) -> Option<&mut GraphItem> {
|
||||
match path.split_first() {
|
||||
Some((first, rest)) => match self {
|
||||
GraphItem::User(u) => u.get_mut(*first)?.get_mut(rest),
|
||||
GraphItem::Section(s) => s.get_mut(*first)?.get_mut(rest),
|
||||
GraphItem::Item { .. } => None,
|
||||
},
|
||||
None => Some(self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take(&mut self, path: &[&str]) -> Option<GraphItem> {
|
||||
match path.split_first() {
|
||||
Some((first, rest)) => match self {
|
||||
GraphItem::User(u) => {
|
||||
if rest.is_empty() {
|
||||
let val = u.remove(*first);
|
||||
val.map(|v| *v)
|
||||
} else {
|
||||
u.get_mut(*first)?.take(rest)
|
||||
}
|
||||
}
|
||||
GraphItem::Section(s) => {
|
||||
if rest.is_empty() {
|
||||
let val = s.remove(*first);
|
||||
val.map(|v| *v)
|
||||
} else {
|
||||
s.get_mut(*first)?.take(rest)
|
||||
}
|
||||
}
|
||||
GraphItem::Item { .. } => None,
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, PartialEq, Eq, Default)]
|
||||
pub struct Graph(BTreeMap<String, GraphItem>);
|
||||
|
||||
impl Deref for Graph {
|
||||
type Target = BTreeMap<String, GraphItem>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Graph {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::collections::{hash_map, BTreeMap, HashMap};
|
||||
|
||||
use crate::log::{GraphItem, ItemState};
|
||||
|
||||
use super::Graph;
|
||||
|
||||
#[test]
|
||||
fn some_test() {
|
||||
let test_graph = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user"
|
||||
}
|
||||
}"#;
|
||||
|
||||
let graph: Graph = serde_json::from_str(test_graph).unwrap();
|
||||
|
||||
let mut expected = Graph::default();
|
||||
|
||||
let user = BTreeMap::new();
|
||||
|
||||
expected.insert("kjuulh".into(), GraphItem::User(user));
|
||||
similar_asserts::assert_eq!(expected, graph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn some_user_test() {
|
||||
let test_graph = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-project": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
let graph: Graph = serde_json::from_str(test_graph).unwrap();
|
||||
|
||||
let mut expected = Graph::default();
|
||||
let mut user = BTreeMap::new();
|
||||
user.insert(
|
||||
"some-project".into(),
|
||||
Box::new(GraphItem::Section(BTreeMap::default())),
|
||||
);
|
||||
|
||||
expected.insert("kjuulh".into(), GraphItem::User(user));
|
||||
|
||||
similar_asserts::assert_eq!(expected, graph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn some_section_test() {
|
||||
let test_graph = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-project": {
|
||||
"type": "section",
|
||||
"some-nested-project": {
|
||||
"type": "section"
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
let graph: Graph = serde_json::from_str(test_graph).unwrap();
|
||||
|
||||
let mut expected = Graph::default();
|
||||
|
||||
let mut some_project = BTreeMap::default();
|
||||
some_project.insert(
|
||||
"some-nested-project".into(),
|
||||
Box::new(GraphItem::Section(BTreeMap::default())),
|
||||
);
|
||||
let mut user = BTreeMap::new();
|
||||
user.insert(
|
||||
"some-project".into(),
|
||||
Box::new(GraphItem::Section(some_project)),
|
||||
);
|
||||
|
||||
expected.insert("kjuulh".into(), GraphItem::User(user));
|
||||
|
||||
similar_asserts::assert_eq!(expected, graph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn some_item_test() {
|
||||
let test_graph = r#"{
|
||||
"kjuulh": {
|
||||
"type": "user",
|
||||
"some-project": {
|
||||
"type": "section",
|
||||
"some-nested-project": {
|
||||
"type": "section",
|
||||
"some-todo": {
|
||||
"type": "item",
|
||||
"title": "some title",
|
||||
"description": "some description",
|
||||
"state": "not-done"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
let graph: Graph = serde_json::from_str(test_graph).unwrap();
|
||||
|
||||
let mut expected = Graph::default();
|
||||
|
||||
let mut nested_project = BTreeMap::default();
|
||||
nested_project.insert(
|
||||
"some-todo".into(),
|
||||
Box::new(GraphItem::Item {
|
||||
title: "some title".into(),
|
||||
description: "some description".into(),
|
||||
state: ItemState::NotDone,
|
||||
}),
|
||||
);
|
||||
|
||||
let mut some_project = BTreeMap::default();
|
||||
some_project.insert(
|
||||
"some-nested-project".into(),
|
||||
Box::new(GraphItem::Section(nested_project)),
|
||||
);
|
||||
let mut user = BTreeMap::new();
|
||||
user.insert(
|
||||
"some-project".into(),
|
||||
Box::new(GraphItem::Section(some_project)),
|
||||
);
|
||||
|
||||
expected.insert("kjuulh".into(), GraphItem::User(user));
|
||||
|
||||
similar_asserts::assert_eq!(
|
||||
serde_json::to_string_pretty(&expected).unwrap(),
|
||||
serde_json::to_string_pretty(&graph).unwrap()
|
||||
);
|
||||
}
|
||||
}
|
24
crates/hyperlog-core/src/querier.rs
Normal file
24
crates/hyperlog-core/src/querier.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::{log::GraphItem, shared_engine::SharedEngine};
|
||||
|
||||
pub struct Querier {
|
||||
engine: SharedEngine,
|
||||
}
|
||||
|
||||
impl Querier {
|
||||
pub fn new(engine: SharedEngine) -> Self {
|
||||
Self { engine }
|
||||
}
|
||||
|
||||
pub fn get(
|
||||
&self,
|
||||
root: &str,
|
||||
path: impl IntoIterator<Item = impl Into<String>>,
|
||||
) -> Option<GraphItem> {
|
||||
let path = path.into_iter().map(|i| i.into()).collect::<Vec<String>>();
|
||||
|
||||
tracing::debug!("quering: {}, len: ({}))", path.join("."), path.len());
|
||||
|
||||
self.engine
|
||||
.get(root, &path.iter().map(|i| i.as_str()).collect::<Vec<_>>())
|
||||
}
|
||||
}
|
46
crates/hyperlog-core/src/shared_engine.rs
Normal file
46
crates/hyperlog-core/src/shared_engine.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::{engine::Engine, log::GraphItem};
|
||||
|
||||
#[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)
|
||||
}
|
||||
}
|
31
crates/hyperlog-core/src/state.rs
Normal file
31
crates/hyperlog-core/src/state.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use crate::{
|
||||
commander::Commander, events::Events, querier::Querier, shared_engine::SharedEngine,
|
||||
storage::Storage,
|
||||
};
|
||||
|
||||
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),
|
||||
})
|
||||
}
|
||||
}
|
253
crates/hyperlog-core/src/storage.rs
Normal file
253
crates/hyperlog-core/src/storage.rs
Normal file
@@ -0,0 +1,253 @@
|
||||
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) {
|
||||
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(())
|
||||
}
|
||||
|
||||
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 similar_asserts::assert_eq;
|
||||
|
||||
use crate::log::GraphItem;
|
||||
|
||||
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