Lines
100 %
Functions
58.33 %
Branches
//! `format-date`: an RFC 3339 instant printed in the user's zone.
use chrono::{DateTime, Utc};
use scripting::runtime::{alloc_string_ref, read_string_arg};
use server::timezone::display;
use uuid::Uuid;
use wasmtime::{ArrayRef, Linker, Rooted};
use crate::forms::user_zone;
use crate::natives::args::required;
use crate::natives::forms::{args_error, native_failure};
use crate::natives::native::register_native;
use crate::session::SessionData;
pub const REGISTERED_COMMANDS: &[&str] = &["format-date"];
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
register_native(
linker,
"date_format_date",
|caller, (instant_arg,): (Option<Rooted<ArrayRef>>,)| {
Box::pin(async move {
let user_id = caller.data().ctx().user_id;
let instant = read_string_arg(caller, instant_arg)?;
let printed = run_format_date(user_id, instant).await?;
Ok(Some(alloc_string_ref(caller, printed.as_bytes())?))
})
},
)
}
async fn run_format_date(user_id: Uuid, instant_arg: Option<String>) -> wasmtime::Result<String> {
let text = required("format-date", "instant", instant_arg)?;
let at = DateTime::parse_from_rfc3339(&text)
.map_err(|err| {
args_error(format!(
"format-date: '{text}' is not an RFC 3339 instant: {err}"
))
})?
.with_timezone(&Utc);
let zone = user_zone(user_id).await.map_err(native_failure)?;
Ok(display(at, zone))