| 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 indexmap::IndexMap; |
| 6 |
use uniffi_pipeline::Node; |
| 7 |
|
| 8 |
/// Root node of the Initial IR |
| 9 |
#[derive(Debug, Clone, Node)] |
| 10 |
pub struct Root { |
| 11 |
pub modules: IndexMap<String, Module>, |
| 12 |
/// The library path the user passed to us, if we're in library mode |
| 13 |
pub cdylib: Option<String>, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Debug, Clone, Node)] |
| 17 |
pub struct Module { |
| 18 |
pub name: String, |
| 19 |
pub crate_name: String, |
| 20 |
/// contents of the `uniffi.toml` file for this module, if present |
| 21 |
pub config_toml: Option<String>, |
| 22 |
pub docstring: Option<String>, |
| 23 |
pub functions: Vec<Function>, |
| 24 |
pub type_definitions: Vec<TypeDefinition>, |
| 25 |
} |
| 26 |
|
| 27 |
#[derive(Debug, Clone, Node)] |
| 28 |
#[node(from(FnMetadata))] |
| 29 |
pub struct Function { |
| 30 |
pub name: String, |
| 31 |
pub is_async: bool, |
| 32 |
pub inputs: Vec<Argument>, |
| 33 |
pub return_type: Option<Type>, |
| 34 |
pub throws: Option<Type>, |
| 35 |
pub checksum: Option<u16>, |
| 36 |
pub docstring: Option<String>, |
| 37 |
} |
| 38 |
|
| 39 |
#[derive(Debug, Clone, Node)] |
| 40 |
pub enum TypeDefinition { |
| 41 |
Interface(Interface), |
| 42 |
CallbackInterface(CallbackInterface), |
| 43 |
Record(Record), |
| 44 |
Enum(Enum), |
| 45 |
Custom(CustomType), |
| 46 |
} |
| 47 |
|
| 48 |
#[derive(Debug, Clone, Node)] |
| 49 |
#[node(from(ConstructorMetadata))] |
| 50 |
pub struct Constructor { |
| 51 |
pub name: String, |
| 52 |
pub is_async: bool, |
| 53 |
pub inputs: Vec<Argument>, |
| 54 |
pub throws: Option<Type>, |
| 55 |
pub checksum: Option<u16>, |
| 56 |
pub docstring: Option<String>, |
| 57 |
} |
| 58 |
|
| 59 |
#[derive(Debug, Clone, Node)] |
| 60 |
#[node(from(MethodMetadata))] |
| 61 |
pub struct Method { |
| 62 |
pub name: String, |
| 63 |
pub is_async: bool, |
| 64 |
pub inputs: Vec<Argument>, |
| 65 |
pub return_type: Option<Type>, |
| 66 |
pub throws: Option<Type>, |
| 67 |
pub checksum: Option<u16>, |
| 68 |
pub docstring: Option<String>, |
| 69 |
} |
| 70 |
|
| 71 |
#[derive(Debug, Clone, Node)] |
| 72 |
#[node(from(TraitMethodMetadata))] |
| 73 |
pub struct TraitMethod { |
| 74 |
pub trait_name: String, |
| 75 |
// Note: the position of `index` is important since it causes callback interface methods to be |
| 76 |
// ordered correctly in MetadataGroup.items |
| 77 |
pub index: u32, |
| 78 |
pub name: String, |
| 79 |
pub is_async: bool, |
| 80 |
pub inputs: Vec<Argument>, |
| 81 |
pub return_type: Option<Type>, |
| 82 |
pub throws: Option<Type>, |
| 83 |
pub checksum: Option<u16>, |
| 84 |
pub docstring: Option<String>, |
| 85 |
} |
| 86 |
|
| 87 |
#[derive(Debug, Clone, Node)] |
| 88 |
#[node(from(FnParamMetadata))] |
| 89 |
pub struct Argument { |
| 90 |
pub name: String, |
| 91 |
pub ty: Type, |
| 92 |
pub optional: bool, |
| 93 |
pub default: Option<Literal>, |
| 94 |
} |
| 95 |
|
| 96 |
#[derive(Debug, Clone, Node)] |
| 97 |
#[node(from(LiteralMetadata))] |
| 98 |
pub enum Literal { |
| 99 |
Boolean(bool), |
| 100 |
String(String), |
| 101 |
// Integers are represented as the widest representation we can. |
| 102 |
// Number formatting vary with language and radix, so we avoid a lot of parsing and |
| 103 |
// formatting duplication by using only signed and unsigned variants. |
| 104 |
UInt(u64, Radix, Type), |
| 105 |
Int(i64, Radix, Type), |
| 106 |
// Pass the string representation through as typed in the UDL. |
| 107 |
// This avoids a lot of uncertainty around precision and accuracy, |
| 108 |
// though bindings for languages less sophisticated number parsing than WebIDL |
| 109 |
// will have to do extra work. |
| 110 |
Float(String, Type), |
| 111 |
Enum(String, Type), |
| 112 |
EmptySequence, |
| 113 |
EmptyMap, |
| 114 |
None, |
| 115 |
Some { inner: Box<Literal> }, |
| 116 |
} |
| 117 |
|
| 118 |
// Represent the radix of integer literal values. |
| 119 |
// We preserve the radix into the generated bindings for readability reasons. |
| 120 |
#[derive(Debug, Clone, Node)] |
| 121 |
pub enum Radix { |
| 122 |
Decimal = 10, |
| 123 |
Octal = 8, |
| 124 |
Hexadecimal = 16, |
| 125 |
} |
| 126 |
|
| 127 |
#[derive(Debug, Clone, Node)] |
| 128 |
#[node(from(RecordMetadata))] |
| 129 |
pub struct Record { |
| 130 |
pub name: String, |
| 131 |
pub fields: Vec<Field>, |
| 132 |
pub docstring: Option<String>, |
| 133 |
} |
| 134 |
|
| 135 |
#[derive(Debug, Clone, Node)] |
| 136 |
#[node(from(FieldMetadata))] |
| 137 |
pub struct Field { |
| 138 |
pub name: String, |
| 139 |
pub ty: Type, |
| 140 |
pub default: Option<Literal>, |
| 141 |
pub docstring: Option<String>, |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Debug, Clone, Node)] |
| 145 |
pub enum EnumShape { |
| 146 |
Enum, |
| 147 |
Error { flat: bool }, |
| 148 |
} |
| 149 |
|
| 150 |
#[derive(Debug, Clone, Node)] |
| 151 |
#[node(from(EnumMetadata))] |
| 152 |
pub struct Enum { |
| 153 |
pub name: String, |
| 154 |
pub shape: EnumShape, |
| 155 |
pub variants: Vec<Variant>, |
| 156 |
pub discr_type: Option<Type>, |
| 157 |
pub docstring: Option<String>, |
| 158 |
} |
| 159 |
|
| 160 |
#[derive(Debug, Clone, Node)] |
| 161 |
#[node(from(VariantMetadata))] |
| 162 |
pub struct Variant { |
| 163 |
pub name: String, |
| 164 |
pub discr: Option<Literal>, |
| 165 |
pub fields: Vec<Field>, |
| 166 |
pub docstring: Option<String>, |
| 167 |
} |
| 168 |
|
| 169 |
#[derive(Debug, Clone, Node)] |
| 170 |
#[node(from(ObjectMetadata))] |
| 171 |
pub struct Interface { |
| 172 |
pub name: String, |
| 173 |
pub docstring: Option<String>, |
| 174 |
pub constructors: Vec<Constructor>, |
| 175 |
pub methods: Vec<Method>, |
| 176 |
pub uniffi_traits: Vec<UniffiTrait>, |
| 177 |
pub trait_impls: Vec<ObjectTraitImpl>, |
| 178 |
pub imp: ObjectImpl, |
| 179 |
} |
| 180 |
|
| 181 |
#[derive(Debug, Clone, Node)] |
| 182 |
#[node(from(CallbackInterfaceMetadata))] |
| 183 |
pub struct CallbackInterface { |
| 184 |
pub name: String, |
| 185 |
pub docstring: Option<String>, |
| 186 |
pub methods: Vec<Method>, |
| 187 |
} |
| 188 |
|
| 189 |
#[derive(Debug, Clone, Node)] |
| 190 |
#[node(from(UniffiTraitMetadata))] |
| 191 |
pub enum UniffiTrait { |
| 192 |
Debug { fmt: Method }, |
| 193 |
Display { fmt: Method }, |
| 194 |
Eq { eq: Method, ne: Method }, |
| 195 |
Hash { hash: Method }, |
| 196 |
} |
| 197 |
|
| 198 |
#[derive(Debug, Clone, Node)] |
| 199 |
#[node(from(ObjectTraitImplMetadata))] |
| 200 |
pub struct ObjectTraitImpl { |
| 201 |
pub ty: Type, |
| 202 |
pub trait_name: String, |
| 203 |
pub tr_module_name: Option<String>, |
| 204 |
} |
| 205 |
|
| 206 |
#[derive(Debug, Clone, Node)] |
| 207 |
#[node(from(CustomTypeMetadata))] |
| 208 |
pub struct CustomType { |
| 209 |
pub name: String, |
| 210 |
pub builtin: Type, |
| 211 |
pub docstring: Option<String>, |
| 212 |
} |
| 213 |
|
| 214 |
#[derive(Debug, Clone, Node)] |
| 215 |
pub enum Type { |
| 216 |
// Primitive types. |
| 217 |
UInt8, |
| 218 |
Int8, |
| 219 |
UInt16, |
| 220 |
Int16, |
| 221 |
UInt32, |
| 222 |
Int32, |
| 223 |
UInt64, |
| 224 |
Int64, |
| 225 |
Float32, |
| 226 |
Float64, |
| 227 |
Boolean, |
| 228 |
String, |
| 229 |
Bytes, |
| 230 |
Timestamp, |
| 231 |
Duration, |
| 232 |
// Structurally recursive types. |
| 233 |
Optional { |
| 234 |
inner_type: Box<Type>, |
| 235 |
}, |
| 236 |
Sequence { |
| 237 |
inner_type: Box<Type>, |
| 238 |
}, |
| 239 |
Map { |
| 240 |
key_type: Box<Type>, |
| 241 |
value_type: Box<Type>, |
| 242 |
}, |
| 243 |
// User defined types in the API |
| 244 |
#[node(from(Object))] |
| 245 |
Interface { |
| 246 |
// Name of the module this is defined in. |
| 247 |
// Taken from `module_path` and normalized |
| 248 |
#[node(from(module_path))] |
| 249 |
module_name: String, |
| 250 |
name: String, |
| 251 |
imp: ObjectImpl, |
| 252 |
}, |
| 253 |
Record { |
| 254 |
#[node(from(module_path))] |
| 255 |
module_name: String, |
| 256 |
name: String, |
| 257 |
}, |
| 258 |
Enum { |
| 259 |
#[node(from(module_path))] |
| 260 |
module_name: String, |
| 261 |
name: String, |
| 262 |
}, |
| 263 |
CallbackInterface { |
| 264 |
#[node(from(module_path))] |
| 265 |
module_name: String, |
| 266 |
name: String, |
| 267 |
}, |
| 268 |
Custom { |
| 269 |
#[node(from(module_path))] |
| 270 |
module_name: String, |
| 271 |
name: String, |
| 272 |
builtin: Box<Type>, |
| 273 |
}, |
| 274 |
} |
| 275 |
|
| 276 |
#[derive(Debug, Clone, Node)] |
| 277 |
pub enum ObjectImpl { |
| 278 |
// A single Rust type |
| 279 |
Struct, |
| 280 |
// A trait that's can be implemented by Rust types |
| 281 |
Trait, |
| 282 |
// A trait + a callback interface -- can be implemented by both Rust and foreign types. |
| 283 |
CallbackTrait, |
| 284 |
} |