1use chrono::{DateTime, Utc};
4use scripting::runtime::{alloc_string_ref, read_string_arg};
5use server::timezone::display;
6use uuid::Uuid;
7use wasmtime::{ArrayRef, Linker, Rooted};
8
9use crate::forms::user_zone;
10use crate::natives::args::required;
11use crate::natives::forms::{args_error, native_failure};
12use crate::natives::native::register_native;
13use crate::session::SessionData;
14
15pub const REGISTERED_COMMANDS: &[&str] = &["format-date"];
16
17pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
18 register_native(
19 linker,
20 "date_format_date",
21 |caller, (instant_arg,): (Option<Rooted<ArrayRef>>,)| {
22 Box::pin(async move {
23 let user_id = caller.data().ctx().user_id;
24 let instant = read_string_arg(caller, instant_arg)?;
25 let printed = run_format_date(user_id, instant).await?;
26 Ok(Some(alloc_string_ref(caller, printed.as_bytes())?))
27 })
28 },
29 )
30}
31
32async fn run_format_date(user_id: Uuid, instant_arg: Option<String>) -> wasmtime::Result<String> {
33 let text = required("format-date", "instant", instant_arg)?;
34 let at = DateTime::parse_from_rfc3339(&text)
35 .map_err(|err| {
36 args_error(format!(
37 "format-date: '{text}' is not an RFC 3339 instant: {err}"
38 ))
39 })?
40 .with_timezone(&Utc);
41 let zone = user_zone(user_id).await.map_err(native_failure)?;
42 Ok(display(at, zone))
43}