Functional Weave
Code in Rust

todo.list@1.0.0

impl/rust/toggle_all.rs

1,095 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;  ← from todo.item ^1.0.0 · built alongside by fune
use super::todo_item_validate_todo::{todos_from_value, todos_to_value};
use super::todo_list_add_todo::require_utc_now;  ← addTodo, another function of this group · built into the same file, even by a slim install
use super::todo_list_toggle_todo::{complete_todo, require_today};  ← toggleTodo, another function of this group · built into the same file, even by a slim install

/// Complete every open todo as toggle_todo would (repeating ones roll forward
/// and stay open), or reopen all once none is open.
///
/// # Panics
/// On a `now` that is not a UTC timestamp or a `today` that is not a date.
pub fn toggle_all(todos: &[Todo], now: &str, today: &str) -> Vec<Todo> {
    require_utc_now(now);
    require_today(today);
    if todos.iter().any(|t| !t.done) {
        return todos.iter().map(|t| if t.done { t.clone() } else { complete_todo(t, now, today) }).collect();
    }
    todos
        .iter()
        .map(|t| {
            let mut open = t.clone();
            open.done = false;
            open.completed_at = None;
            open
        })
        .collect()
}

pub fn fune_vector(args: &[Value]) -> Value {
    todos_to_value(&toggle_all(&todos_from_value(&args[0]), args[1].as_str(), args[2].as_str()))
}