Debian Codegraph @

third_party/rust/uniffi_udl/src/converters/callables.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use super::APIConverter;
6 use crate::attributes::ArgumentAttributes;
7 use crate::attributes::{ConstructorAttributes, FunctionAttributes, MethodAttributes};
8 use crate::converters::convert_docstring;
9 use crate::literal::convert_default_value;
10 use crate::InterfaceCollector;
11 use anyhow::{bail, Result};
12
13 use uniffi_meta::{
14 ConstructorMetadata, FieldMetadata, FnMetadata, FnParamMetadata, MethodMetadata,
15 TraitMethodMetadata,
16 };
17
18 impl APIConverter<FieldMetadata> for weedle::argument::Argument<'_> {
19 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FieldMetadata> {
20 match self {
21 weedle::argument::Argument::Single(t) => t.convert(ci),
22 weedle::argument::Argument::Variadic(_) => bail!("variadic arguments not supported"),
23 }
24 }
25 }
26
27 impl APIConverter<FieldMetadata> for weedle::argument::SingleArgument<'_> {
28 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FieldMetadata> {
29 let type_ = ci.resolve_type_expression(&self.type_)?;
30 if self.default.is_some() {
31 bail!("enum interface variant fields must not have default values");
32 }
33 if self.attributes.is_some() {
34 bail!("enum interface variant fields must not have attributes");
35 }
36 Ok(FieldMetadata {
37 name: self.identifier.0.to_string(),
38 ty: type_,
39 default: None,
40 docstring: None,
41 })
42 }
43 }
44
45 impl APIConverter<FnParamMetadata> for weedle::argument::Argument<'_> {
46 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FnParamMetadata> {
47 match self {
48 weedle::argument::Argument::Single(t) => t.convert(ci),
49 weedle::argument::Argument::Variadic(_) => bail!("variadic arguments not supported"),
50 }
51 }
52 }
53
54 impl APIConverter<FnParamMetadata> for weedle::argument::SingleArgument<'_> {
55 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FnParamMetadata> {
56 let type_ = ci.resolve_type_expression(&self.type_)?;
57 let default = match self.default {
58 None => None,
59 Some(v) => Some(convert_default_value(&v.value, &type_)?),
60 };
61 let by_ref = ArgumentAttributes::try_from(self.attributes.as_ref())?.by_ref();
62 Ok(FnParamMetadata {
63 name: self.identifier.0.to_string(),
64 ty: type_,
65 by_ref,
66 optional: self.optional.is_some(),
67 default,
68 })
69 }
70 }
71
72 impl APIConverter<FnMetadata> for weedle::namespace::NamespaceMember<'_> {
73 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FnMetadata> {
74 match self {
75 weedle::namespace::NamespaceMember::Operation(f) => f.convert(ci),
76 _ => bail!("no support for namespace member type {:?} yet", self),
77 }
78 }
79 }
80
81 impl APIConverter<FnMetadata> for weedle::namespace::OperationNamespaceMember<'_> {
82 fn convert(&self, ci: &mut InterfaceCollector) -> Result<FnMetadata> {
83 let return_type = ci.resolve_return_type_expression(&self.return_type)?;
84 let name = match self.identifier {
85 None => bail!("anonymous functions are not supported {:?}", self),
86 Some(id) => id.0.to_string(),
87 };
88 let attrs = FunctionAttributes::try_from(self.attributes.as_ref())?;
89 let is_async = attrs.is_async();
90 let throws = match attrs.get_throws_err() {
91 None => None,
92 Some(name) => match ci.get_type(name) {
93 Some(t) => Some(t),
94 None => bail!("unknown type for error: {name}"),
95 },
96 };
97 Ok(FnMetadata {
98 module_path: ci.module_path(),
99 name,
100 is_async,
101 return_type,
102 inputs: self.args.body.list.convert(ci)?,
103 throws,
104 docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)),
105 checksum: None,
106 })
107 }
108 }
109
110 impl APIConverter<ConstructorMetadata> for weedle::interface::ConstructorInterfaceMember<'_> {
111 fn convert(&self, ci: &mut InterfaceCollector) -> Result<ConstructorMetadata> {
112 let attributes = match &self.attributes {
113 Some(attr) => ConstructorAttributes::try_from(attr)?,
114 None => Default::default(),
115 };
116 let throws = attributes
117 .get_throws_err()
118 .map(|name| ci.get_type(name).expect("invalid throws type"));
119 Ok(ConstructorMetadata {
120 module_path: ci.module_path(),
121 name: String::from(attributes.get_name().unwrap_or("new")),
122 // We don't know the name of the containing `Object` at this point, fill it in later.
123 self_name: Default::default(),
124 is_async: attributes.is_async(),
125 // Also fill in checksum_fn_name later, since it depends on object_name
126 inputs: self.args.body.list.convert(ci)?,
127 throws,
128 checksum: None,
129 docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)),
130 })
131 }
132 }
133
134 impl APIConverter<MethodMetadata> for weedle::interface::OperationInterfaceMember<'_> {
135 fn convert(&self, ci: &mut InterfaceCollector) -> Result<MethodMetadata> {
136 if self.special.is_some() {
137 bail!("special operations not supported");
138 }
139 if self.modifier.is_some() {
140 bail!("method modifiers are not supported")
141 }
142 let return_type = ci.resolve_return_type_expression(&self.return_type)?;
143 let attributes = MethodAttributes::try_from(self.attributes.as_ref())?;
144 let is_async = attributes.is_async();
145
146 let throws = match attributes.get_throws_err() {
147 Some(name) => match ci.get_type(name) {
148 Some(t) => Some(t),
149 None => bail!("unknown type for error: {name}"),
150 },
151 None => None,
152 };
153
154 let takes_self_by_arc = attributes.get_self_by_arc();
155 Ok(MethodMetadata {
156 module_path: ci.module_path(),
157 name: match self.identifier {
158 None => bail!("anonymous methods are not supported {:?}", self),
159 Some(id) => {
160 let name = id.0.to_string();
161 if name == "new" {
162 bail!("the method name \"new\" is reserved for the default constructor");
163 }
164 name
165 }
166 },
167 // We don't know the name of the containing `Object` at this point, fill it in later.
168 self_name: Default::default(),
169 is_async,
170 inputs: self.args.body.list.convert(ci)?,
171 return_type,
172 throws,
173 takes_self_by_arc,
174 checksum: None,
175 docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)),
176 })
177 }
178 }
179
180 impl APIConverter<TraitMethodMetadata> for weedle::interface::OperationInterfaceMember<'_> {
181 fn convert(&self, ci: &mut InterfaceCollector) -> Result<TraitMethodMetadata> {
182 if self.special.is_some() {
183 bail!("special operations not supported");
184 }
185 if self.modifier.is_some() {
186 bail!("method modifiers are not supported")
187 }
188 let return_type = ci.resolve_return_type_expression(&self.return_type)?;
189 let attributes = MethodAttributes::try_from(self.attributes.as_ref())?;
190 let is_async = attributes.is_async();
191
192 let throws = match attributes.get_throws_err() {
193 Some(name) => match ci.get_type(name) {
194 Some(t) => Some(t),
195 None => bail!("unknown type for error: {name}"),
196 },
197 None => None,
198 };
199
200 let takes_self_by_arc = attributes.get_self_by_arc();
201 Ok(TraitMethodMetadata {
202 module_path: ci.module_path(),
203 trait_name: Default::default(), // we'll fill these in later.
204 index: Default::default(),
205 name: match self.identifier {
206 None => bail!("anonymous methods are not supported {:?}", self),
207 Some(id) => {
208 let name = id.0.to_string();
209 if name == "new" {
210 bail!("the method name \"new\" is reserved for the default constructor");
211 }
212 name
213 }
214 },
215 is_async,
216 inputs: self.args.body.list.convert(ci)?,
217 return_type,
218 throws,
219 takes_self_by_arc,
220 checksum: None,
221 docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)),
222 })
223 }
224 }