Skip to content

Commit

Permalink
Change properties on fs.metadata to be DateTime values instead of num…
Browse files Browse the repository at this point in the history
…bers
  • Loading branch information
filiptibell committed Jan 14, 2024
1 parent b322d3d commit 474ceb1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions src/lune/builtins/fs/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
}

Expand All @@ -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)?;
Expand All @@ -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()),
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion tests/fs/metadata.luau
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--!nolint UnknownType

local TEMP_DIR_PATH = "bin/"
local TEMP_FILE_PATH = TEMP_DIR_PATH .. "metadata_test"

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions types/fs.luau
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local DateTime = require("./datetime")
type DateTime = DateTime.DateTime

export type MetadataKind = "file" | "dir" | "symlink"

--[=[
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 474ceb1

Please sign in to comment.