MCPcopy Create free account
hub / github.com/StrataWM/strata / module

Function module

src/bindings/input/modflags.rs:48–91  ·  view source on GitHub ↗
(
	ctx: lua::Context<'gc>,
	comp: Rc<RefCell<StrataComp>>,
)

Source from the content-addressed store, hash-verified

46}
47
48pub fn module<'gc>(
49 ctx: lua::Context<'gc>,
50 comp: Rc<RefCell<StrataComp>>,
51) -> anyhow::Result<lua::Value<'gc>> {
52 let meta = lua::Table::new(&ctx);
53
54 meta.set(
55 ctx,
56 lua::MetaMethod::Index,
57 lua::Callback::from_fn(&ctx, |ctx, _, mut stack| {
58 let _ = stack.pop_front();
59
60 let k = stack.consume::<lua::String>(ctx)?;
61 let k = k.to_str()?;
62 let bits =
63 ModFlags::from_name(k).ok_or_else(|| anyhow::anyhow!("invalid Mod key: {}", k))?;
64 let bits = lua::UserData::new_static(&ctx, bits);
65
66 let bits_meta = lua::Table::new(&ctx);
67 bits_meta.set(
68 ctx,
69 lua::MetaMethod::ToString,
70 lua::Callback::from_fn(&ctx, |ctx, _, mut stack| {
71 let this = stack.consume::<lua::UserData>(ctx)?;
72 let this = this.downcast_static::<ModFlags>()?;
73
74 stack.push_front(format!("{:#?}", this).into_value(ctx));
75 Ok(lua::CallbackReturn::Return)
76 }),
77 )?;
78 bits.set_metatable(&ctx, Some(bits_meta));
79
80 stack.push_front(lua::Value::UserData(bits));
81
82 Ok(lua::CallbackReturn::Return)
83 }),
84 )?;
85
86 let ud = lua::UserData::new_static(&ctx, comp);
87
88 ud.set_metatable(&ctx, Some(meta));
89
90 Ok(lua::Value::UserData(ud))
91}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected