Change properties on fs.metadata to be DateTime values instead of numbers

This commit is contained in:
Filip Tibell 2024-01-14 17:06:41 +01:00
parent b322d3dfa5
commit 474ceb139a
No known key found for this signature in database
4 changed files with 23 additions and 11 deletions

View file

@ -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

View file

@ -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<f64>,
pub(crate) modified_at: Option<f64>,
pub(crate) accessed_at: Option<f64>,
pub(crate) created_at: Option<DateTime>,
pub(crate) modified_at: Option<DateTime>,
pub(crate) accessed_at: Option<DateTime>,
pub(crate) permissions: Option<FsPermissions>,
}
@ -116,7 +118,7 @@ impl FsMetadata {
impl<'lua> IntoLua<'lua> for FsMetadata {
fn into_lua(self, lua: &'lua Lua) -> LuaResult<LuaValue<'lua>> {
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<StdMetadata> 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<StdMetadata> for FsMetadata {
}
}
fn system_time_to_timestamp(res: IoResult<SystemTime>) -> Option<f64> {
fn system_time_to_timestamp(res: IoResult<SystemTime>) -> Option<DateTime> {
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,

View file

@ -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

View file

@ -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,