lua_protobuf_rs/descriptor_proto/
file_descriptor_proto.rs

1use crate::descriptor_proto::descriptor_proto::LuaDescriptorProto;
2use crate::descriptor_proto::enum_descriptor_proto::LuaEnumDescriptorProto;
3use crate::descriptor_proto::field_descriptor_proto::LuaFieldDescriptorProto;
4use crate::descriptor_proto::file_options::LuaFileOptions;
5use crate::descriptor_proto::service_descriptor_proto::LuaServiceDescriptorProto;
6use crate::descriptor_proto::source_code_info::LuaSourceCodeInfo;
7use crate::{add_message_dyn_trait_method, add_message_full_trait_method, add_message_trait_method};
8use derive_more::{Deref, DerefMut, From, Into};
9use mlua::prelude::LuaUserData;
10use mlua::{MetaMethod, UserDataFields, UserDataMethods};
11use protobuf::descriptor::FileDescriptorProto;
12
13#[derive(PartialEq, Clone, Default, Debug, Deref, DerefMut, From, Into, mlua::FromLua)]
14pub struct LuaFileDescriptorProto(FileDescriptorProto);
15
16impl LuaUserData for LuaFileDescriptorProto {
17    fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
18        fields.add_field_method_get("name", |_, this| {
19            Ok(this.name.clone())
20        });
21
22        fields.add_field_method_get("package", |_, this| {
23            Ok(this.package.clone())
24        });
25
26        fields.add_field_method_get("dependency", |_, this| {
27            Ok(this.dependency.clone())
28        });
29
30        fields.add_field_method_get("public_dependency", |_, this| {
31            Ok(this.public_dependency.clone())
32        });
33
34        fields.add_field_method_get("weak_dependency", |_, this| {
35            Ok(this.weak_dependency.clone())
36        });
37
38        fields.add_field_method_get("message_type", |_, this| {
39            let message_type = this.message_type.iter().map(Clone::clone).map(Into::into).collect::<Vec<LuaDescriptorProto>>();
40            Ok(message_type)
41        });
42
43        fields.add_field_method_get("enum_type", |_, this| {
44            let enum_type = this.enum_type.iter().map(Clone::clone).map(Into::into).collect::<Vec<LuaEnumDescriptorProto>>();
45            Ok(enum_type)
46        });
47
48        fields.add_field_method_get("service", |_, this| {
49            let service = this.service.iter().map(Clone::clone).map(Into::into).collect::<Vec<LuaServiceDescriptorProto>>();
50            Ok(service)
51        });
52
53        fields.add_field_method_get("extension", |_, this| {
54            let extension = this.extension.iter().map(Clone::clone).map(Into::into).collect::<Vec<LuaFieldDescriptorProto>>();
55            Ok(extension)
56        });
57
58        fields.add_field_method_get("options", |_, this| {
59            let options: Option<LuaFileOptions> = this.options.clone().into_option().map(Into::into);
60            Ok(options)
61        });
62
63        fields.add_field_method_get("source_code_info", |_, this| {
64            let source_code_info: Option<LuaSourceCodeInfo> = this.source_code_info.clone().into_option().map(Into::into);
65            Ok(source_code_info)
66        });
67
68        fields.add_field_method_get("syntax", |_, this| {
69            Ok(this.syntax.clone())
70        });
71    }
72
73    fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
74        methods.add_meta_method(MetaMethod::ToString, |_, this, ()| {
75            Ok(this.to_string())
76        });
77
78        methods.add_method("name", |_, this, ()| {
79            Ok(this.name().to_string())
80        });
81
82        methods.add_method("has_name", |_, this, ()| {
83            Ok(this.has_name())
84        });
85
86        methods.add_method_mut("clear_name", |_, this, ()| {
87            this.clear_name();
88            Ok(())
89        });
90
91        methods.add_method_mut("set_name", |_, this, value: String| {
92            this.set_name(value);
93            Ok(())
94        });
95
96        methods.add_method_mut("mut_name", |_, this, ()| {
97            Ok(this.mut_name().clone())
98        });
99
100        methods.add_method_mut("take_name", |_, this, ()| {
101            Ok(this.take_name())
102        });
103
104        methods.add_method("package", |_, this, ()| {
105            Ok(this.package().to_string())
106        });
107
108        methods.add_method("has_package", |_, this, ()| {
109            Ok(this.has_package())
110        });
111
112        methods.add_method_mut("clear_package", |_, this, ()| {
113            this.clear_package();
114            Ok(())
115        });
116
117        methods.add_method_mut("set_package", |_, this, value: String| {
118            this.set_package(value);
119            Ok(())
120        });
121
122        methods.add_method_mut("mut_package", |_, this, ()| {
123            Ok(this.mut_package().clone())
124        });
125
126        methods.add_method_mut("take_package", |_, this, ()| {
127            Ok(this.take_package())
128        });
129
130        methods.add_method("syntax", |_, this, ()| {
131            Ok(this.syntax().to_string())
132        });
133
134        methods.add_method("has_syntax", |_, this, ()| {
135            Ok(this.has_syntax())
136        });
137
138        methods.add_method_mut("clear_syntax", |_, this, ()| {
139            this.clear_syntax();
140            Ok(())
141        });
142
143        methods.add_method_mut("set_syntax", |_, this, value: String| {
144            this.set_syntax(value);
145            Ok(())
146        });
147
148        methods.add_method_mut("mut_syntax", |_, this, ()| {
149            Ok(this.mut_syntax().clone())
150        });
151
152        methods.add_method_mut("take_syntax", |_, this, ()| {
153            Ok(this.take_syntax())
154        });
155
156        add_message_trait_method!(methods, FileDescriptorProto, LuaFileDescriptorProto);
157
158        add_message_dyn_trait_method!(methods, FileDescriptorProto, LuaFileDescriptorProto);
159
160        add_message_full_trait_method!(methods, FileDescriptorProto, LuaFileDescriptorProto);
161    }
162}