lua_protobuf_rs/descriptor/
enum_descriptor.rs1use crate::descriptor::enum_value_descriptor::LuaEnumValueDescriptor;
2use crate::descriptor::message_descriptor::LuaMessageDescriptor;
3use crate::descriptor_proto::enum_descriptor_proto::LuaEnumDescriptorProto;
4use derive_more::{Deref, From, Into};
5use mlua::prelude::LuaUserData;
6use mlua::{MetaMethod, UserDataMethods};
7use protobuf::reflect::EnumDescriptor;
8
9#[derive(Clone, Eq, PartialEq, Hash, Deref, From, Into)]
10pub struct LuaEnumDescriptor(pub EnumDescriptor);
11
12impl LuaUserData for LuaEnumDescriptor {
13 fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
14 methods.add_meta_method(MetaMethod::ToString, |_, this, ()| Ok(this.to_string()));
15
16 methods.add_method("proto", |_, this, ()| {
17 Ok::<LuaEnumDescriptorProto, _>(this.proto().clone().into())
18 });
19
20 methods.add_method("name", |_, this, ()| Ok(this.name().to_string()));
21
22 methods.add_method("full_name", |_, this, ()| Ok(this.full_name().to_string()));
23
24 methods.add_method("name_to_package", |_, this, ()| {
25 Ok(this.name_to_package().to_string())
26 });
27
28 methods.add_method("enclosing_message", |_, this, ()| {
33 Ok(this.enclosing_message().map(LuaMessageDescriptor))
34 });
35
36 methods.add_method("values", |_, this, ()| {
37 let values: Vec<_> = this.values().map(LuaEnumValueDescriptor).collect();
38 Ok(values)
39 });
40
41 methods.add_method("value_by_name", |_, this, name: String| {
42 Ok(this.value_by_name(&name).map(LuaEnumValueDescriptor))
43 });
44
45 methods.add_method("value_by_number", |_, this, number: i32| {
46 Ok(this.value_by_number(number).map(LuaEnumValueDescriptor))
47 });
48
49 methods.add_method("value_by_index", |_, this, index: usize| {
50 Ok(LuaEnumValueDescriptor(this.value_by_index(index)))
51 });
52
53 methods.add_method("default_value", |_, this, ()| {
54 Ok(LuaEnumValueDescriptor(this.default_value()))
55 });
56
57 methods.add_method("value_by_number_or_default", |_, this, number: i32| {
58 Ok(LuaEnumValueDescriptor(
59 this.value_by_number_or_default(number),
60 ))
61 });
62
63 }
67}