Skip to content

Commit

Permalink
Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matta committed Jun 21, 2024
1 parent 7ffe7d6 commit 2193e93
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 103 deletions.
42 changes: 18 additions & 24 deletions src/persist/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) mod memory {
.map_or_else(|| bail!("task not found"), |task| Ok(task.clone()))
}

fn put_task(&mut self, task: &Task) -> anyhow::Result<()> {
fn put_task(&mut self, task: &Task) {
use im::hashmap::Entry::{Occupied, Vacant};
debug_assert!(
self.order.contains(&task.id()),
Expand All @@ -77,11 +77,9 @@ pub(crate) mod memory {
panic!("MemoryStore::put called with task not in the tasks map")
}
}

Ok(())
}

fn insert_task(&mut self, previous: Option<&TaskId>, task: &Task) -> anyhow::Result<()> {
fn insert_task(&mut self, previous: Option<&TaskId>, task: &Task) {
let index = if let Some(previous) = previous {
self.order
.index_of(previous)
Expand All @@ -96,17 +94,14 @@ pub(crate) mod memory {
);
self.order.insert(index, task.id());
self.tasks.entry(task.id()).or_insert(task.clone());

Ok(())
}

fn delete_task(&mut self, id: &TaskId) -> anyhow::Result<()> {
fn delete_task(&mut self, id: &TaskId) {
self.order.retain(|entry| entry != id);
self.tasks.retain(|key, _| key != id);
Ok(())
}

fn move_task(&mut self, previous: Option<&TaskId>, id: &TaskId) -> anyhow::Result<()> {
fn move_task(&mut self, previous: Option<&TaskId>, id: &TaskId) {
self.order.retain(|other| other != id);

let index = if let Some(previous_id) = previous {
Expand All @@ -117,21 +112,18 @@ pub(crate) mod memory {
.unwrap_or(0);

self.order.insert(index, *id);

Ok(())
}

fn list_tasks(&mut self) -> anyhow::Result<Vec<Task>> {
Ok(self
.order
fn list_tasks(&mut self) -> Vec<Task> {
self.order
.iter()
.map(|id| {
self.tasks
.get(id)
.expect("all items in MemoryStore::order must be in MemoryStore::tasks")
.clone()
})
.collect())
.collect()
}
}

Expand All @@ -150,13 +142,14 @@ pub(crate) mod memory {
impl<'a> MemoryTransaction<'a> {
fn new(store: &'a mut MemoryStore) -> Self {
let start = store.current.clone();
Self { start, store }
Self { store, start }
}
}

impl Transaction for MemoryTransaction<'_> {
fn delete_task(&mut self, id: &TaskId) -> anyhow::Result<()> {
self.store.delete_task(id)
self.store.delete_task(id);
Ok(())
}

fn commit(self: Box<Self>) -> anyhow::Result<()> {
Expand Down Expand Up @@ -202,8 +195,8 @@ pub(crate) mod memory {
Ok(())
}

fn delete_task(&mut self, id: &TaskId) -> Result<(), anyhow::Error> {
self.current.delete_task(id)
fn delete_task(&mut self, id: &TaskId) {
self.current.delete_task(id);
}
}

Expand All @@ -214,27 +207,28 @@ pub(crate) mod memory {

fn put_task(&mut self, task: &Task) -> anyhow::Result<()> {
let saved = self.current.clone();
self.current.put_task(task)?;
self.current.put_task(task);
self.undo_stack.push(saved);
Ok(())
}

fn insert_task(&mut self, previous: Option<&TaskId>, task: &Task) -> anyhow::Result<()> {
let saved = self.current.clone();
self.current.insert_task(previous, task)?;
self.current.insert_task(previous, task);
self.undo_stack.push(saved);
Ok(())
}

fn move_task(&mut self, previous: Option<&TaskId>, task: &TaskId) -> anyhow::Result<()> {
let saved = self.current.clone();
self.current.move_task(previous, task)?;
self.current.move_task(previous, task);
self.undo_stack.push(saved);
Ok(())
}

fn list_tasks(&mut self) -> anyhow::Result<Vec<Task>> {
self.current.list_tasks()
let tasks = self.current.list_tasks();
Ok(tasks)
}

fn undo(&mut self) -> anyhow::Result<()> {
Expand All @@ -256,7 +250,7 @@ pub(crate) mod memory {
}
}

fn transaction(&mut self) -> Box<dyn Transaction + '_> {
fn transaction<'a>(&'a mut self) -> Box<dyn Transaction + 'a> {
let transaction = MemoryTransaction::new(self);
Box::new(transaction)
}
Expand Down
2 changes: 1 addition & 1 deletion src/screen/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl screen::Screen for State {
}
}

fn render(self: &Self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
let prompt = TextPrompt::new(Cow::Borrowed("edit"));
frame.render_stateful_widget(prompt, frame.size(), &mut self.text.borrow_mut());
let (x, y) = self.text.borrow().cursor();
Expand Down
151 changes: 75 additions & 76 deletions src/screen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,101 +22,100 @@ impl State {
pub(crate) fn new() -> Self {
Self::default()
}
}

fn add(&mut self, common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
// FIXME: make generating new tasks less cumbersome
// FIXME: handle error
let task = Task::new(Task::new_id(), String::new(), None, None, None);
common_state
.store
.insert_task(common_state.selected.as_ref(), &task)
.expect("FIXME: handle error");
common_state.selected = Some(task.id());
self.edit(common_state)
}
fn add(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
// FIXME: make generating new tasks less cumbersome
// FIXME: handle error
let task = Task::new(Task::new_id(), String::new(), None, None, None);
common_state
.store
.insert_task(common_state.selected.as_ref(), &task)
.expect("FIXME: handle error");
common_state.selected = Some(task.id());
edit(common_state)
}

fn edit(&mut self, common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
if let Some((id, text)) = {
if let Some(id) = common_state.selected {
let title = common_state.store.get_task(&id).unwrap().title().into();
let text = tui_prompts::TextState::new()
.with_value(Cow::Owned(title))
.with_focus(tui_prompts::FocusState::Focused);
let text = RefCell::new(text);
Some((id, text))
} else {
None
}
} {
let edit = screen::edit::State::new(id, text);
return Some(Box::new(edit));
fn edit(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
if let Some((id, text)) = {
if let Some(id) = common_state.selected {
let title = common_state.store.get_task(&id).unwrap().title().into();
let text = tui_prompts::TextState::new()
.with_value(Cow::Owned(title))
.with_focus(tui_prompts::FocusState::Focused);
let text = RefCell::new(text);
Some((id, text))
} else {
None
}
None
} {
let edit = screen::edit::State::new(id, text);
return Some(Box::new(edit));
}
None
}

fn do_handle_key_event(
&mut self,
common_state: &mut CommonState,
key_combination: crokey::KeyCombination,
) -> Option<Box<dyn Screen>> {
let bindings = crate::keys::default_bindings();
match bindings.get(&key_combination) {
None => {}
Some(action) => match action {
keys::Command::Quit => {
return Some(Box::new(screen::quit::State {}));
}
keys::Command::Toggle => {
common_state.toggle();
}
keys::Command::Edit => {
return self.edit(common_state);
}
keys::Command::Snooze => {
common_state.snooze();
}
keys::Command::Next => {
common_state.next();
}
keys::Command::Previous => {
common_state.previous();
}
keys::Command::MoveUp => {
common_state.move_up();
}
keys::Command::MoveDown => {
common_state.move_down();
}
keys::Command::Add => {
return self.add(common_state);
}
keys::Command::Delete => {
common_state.delete_selected();
}
keys::Command::Undo => {
common_state.undo();
}
keys::Command::Redo => common_state.redo(),
},
}
None
fn do_handle_key_event(
common_state: &mut CommonState,
key_combination: crokey::KeyCombination,
) -> Option<Box<dyn Screen>> {
let bindings = crate::keys::default_bindings();
match bindings.get(&key_combination) {
None => {}
Some(action) => match action {
keys::Command::Quit => {
return Some(Box::new(screen::quit::State {}));
}
keys::Command::Toggle => {
common_state.toggle();
}
keys::Command::Edit => {
return edit(common_state);
}
keys::Command::Snooze => {
common_state.snooze();
}
keys::Command::Next => {
common_state.next();
}
keys::Command::Previous => {
common_state.previous();
}
keys::Command::MoveUp => {
common_state.move_up();
}
keys::Command::MoveDown => {
common_state.move_down();
}
keys::Command::Add => {
return add(common_state);
}
keys::Command::Delete => {
common_state.delete_selected();
}
keys::Command::Undo => {
common_state.undo();
}
keys::Command::Redo => common_state.redo(),
},
}
None
}

impl screen::Screen for State {
fn handle_key_event(
mut self: Box<Self>,
self: Box<Self>,
common_state: &mut CommonState,
key_combination: crokey::KeyCombination,
) -> Box<dyn Screen> {
if let Some(screen) = self.do_handle_key_event(common_state, key_combination) {
if let Some(screen) = do_handle_key_event(common_state, key_combination) {
screen
} else {
self
}
}

fn render(self: &Self, common_state: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, common_state: &mut CommonState, frame: &mut ratatui::Frame) {
// Set the list widet's selected state based on the list state.
let state: &mut ListState = &mut self.list.borrow_mut();
state.select(common_state.index_of_id(common_state.selected));
Expand Down
2 changes: 1 addition & 1 deletion src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Screen {
key_combination: crokey::KeyCombination,
) -> Box<dyn Screen>;

fn render(self: &Self, conext: &mut CommonState, frame: &mut ratatui::Frame);
fn render(&self, conext: &mut CommonState, frame: &mut ratatui::Frame);

// FIXME: replace this with a back channel to the event queue logic?
// ...at which point should handle_key_event return a Box<dyn Screen>
Expand Down
2 changes: 1 addition & 1 deletion src/screen/quit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Screen for State {
self
}

fn render(self: &Self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
frame.render_widget(Line::from("quitting..."), frame.size());
}

Expand Down

0 comments on commit 2193e93

Please sign in to comment.