use super::funejson::Value; use super::todo_item::{Todo, TodoDraft}; use super::todo_item_validate_draft::todo_draft_from_value; use super::todo_item_validate_todo::{todos_from_value, todos_to_value}; use super::todo_list_add_todo::checked_draft; /// The position of the todo with this id, or the unknown-id panic every list /// operation shares. pub fn index_of_todo(todos: &[Todo], id: &str) -> usize { match todos.iter().position(|t| t.id == id) { Some(at) => at, None => panic!("no todo with id \"{}\"", id), } } /// Save the edit form; id, done state, timestamps, order and place are kept. /// /// # Panics /// On an unknown id or an invalid draft. pub fn update_todo(todos: &[Todo], id: &str, draft: &TodoDraft) -> Vec { let at = index_of_todo(todos, id); let clean = checked_draft(draft); let mut out = todos.to_vec(); let t = &mut out[at]; t.title = clean.title; t.notes = clean.notes; t.priority = clean.priority; t.due = clean.due; t.tags = clean.tags; t.recurrence = clean.recurrence; out } pub fn fune_vector(args: &[Value]) -> Value { let todos = todos_from_value(&args[0]); todos_to_value(&update_todo(&todos, args[1].as_str(), &todo_draft_from_value(&args[2]))) }