chore: fix test breaking changes
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-10 12:03:38 +02:00
parent f284171f5a
commit 86310c6764
13 changed files with 763 additions and 270 deletions

View File

@@ -25,6 +25,13 @@ pub enum Command {
description: String,
state: ItemState,
},
UpdateItem {
root: String,
path: Vec<String>,
title: String,
description: String,
state: ItemState,
},
ToggleItem {
root: String,
path: Vec<String>,
@@ -88,6 +95,21 @@ impl Commander {
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)?;

View File

@@ -44,7 +44,7 @@ impl Engine {
match current_item {
GraphItem::User(u) => match u.get_mut(section.to_owned()) {
Some(graph_item) => {
current_item = graph_item.as_mut();
current_item = graph_item;
}
None => anyhow::bail!("path: {} section was not found", section),
},
@@ -60,10 +60,10 @@ impl Engine {
match current_item {
GraphItem::User(u) => {
u.insert(last.to_string(), Box::new(item));
u.insert(last.to_string(), item);
}
GraphItem::Section(s) => {
s.insert(last.to_string(), Box::new(item));
s.insert(last.to_string(), item);
}
GraphItem::Item { .. } => anyhow::bail!("cannot insert an item into an item"),
}
@@ -109,11 +109,11 @@ impl Engine {
match dest {
GraphItem::User(u) => {
u.try_insert(src_item.to_string(), Box::new(src))
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(), Box::new(src))
s.try_insert(src_item.to_string(), src)
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
}
GraphItem::Item { .. } => {
@@ -145,6 +145,57 @@ impl Engine {
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(())
}
}
impl Display for Engine {

View File

@@ -13,13 +13,19 @@ pub enum ItemState {
Done,
}
impl Default for ItemState {
fn default() -> Self {
Self::NotDone
}
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone, Debug)]
#[serde(tag = "type")]
pub enum GraphItem {
#[serde(rename = "user")]
User(BTreeMap<String, Box<GraphItem>>),
User(BTreeMap<String, GraphItem>),
#[serde(rename = "section")]
Section(BTreeMap<String, Box<GraphItem>>),
Section(BTreeMap<String, GraphItem>),
#[serde(rename = "item")]
Item {
title: String,
@@ -58,8 +64,7 @@ impl GraphItem {
Some((first, rest)) => match self {
GraphItem::User(section) | GraphItem::Section(section) => {
if rest.is_empty() {
let val = section.remove(*first);
val.map(|v| *v)
section.remove(*first)
} else {
section.get_mut(*first)?.take(rest)
}
@@ -131,7 +136,7 @@ mod test {
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(BTreeMap::default())),
GraphItem::Section(BTreeMap::default()),
);
expected.insert("kjuulh".into(), GraphItem::User(user));
@@ -160,13 +165,10 @@ mod test {
let mut some_project = BTreeMap::default();
some_project.insert(
"some-nested-project".into(),
Box::new(GraphItem::Section(BTreeMap::default())),
GraphItem::Section(BTreeMap::default()),
);
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(some_project)),
);
user.insert("some-project".into(), GraphItem::Section(some_project));
expected.insert("kjuulh".into(), GraphItem::User(user));
@@ -200,23 +202,20 @@ mod test {
let mut nested_project = BTreeMap::default();
nested_project.insert(
"some-todo".into(),
Box::new(GraphItem::Item {
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)),
GraphItem::Section(nested_project),
);
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(some_project)),
);
user.insert("some-project".into(), GraphItem::Section(some_project));
expected.insert("kjuulh".into(), GraphItem::User(user));

View File

@@ -49,4 +49,18 @@ impl SharedEngine {
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(())
}
}