From 474ceb139ac2f1fb9e6e4e6c547944d5fdb2399f Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 14 Jan 2024 17:06:41 +0100 Subject: [PATCH] Change properties on fs.metadata to be DateTime values instead of numbers --- CHANGELOG.md | 2 ++ src/lune/builtins/fs/metadata.rs | 15 ++++++++------- tests/fs/metadata.luau | 8 +++++++- types/fs.luau | 9 ++++++--- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc129e..a1affac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 This unfortunately hurts ergonomics for quickly running scripts but is a necessary change to allow us to add more commands, such as the new `build` subcommand. +- The `createdAt`, `modifiedAt`, and `accessedAt` properties returned from `fs.metadata` are now `DateTime` values instead of numbers. + - The `Lune` struct has been renamed to `Runtime` in the Lune rust crate. ### Added diff --git a/src/lune/builtins/fs/metadata.rs b/src/lune/builtins/fs/metadata.rs index a5e1ddf..93bdbe6 100644 --- a/src/lune/builtins/fs/metadata.rs +++ b/src/lune/builtins/fs/metadata.rs @@ -8,6 +8,8 @@ use std::{ use mlua::prelude::*; +use crate::lune::builtins::datetime::DateTime; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FsMetadataKind { None, @@ -95,9 +97,9 @@ impl<'lua> IntoLua<'lua> for FsPermissions { pub struct FsMetadata { pub(crate) kind: FsMetadataKind, pub(crate) exists: bool, - pub(crate) created_at: Option, - pub(crate) modified_at: Option, - pub(crate) accessed_at: Option, + pub(crate) created_at: Option, + pub(crate) modified_at: Option, + pub(crate) accessed_at: Option, pub(crate) permissions: Option, } @@ -116,7 +118,7 @@ impl FsMetadata { impl<'lua> IntoLua<'lua> for FsMetadata { fn into_lua(self, lua: &'lua Lua) -> LuaResult> { - let tab = lua.create_table_with_capacity(0, 5)?; + let tab = lua.create_table_with_capacity(0, 6)?; tab.set("kind", self.kind)?; tab.set("exists", self.exists)?; tab.set("createdAt", self.created_at)?; @@ -133,7 +135,6 @@ impl From for FsMetadata { Self { kind: value.file_type().into(), exists: true, - // FUTURE: Turn these into DateTime structs instead when that's implemented created_at: system_time_to_timestamp(value.created()), modified_at: system_time_to_timestamp(value.modified()), accessed_at: system_time_to_timestamp(value.accessed()), @@ -142,10 +143,10 @@ impl From for FsMetadata { } } -fn system_time_to_timestamp(res: IoResult) -> Option { +fn system_time_to_timestamp(res: IoResult) -> Option { match res { Ok(t) => match t.duration_since(SystemTime::UNIX_EPOCH) { - Ok(d) => Some(d.as_secs_f64()), + Ok(d) => DateTime::from_unix_timestamp_float(d.as_secs_f64()).ok(), Err(_) => None, }, Err(_) => None, diff --git a/tests/fs/metadata.luau b/tests/fs/metadata.luau index b96b93d..e926a2c 100644 --- a/tests/fs/metadata.luau +++ b/tests/fs/metadata.luau @@ -1,3 +1,5 @@ +--!nolint UnknownType + local TEMP_DIR_PATH = "bin/" local TEMP_FILE_PATH = TEMP_DIR_PATH .. "metadata_test" @@ -37,7 +39,8 @@ assert(metaFile.kind == "file", "File metadata kind was invalid") 2. Wait for a bit so that timestamps can change 3. Write the file, with an extra newline 4. Metadata changed timestamp should be different - 5. Metadata created timestamp should be the same different + 5. Metadata created timestamp should be the same + 6. Timestamps should be DateTime structs ]] local metaBefore = fs.metadata(TEMP_FILE_PATH) @@ -54,6 +57,9 @@ assert( "File metadata creation timestamp changed from modification" ) +assert(typeof(metaAfter.modifiedAt) == "DateTime", "File metadata modifiedAt is not a DateTime") +assert(typeof(metaAfter.createdAt) == "DateTime", "File metadata createdAt is not a DateTime") + --[[ 1. Permissions should exist 2. Our newly created file should not be readonly diff --git a/types/fs.luau b/types/fs.luau index eed03e4..d81ce6d 100644 --- a/types/fs.luau +++ b/types/fs.luau @@ -1,3 +1,6 @@ +local DateTime = require("./datetime") +type DateTime = DateTime.DateTime + export type MetadataKind = "file" | "dir" | "symlink" --[=[ @@ -37,9 +40,9 @@ export type MetadataPermissions = { export type Metadata = { kind: MetadataKind, exists: true, - createdAt: number, - modifiedAt: number, - accessedAt: number, + createdAt: DateTime, + modifiedAt: DateTime, + accessedAt: DateTime, permissions: MetadataPermissions, } | { kind: nil,