Functional Weave
Code in Python

todo.list@1.1.0

impl/rust/update_todo.rs

1,264 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::todo_item::{Todo, TodoDraft};  ← from todo.item ^1.0.0 · built alongside by fune
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;  ← addTodo, another function of this group · built into the same file, even by a slim install

/// 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<Todo> {
    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])))
}