refactor: implement requested changes

This commit is contained in:
Erica Marigold 2023-09-04 12:23:18 +05:30
parent 09063e2279
commit 2b46958007
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
2 changed files with 12 additions and 32 deletions

View file

@ -97,21 +97,15 @@ impl DateTimeBuilder {
where
T: ToString,
{
let format_lazy: Lazy<String, _> = 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<String, _> = 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(),
})
}

View file

@ -22,13 +22,12 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
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::<Vec<&str>>()[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<Self> {
fn num_to_enum(num: i32) -> LuaResult<Timezone> {
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")),
}
}
}