luau 0.732.0

Safe lifetime-bound Rust embedding API for the Luau runtime
use super::super::MacroCounter;
use luau::{AnyUserdata, LuaRef, Result, Table};

#[luau::userdata_impl]
impl MacroCounter {
    const KIND: &'static str = "counter";

    #[luau(field)]
    fn metadata<'lua>(lua: LuaRef<'lua>) -> Result<Table<'lua>> {
        let metadata = lua.create_table()?;
        metadata.set("category", "number")?;
        Ok(metadata)
    }

    #[luau(infallible)]
    fn new(value: i32, secret: String) -> Self {
        Self { value, secret }
    }

    fn describe(
        &self,
        prefix: &str,
        bytes: &[u8],
        other: &MacroCounter,
        optional: Option<&MacroCounter>,
    ) -> Result<String> {
        Ok(format!(
            "{prefix}:{}:{}:{}:{}",
            bytes.len(),
            self.value,
            other.value,
            optional
                .map(|value| value.value.to_string())
                .unwrap_or_else(|| "none".to_string())
        ))
    }

    fn identity<'lua>(&self, _lua: LuaRef<'lua>, table: Table<'lua>) -> Result<Table<'lua>> {
        Ok(table)
    }

    fn take(self) -> Result<i32> {
        Ok(self.value)
    }

    #[luau(meta, infallible)]
    fn __call(_proxy: AnyUserdata<'_>, value: i32, secret: String) -> Self {
        Self { value, secret }
    }
}