Debian Codegraph @

third_party/rust/uniffi_udl/src/converters/interface.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::InterfaceAttributes;
7 use crate::{converters::convert_docstring, InterfaceCollector};
8 use anyhow::{bail, Result};
9 use std::collections::HashSet;
10 use uniffi_meta::{
11 ConstructorMetadata, FnParamMetadata, MethodMetadata, ObjectImpl, ObjectMetadata, Type,
12 UniffiTraitMetadata,
13 };
14
15 impl APIConverter<ObjectMetadata> for weedle::InterfaceDefinition<'_> {
16 fn convert(&self, ci: &mut InterfaceCollector) -> Result<ObjectMetadata> {
17 if self.inheritance.is_some() {
18 bail!("interface inheritance is not supported");
19 }
20 let attributes = match &self.attributes {
21 Some(attrs) => InterfaceAttributes::try_from(attrs)?,
22 None => Default::default(),
23 };
24
25 let object_name = self.identifier.0;
26 let object_impl = attributes.object_impl()?;
27 // Convert each member into a constructor or method, guarding against duplicate names.
28 // They get added to the ci and aren't carried in ObjectMetadata.
29 let mut member_names = HashSet::new();
30 for member in &self.members.body {
31 match member {
32 weedle::interface::InterfaceMember::Constructor(t) => {
33 let mut cons: ConstructorMetadata = t.convert(ci)?;
34 if object_impl == ObjectImpl::Trait {
35 bail!(
36 "Trait interfaces can not have constructors: \"{}\"",
37 cons.name
38 )
39 }
40 if !member_names.insert(cons.name.clone()) {
41 bail!("Duplicate interface member name: \"{}\"", cons.name)
42 }
43 cons.self_name = object_name.to_string();
44 ci.items.insert(cons.into());
45 }
46 weedle::interface::InterfaceMember::Operation(t) => {
47 let mut method: MethodMetadata = t.convert(ci)?;
48 if !member_names.insert(method.name.clone()) {
49 bail!("Duplicate interface member name: \"{}\"", method.name)
50 }
51 method.self_name = object_name.to_string();
52 ci.items.insert(method.into());
53 }
54 _ => bail!("no support for interface member type {:?} yet", member),
55 }
56 }
57 // A helper for our trait methods
58 let make_trait_method = |name: &str,
59 inputs: Vec<FnParamMetadata>,
60 return_type: Option<Type>|
61 -> Result<MethodMetadata> {
62 Ok(MethodMetadata {
63 module_path: ci.module_path(),
64 // The name is used to create the ffi function for the method.
65 name: name.to_string(),
66 self_name: object_name.to_string(),
67 is_async: false,
68 inputs,
69 return_type,
70 throws: None,
71 takes_self_by_arc: false,
72 checksum: None,
73 docstring: None,
74 })
75 };
76 // Trait methods are in the Metadata.
77 let uniffi_traits = attributes
78 .get_traits()
79 .into_iter()
80 .map(|trait_name| {
81 Ok(match trait_name.as_str() {
82 "Debug" => UniffiTraitMetadata::Debug {
83 fmt: make_trait_method("uniffi_trait_debug", vec![], Some(Type::String))?,
84 },
85 "Display" => UniffiTraitMetadata::Display {
86 fmt: make_trait_method("uniffi_trait_display", vec![], Some(Type::String))?,
87 },
88 "Eq" => UniffiTraitMetadata::Eq {
89 eq: make_trait_method(
90 "uniffi_trait_eq_eq",
91 vec![FnParamMetadata {
92 name: "other".to_string(),
93 ty: Type::Object {
94 module_path: ci.module_path(),
95 name: object_name.to_string(),
96 imp: object_impl,
97 },
98 by_ref: true,
99 default: None,
100 optional: false,
101 }],
102 Some(Type::Boolean),
103 )?,
104 ne: make_trait_method(
105 "uniffi_trait_eq_ne",
106 vec![FnParamMetadata {
107 name: "other".to_string(),
108 ty: Type::Object {
109 module_path: ci.module_path(),
110 name: object_name.to_string(),
111 imp: object_impl,
112 },
113 by_ref: true,
114 default: None,
115 optional: false,
116 }],
117 Some(Type::Boolean),
118 )?,
119 },
120 "Hash" => UniffiTraitMetadata::Hash {
121 hash: make_trait_method("uniffi_trait_hash", vec![], Some(Type::UInt64))?,
122 },
123 _ => bail!("Invalid trait name: {}", trait_name),
124 })
125 })
126 .collect::<Result<Vec<_>>>()?;
127 for ut in uniffi_traits {
128 ci.items.insert(ut.into());
129 }
130 Ok(ObjectMetadata {
131 module_path: ci.module_path(),
132 name: object_name.to_string(),
133 remote: attributes.contains_remote(),
134 imp: object_impl,
135 docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)),
136 })
137 }
138 }