From 2b469580078b3873d9d11c0f29042226d1f7acb9 Mon Sep 17 00:00:00 2001 From: Compey Date: Mon, 4 Sep 2023 12:23:18 +0530 Subject: [PATCH] refactor: implement requested changes --- src/lune/builtins/datetime/builder.rs | 26 ++++++++++---------------- src/lune/builtins/datetime/mod.rs | 18 ++---------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/lune/builtins/datetime/builder.rs b/src/lune/builtins/datetime/builder.rs index 8cfc48f..6a0a88f 100644 --- a/src/lune/builtins/datetime/builder.rs +++ b/src/lune/builtins/datetime/builder.rs @@ -97,21 +97,15 @@ impl DateTimeBuilder { where T: ToString, { - let format_lazy: Lazy = Lazy::new(|| { - if let Some(fmt) = format { - fmt.to_string() - } else { - "%Y-%m-%dT%H:%M:%SZ".to_string() - } - }); + let format = match format { + Some(fmt) => fmt.to_string(), + None => "%Y-%m-%dT%H:%M:%SZ".to_string(), + }; - let locale_lazy: Lazy = Lazy::new(|| { - if let Some(locale) = locale { - locale.to_string() - } else { - "en".to_string() - } - }); + let locale = match locale { + Some(locale) => locale.to_string(), + None => "en".to_string(), + }; Ok(match timezone { Timezone::Utc => Utc @@ -125,7 +119,7 @@ impl DateTimeBuilder { ) .single() .ok_or(())? - .formatl((*format_lazy).as_str(), (*locale_lazy).as_str()) + .formatl((format).as_str(), locale.as_str()) .to_string(), Timezone::Local => Local .with_ymd_and_hms( @@ -138,7 +132,7 @@ impl DateTimeBuilder { ) .single() .ok_or(())? - .formatl((*format_lazy).as_str(), (*locale_lazy).as_str()) + .formatl((format).as_str(), locale.as_str()) .to_string(), }) } diff --git a/src/lune/builtins/datetime/mod.rs b/src/lune/builtins/datetime/mod.rs index 320d239..70a4af0 100644 --- a/src/lune/builtins/datetime/mod.rs +++ b/src/lune/builtins/datetime/mod.rs @@ -22,13 +22,12 @@ pub fn create(lua: &'static Lua) -> LuaResult { TimestampType::Seconds => timestamp_cloned.as_i64().ok_or(LuaError::external("invalid float integer timestamp supplied"))?, TimestampType::Millis => { let timestamp = timestamp_cloned.as_f64().ok_or(LuaError::external("invalid float timestamp with millis component supplied"))?; - ((((timestamp - timestamp.fract()) as u64) * 1000_u64) // converting the whole seconds part to millis // the ..3 gets a &str of the first 3 chars of the digits after the decimals, ignoring // additional floating point accuracy digits + (timestamp.fract() * (10_u64.pow(timestamp.fract().to_string().split('.').collect::>()[1][..3].len() as u32)) as f64) as u64) as i64 // adding the millis to the fract as a whole number - // HACK: 10 * (timestamp.fract().to_string().len() - 2) gives us the number of digits + // HACK: 10 ** (timestamp.fract().to_string().len() - 2) gives us the number of digits // after the decimal } }; @@ -187,7 +186,6 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder { .with_minute(t.get("minute")?) .with_second(t.get("second")?) .with_millisecond(t.get("millisecond")?) - // TODO: millisecond support .build()), _ => Err(LuaError::external( "expected type table for DateTimeBuilder", @@ -198,25 +196,13 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder { impl<'lua> FromLua<'lua> for Timezone { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult { - fn num_to_enum(num: i32) -> LuaResult { - match num { - 1 => Ok(Timezone::Utc), - 2 => Ok(Timezone::Local), - _ => Err(LuaError::external("Invalid enum member!")), - } - } - match value { - LuaValue::Integer(num) => num_to_enum(num), - LuaValue::Number(num) => num_to_enum(num as i32), LuaValue::String(str) => match str.to_str()?.to_lowercase().as_str() { "utc" => Ok(Timezone::Utc), "local" => Ok(Timezone::Local), &_ => Err(LuaError::external("Invalid enum member!")), }, - _ => Err(LuaError::external( - "Invalid enum type, number or string expected", - )), + _ => Err(LuaError::external("Invalid enum type, string expected")), } } }