| 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 |
//! # Record definitions for a `ComponentInterface`. |
| 6 |
//! |
| 7 |
//! This module converts "dictionary" definitions from UDL into [`Record`] structures |
| 8 |
//! that can be added to a `ComponentInterface`, which are the main way we define structured |
| 9 |
//! data types for a UniFFI Rust Component. A [`Record`] has a fixed set of named fields, |
| 10 |
//! each of a specific type. |
| 11 |
//! |
| 12 |
//! (The terminology mismatch between "dictionary" and "record" is a historical artifact |
| 13 |
//! due to this tool being loosely inspired by WebAssembly Interface Types, which used |
| 14 |
//! the term "record" for this sort of data). |
| 15 |
//! |
| 16 |
//! A declaration in the UDL like this: |
| 17 |
//! |
| 18 |
//! ``` |
| 19 |
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##" |
| 20 |
//! # namespace example {}; |
| 21 |
//! dictionary Example { |
| 22 |
//! string name; |
| 23 |
//! u32 value; |
| 24 |
//! }; |
| 25 |
//! # "##, "crate_name")?; |
| 26 |
//! # Ok::<(), anyhow::Error>(()) |
| 27 |
//! ``` |
| 28 |
//! |
| 29 |
//! Will result in a [`Record`] member with two [`Field`]s being added to the resulting |
| 30 |
//! [`crate::ComponentInterface`]: |
| 31 |
//! |
| 32 |
//! ``` |
| 33 |
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##" |
| 34 |
//! # namespace example {}; |
| 35 |
//! # dictionary Example { |
| 36 |
//! # string name; |
| 37 |
//! # u32 value; |
| 38 |
//! # }; |
| 39 |
//! # "##, "crate_name")?; |
| 40 |
//! let record = ci.get_record_definition("Example").unwrap(); |
| 41 |
//! assert_eq!(record.name(), "Example"); |
| 42 |
//! assert_eq!(record.fields()[0].name(), "name"); |
| 43 |
//! assert_eq!(record.fields()[1].name(), "value"); |
| 44 |
//! # Ok::<(), anyhow::Error>(()) |
| 45 |
//! ``` |
| 46 |
|
| 47 |
use anyhow::Result; |
| 48 |
use uniffi_meta::Checksum; |
| 49 |
|
| 50 |
use super::Literal; |
| 51 |
use super::{AsType, Type, TypeIterator}; |
| 52 |
|
| 53 |
/// Represents a "data class" style object, for passing around complex values. |
| 54 |
/// |
| 55 |
/// In the FFI these are represented as a byte buffer, which one side explicitly |
| 56 |
/// serializes the data into and the other serializes it out of. So I guess they're |
| 57 |
/// kind of like "pass by clone" values. |
| 58 |
#[derive(Debug, Clone, PartialEq, Eq, Checksum)] |
| 59 |
pub struct Record { |
| 60 |
pub(super) name: String, |
| 61 |
pub(super) module_path: String, |
| 62 |
pub(super) remote: bool, |
| 63 |
pub(super) fields: Vec<Field>, |
| 64 |
#[checksum_ignore] |
| 65 |
pub(super) docstring: Option<String>, |
| 66 |
} |
| 67 |
|
| 68 |
impl Record { |
| 69 |
pub fn name(&self) -> &str { |
| 70 |
&self.name |
| 71 |
} |
| 72 |
|
| 73 |
pub fn rename(&mut self, name: String) { |
| 74 |
self.name = name; |
| 75 |
} |
| 76 |
|
| 77 |
pub fn remote(&self) -> bool { |
| 78 |
self.remote |
| 79 |
} |
| 80 |
|
| 81 |
pub fn fields(&self) -> &[Field] { |
| 82 |
&self.fields |
| 83 |
} |
| 84 |
|
| 85 |
pub fn docstring(&self) -> Option<&str> { |
| 86 |
self.docstring.as_deref() |
| 87 |
} |
| 88 |
|
| 89 |
pub fn iter_types(&self) -> TypeIterator<'_> { |
| 90 |
Box::new(self.fields.iter().flat_map(Field::iter_types)) |
| 91 |
} |
| 92 |
|
| 93 |
pub fn has_fields(&self) -> bool { |
| 94 |
!self.fields.is_empty() |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl AsType for Record { |
| 99 |
fn as_type(&self) -> Type { |
| 100 |
Type::Record { |
| 101 |
name: self.name.clone(), |
| 102 |
module_path: self.module_path.clone(), |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl TryFrom<uniffi_meta::RecordMetadata> for Record { |
| 108 |
type Error = anyhow::Error; |
| 109 |
|
| 110 |
fn try_from(meta: uniffi_meta::RecordMetadata) -> Result<Self> { |
| 111 |
Ok(Self { |
| 112 |
name: meta.name, |
| 113 |
module_path: meta.module_path, |
| 114 |
remote: meta.remote, |
| 115 |
fields: meta |
| 116 |
.fields |
| 117 |
.into_iter() |
| 118 |
.map(TryInto::try_into) |
| 119 |
.collect::<Result<_>>()?, |
| 120 |
docstring: meta.docstring.clone(), |
| 121 |
}) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Represents an individual field on a Record. |
| 126 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Checksum)] |
| 127 |
pub struct Field { |
| 128 |
pub(super) name: String, |
| 129 |
pub(super) type_: Type, |
| 130 |
pub(super) default: Option<Literal>, |
| 131 |
#[checksum_ignore] |
| 132 |
pub(super) docstring: Option<String>, |
| 133 |
} |
| 134 |
|
| 135 |
impl Field { |
| 136 |
pub fn name(&self) -> &str { |
| 137 |
&self.name |
| 138 |
} |
| 139 |
|
| 140 |
pub fn rename(&mut self, name: String) { |
| 141 |
self.name = name; |
| 142 |
} |
| 143 |
|
| 144 |
pub fn default_value(&self) -> Option<&Literal> { |
| 145 |
self.default.as_ref() |
| 146 |
} |
| 147 |
|
| 148 |
pub fn docstring(&self) -> Option<&str> { |
| 149 |
self.docstring.as_deref() |
| 150 |
} |
| 151 |
|
| 152 |
pub fn iter_types(&self) -> TypeIterator<'_> { |
| 153 |
self.type_.iter_types() |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
impl AsType for Field { |
| 158 |
fn as_type(&self) -> Type { |
| 159 |
self.type_.clone() |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl TryFrom<uniffi_meta::FieldMetadata> for Field { |
| 164 |
type Error = anyhow::Error; |
| 165 |
|
| 166 |
fn try_from(meta: uniffi_meta::FieldMetadata) -> Result<Self> { |
| 167 |
let name = meta.name; |
| 168 |
let type_ = meta.ty; |
| 169 |
let default = meta.default; |
| 170 |
Ok(Self { |
| 171 |
name, |
| 172 |
type_, |
| 173 |
default, |
| 174 |
docstring: meta.docstring.clone(), |
| 175 |
}) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
#[cfg(test)] |
| 180 |
mod test { |
| 181 |
use super::super::ComponentInterface; |
| 182 |
use super::*; |
| 183 |
use uniffi_meta::Radix; |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn test_multiple_record_types() { |
| 187 |
const UDL: &str = r#" |
| 188 |
namespace test{}; |
| 189 |
dictionary Empty {}; |
| 190 |
dictionary Simple { |
| 191 |
u32 field; |
| 192 |
}; |
| 193 |
dictionary Complex { |
| 194 |
string? key; |
| 195 |
u32 value = 0; |
| 196 |
required boolean spin; |
| 197 |
}; |
| 198 |
"#; |
| 199 |
let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap(); |
| 200 |
assert_eq!(ci.record_definitions().count(), 3); |
| 201 |
|
| 202 |
let record = ci.get_record_definition("Empty").unwrap(); |
| 203 |
assert_eq!(record.name(), "Empty"); |
| 204 |
assert_eq!(record.fields().len(), 0); |
| 205 |
|
| 206 |
let record = ci.get_record_definition("Simple").unwrap(); |
| 207 |
assert_eq!(record.name(), "Simple"); |
| 208 |
assert_eq!(record.fields().len(), 1); |
| 209 |
assert_eq!(record.fields()[0].name(), "field"); |
| 210 |
assert_eq!(record.fields()[0].as_type(), Type::UInt32); |
| 211 |
assert!(record.fields()[0].default_value().is_none()); |
| 212 |
|
| 213 |
let record = ci.get_record_definition("Complex").unwrap(); |
| 214 |
assert_eq!(record.name(), "Complex"); |
| 215 |
assert_eq!(record.fields().len(), 3); |
| 216 |
assert_eq!(record.fields()[0].name(), "key"); |
| 217 |
assert_eq!( |
| 218 |
record.fields()[0].as_type(), |
| 219 |
Type::Optional { |
| 220 |
inner_type: Box::new(Type::String) |
| 221 |
}, |
| 222 |
); |
| 223 |
assert!(record.fields()[0].default_value().is_none()); |
| 224 |
assert_eq!(record.fields()[1].name(), "value"); |
| 225 |
assert_eq!(record.fields()[1].as_type(), Type::UInt32); |
| 226 |
assert!(matches!( |
| 227 |
record.fields()[1].default_value(), |
| 228 |
Some(Literal::UInt(0, Radix::Decimal, Type::UInt32)) |
| 229 |
)); |
| 230 |
assert_eq!(record.fields()[2].name(), "spin"); |
| 231 |
assert_eq!(record.fields()[2].as_type(), Type::Boolean); |
| 232 |
assert!(record.fields()[2].default_value().is_none()); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_that_all_field_types_become_known() { |
| 237 |
const UDL: &str = r#" |
| 238 |
namespace test{}; |
| 239 |
dictionary Testing { |
| 240 |
string? maybe_name; |
| 241 |
u32 value; |
| 242 |
}; |
| 243 |
"#; |
| 244 |
let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap(); |
| 245 |
assert_eq!(ci.record_definitions().count(), 1); |
| 246 |
let record = ci.get_record_definition("Testing").unwrap(); |
| 247 |
assert_eq!(record.fields().len(), 2); |
| 248 |
assert_eq!(record.fields()[0].name(), "maybe_name"); |
| 249 |
assert_eq!(record.fields()[1].name(), "value"); |
| 250 |
|
| 251 |
assert_eq!(ci.iter_local_types().count(), 4); |
| 252 |
assert!(ci.iter_local_types().any(|t| t == &Type::UInt32)); |
| 253 |
assert!(ci.iter_local_types().any(|t| t == &Type::String)); |
| 254 |
assert!(ci.iter_local_types().any(|t| t |
| 255 |
== &Type::Optional { |
| 256 |
inner_type: Box::new(Type::String) |
| 257 |
})); |
| 258 |
assert!(ci |
| 259 |
.iter_local_types() |
| 260 |
.any(|t| matches!(t, Type::Record { name, .. } if name == "Testing"))); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn test_docstring_record() { |
| 265 |
const UDL: &str = r#" |
| 266 |
namespace test{}; |
| 267 |
/// informative docstring |
| 268 |
dictionary Testing { }; |
| 269 |
"#; |
| 270 |
let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap(); |
| 271 |
assert_eq!( |
| 272 |
ci.get_record_definition("Testing") |
| 273 |
.unwrap() |
| 274 |
.docstring() |
| 275 |
.unwrap(), |
| 276 |
"informative docstring" |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn test_docstring_record_field() { |
| 282 |
const UDL: &str = r#" |
| 283 |
namespace test{}; |
| 284 |
dictionary Testing { |
| 285 |
/// informative docstring |
| 286 |
i32 testing; |
| 287 |
}; |
| 288 |
"#; |
| 289 |
let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap(); |
| 290 |
assert_eq!( |
| 291 |
ci.get_record_definition("Testing").unwrap().fields()[0] |
| 292 |
.docstring() |
| 293 |
.unwrap(), |
| 294 |
"informative docstring" |
| 295 |
); |
| 296 |
} |
| 297 |
} |