Functional Weave
Code in TypeScript

todo.due-status@1.0.0

impl/rust.rs

1,148 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::dates_day_of_week::day_of_week;  ← from dates.day-of-week ^1.0.0 · built alongside by fune
use super::dates_days_between::days_between;  ← from dates.days-between ^1.0.0 · built alongside by fune
use super::todo_item::{todo_from_value, Todo};  ← from todo.item ^1.0.0 · built alongside by fune

/// One word for where a todo stands today; done wins, this-week is the rest
/// of the ISO week after tomorrow.
///
/// # Panics
/// Panics on an impossible or malformed date.
pub fn due_status(todo: &Todo, today: &str) -> String {
    match &todo.due {
        Some(due) => {
            days_between(today, due);
        }
        None => {
            day_of_week(today);
        }
    }
    if todo.done {
        return "done".to_string();
    }
    let due = match &todo.due {
        None => return "none".to_string(),
        Some(d) => d,
    };
    let days = days_between(today, due);
    let status = if days < 0 {
        "overdue"
    } else if days == 0 {
        "today"
    } else if days == 1 {
        "tomorrow"
    } else if days <= 7 - day_of_week(today) {
        "this-week"
    } else {
        "later"
    };
    status.to_string()
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::str(&due_status(&todo_from_value(&args[0]), args[1].as_str()))
}