lune_std_datetime/
lib.rs

1#![allow(clippy::cargo_common_metadata)]
2
3use mlua::prelude::*;
4
5use lune_utils::TableBuilder;
6
7mod date_time;
8mod result;
9mod values;
10
11pub use self::date_time::DateTime;
12
13const TYPEDEFS: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/types.d.luau"));
14
15/**
16    Returns a string containing type definitions for the `datetime` standard library.
17*/
18#[must_use]
19pub fn typedefs() -> String {
20    TYPEDEFS.to_string()
21}
22
23/**
24    Creates the `datetime` standard library module.
25
26    # Errors
27
28    Errors when out of memory.
29*/
30pub fn module(lua: Lua) -> LuaResult<LuaTable> {
31    TableBuilder::new(lua)?
32        .with_function("fromIsoDate", |_, date: String| {
33            Ok(DateTime::from_rfc_3339(date)?) // FUTURE: Remove this rfc3339 alias method
34        })?
35        .with_function("fromRfc3339", |_, date: String| {
36            Ok(DateTime::from_rfc_3339(date)?)
37        })?
38        .with_function("fromRfc2822", |_, date: String| {
39            Ok(DateTime::from_rfc_2822(date)?)
40        })?
41        .with_function("fromLocalTime", |_, values| {
42            Ok(DateTime::from_local_time(&values)?)
43        })?
44        .with_function("fromUniversalTime", |_, values| {
45            Ok(DateTime::from_universal_time(&values)?)
46        })?
47        .with_function("fromUnixTimestamp", |_, timestamp| {
48            Ok(DateTime::from_unix_timestamp_float(timestamp)?)
49        })?
50        .with_function("now", |_, ()| Ok(DateTime::now()))?
51        .build_readonly()
52}