lua_protobuf_rs/
runtime_type.rs1use crate::descriptor::enum_descriptor::LuaEnumDescriptor;
2use crate::descriptor::message_descriptor::LuaMessageDescriptor;
3use derive_more::{Deref, From, Into};
4use mlua::prelude::LuaUserData;
5use mlua::UserDataMethods;
6use protobuf::reflect::RuntimeType;
7
8#[derive(Debug, Clone, Eq, PartialEq, Deref, From, Into)]
9pub struct LuaRuntimeType(pub RuntimeType);
10
11impl LuaUserData for LuaRuntimeType {
12 fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
13 methods.add_method("is_i32", |_, this, ()| {
14 Ok(matches!(this.deref(), RuntimeType::I32))
15 });
16
17 methods.add_method("is_i64", |_, this, ()| {
18 Ok(matches!(this.deref(), RuntimeType::I64))
19 });
20
21 methods.add_method("is_u32", |_, this, ()| {
22 Ok(matches!(this.deref(), RuntimeType::U32))
23 });
24
25 methods.add_method("is_u64", |_, this, ()| {
26 Ok(matches!(this.deref(), RuntimeType::U64))
27 });
28
29 methods.add_method("is_f32", |_, this, ()| {
30 Ok(matches!(this.deref(), RuntimeType::F32))
31 });
32
33 methods.add_method("is_f64", |_, this, ()| {
34 Ok(matches!(this.deref(), RuntimeType::F64))
35 });
36
37 methods.add_method("is_bool", |_, this, ()| {
38 Ok(matches!(this.deref(), RuntimeType::Bool))
39 });
40
41 methods.add_method("is_string", |_, this, ()| {
42 Ok(matches!(this.deref(), RuntimeType::String))
43 });
44
45 methods.add_method("is_vec_u8", |_, this, ()| {
46 Ok(matches!(this.deref(), RuntimeType::VecU8))
47 });
48
49 methods.add_method("is_enum", |_, this, ()| {
50 Ok(matches!(this.deref(), RuntimeType::Enum(_)))
51 });
52
53 methods.add_method("get_enum", |_, this, ()| {
54 if let RuntimeType::Enum(descriptor) = this.deref() {
55 let descriptor: LuaEnumDescriptor = From::from(descriptor.clone());
56 Ok(Some(descriptor))
57 } else {
58 Ok(None)
59 }
60 });
61
62 methods.add_method("is_message", |_, this, ()| {
63 Ok(matches!(this.deref(), RuntimeType::Message(_)))
64 });
65
66 methods.add_method("get_message", |_, this, ()| {
67 if let RuntimeType::Message(message) = this.deref() {
68 let descriptor: LuaMessageDescriptor = From::from(message.clone());
69 Ok(Some(descriptor))
70 } else {
71 Ok(None)
72 }
73 });
74 }
75}