lune/crates/lune-std-fs/src/options.rs
2024-05-12 13:30:32 +02:00

31 lines
1,007 B
Rust

use mlua::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct FsWriteOptions {
pub(crate) overwrite: bool,
}
impl<'lua> FromLua<'lua> for FsWriteOptions {
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
Ok(match value {
LuaValue::Nil => Self { overwrite: false },
LuaValue::Boolean(b) => Self { overwrite: b },
LuaValue::Table(t) => {
let overwrite: Option<bool> = t.get("overwrite")?;
Self {
overwrite: overwrite.unwrap_or(false),
}
}
_ => {
return Err(LuaError::FromLuaConversionError {
from: value.type_name(),
to: "FsWriteOptions",
message: Some(format!(
"Invalid write options - expected boolean or table, got {}",
value.type_name()
)),
})
}
})
}
}