@@ -25,6 +25,10 @@ pub enum Command {
|
||||
description: String,
|
||||
state: ItemState,
|
||||
},
|
||||
ToggleItem {
|
||||
root: String,
|
||||
path: Vec<String>,
|
||||
},
|
||||
Move {
|
||||
root: String,
|
||||
src: Vec<String>,
|
||||
@@ -81,6 +85,9 @@ impl Commander {
|
||||
&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<_>>())?,
|
||||
}
|
||||
|
||||
self.storage.store(&self.engine)?;
|
||||
|
@@ -2,7 +2,7 @@ use std::{collections::BTreeMap, fmt::Display};
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
|
||||
use crate::log::{Graph, GraphItem};
|
||||
use crate::log::{Graph, GraphItem, ItemState};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Engine {
|
||||
@@ -129,6 +129,22 @@ impl Engine {
|
||||
.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(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Engine {
|
||||
|
@@ -43,4 +43,10 @@ impl SharedEngine {
|
||||
.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(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user