| 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 |
//! # Attribute definitions for a `InterfaceCollector`. |
| 6 |
//! |
| 7 |
//! This module provides some conveniences for working with attribute definitions |
| 8 |
//! from WebIDL. When encountering a weedle `ExtendedAttribute` node, use `TryFrom` |
| 9 |
//! to convert it into an [`Attribute`] representing one of the attributes that we |
| 10 |
//! support. You can also use the [`parse_attributes`] function to parse an |
| 11 |
//! `ExtendedAttributeList` into a vec of same. |
| 12 |
//! |
| 13 |
//! We only support a small number of attributes, so it's manageable to have them |
| 14 |
//! all handled by a single abstraction. This might need to be refactored in future |
| 15 |
//! if we grow significantly more complicated attribute handling. |
| 16 |
|
| 17 |
use anyhow::{bail, Result}; |
| 18 |
use uniffi_meta::{Checksum, ObjectImpl}; |
| 19 |
|
| 20 |
/// Represents an attribute parsed from UDL, like `[ByRef]` or `[Throws]`. |
| 21 |
/// |
| 22 |
/// This is a convenience enum for parsing UDL attributes and erroring out if we encounter |
| 23 |
/// any unsupported ones. These don't convert directly into parts of a `InterfaceCollector`, but |
| 24 |
/// may influence the properties of things like functions and arguments. |
| 25 |
#[derive(Debug, Clone, Checksum)] |
| 26 |
pub(super) enum Attribute { |
| 27 |
ByRef, |
| 28 |
Enum, |
| 29 |
Error, |
| 30 |
Name(String), |
| 31 |
SelfType(SelfType), |
| 32 |
Throws(String), |
| 33 |
Traits(Vec<String>), |
| 34 |
// `[External="crate_name"]` - We can `use crate_name::...` for the type. |
| 35 |
External { crate_name: String }, |
| 36 |
Remote, |
| 37 |
// Custom type on the scaffolding side |
| 38 |
Custom { crate_name: Option<String> }, |
| 39 |
// The interface described is implemented as a trait. |
| 40 |
Trait, |
| 41 |
// Modifies `Trait` to enable foreign implementations (callback interfaces) |
| 42 |
WithForeign, |
| 43 |
Async, |
| 44 |
NonExhaustive, |
| 45 |
} |
| 46 |
|
| 47 |
impl Attribute { |
| 48 |
pub fn is_error(&self) -> bool { |
| 49 |
matches!(self, Attribute::Error) |
| 50 |
} |
| 51 |
pub fn is_enum(&self) -> bool { |
| 52 |
matches!(self, Attribute::Enum) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Convert a weedle `ExtendedAttribute` into an `Attribute` for a `InterfaceCollector` member, |
| 57 |
/// or error out if the attribute is not supported. |
| 58 |
impl TryFrom<&weedle::attribute::ExtendedAttribute<'_>> for Attribute { |
| 59 |
type Error = anyhow::Error; |
| 60 |
fn try_from( |
| 61 |
weedle_attribute: &weedle::attribute::ExtendedAttribute<'_>, |
| 62 |
) -> Result<Self, anyhow::Error> { |
| 63 |
match weedle_attribute { |
| 64 |
// Matches plain named attributes like "[ByRef"]. |
| 65 |
weedle::attribute::ExtendedAttribute::NoArgs(attr) => match (attr.0).0 { |
| 66 |
"ByRef" => Ok(Attribute::ByRef), |
| 67 |
"Enum" => Ok(Attribute::Enum), |
| 68 |
"Error" => Ok(Attribute::Error), |
| 69 |
"Custom" => Ok(Attribute::Custom { crate_name: None }), |
| 70 |
"Trait" => Ok(Attribute::Trait), |
| 71 |
"WithForeign" => Ok(Attribute::WithForeign), |
| 72 |
"Async" => Ok(Attribute::Async), |
| 73 |
"NonExhaustive" => Ok(Attribute::NonExhaustive), |
| 74 |
"Remote" => Ok(Attribute::Remote), |
| 75 |
_ => anyhow::bail!("ExtendedAttributeNoArgs not supported: {:?}", (attr.0).0), |
| 76 |
}, |
| 77 |
// Matches assignment-style attributes like ["Throws=Error"] |
| 78 |
weedle::attribute::ExtendedAttribute::Ident(identity) => { |
| 79 |
match identity.lhs_identifier.0 { |
| 80 |
"Name" => Ok(Attribute::Name(name_from_id_or_string(&identity.rhs))), |
| 81 |
"Throws" => Ok(Attribute::Throws(name_from_id_or_string(&identity.rhs))), |
| 82 |
"Self" => Ok(Attribute::SelfType(SelfType::try_from(&identity.rhs)?)), |
| 83 |
"External" => Ok(Attribute::External { |
| 84 |
crate_name: name_from_id_or_string(&identity.rhs), |
| 85 |
}), |
| 86 |
"Custom" => Ok(Attribute::Custom { |
| 87 |
crate_name: Some(name_from_id_or_string(&identity.rhs)), |
| 88 |
}), |
| 89 |
_ => anyhow::bail!( |
| 90 |
"Attribute identity Identifier not supported: {:?}", |
| 91 |
identity.lhs_identifier.0 |
| 92 |
), |
| 93 |
} |
| 94 |
} |
| 95 |
weedle::attribute::ExtendedAttribute::IdentList(attr_list) => { |
| 96 |
match attr_list.identifier.0 { |
| 97 |
"Traits" => Ok(Attribute::Traits( |
| 98 |
attr_list |
| 99 |
.list |
| 100 |
.body |
| 101 |
.list |
| 102 |
.iter() |
| 103 |
.map(|i| i.0.to_string()) |
| 104 |
.collect(), |
| 105 |
)), |
| 106 |
_ => anyhow::bail!( |
| 107 |
"Attribute identity list not supported: {:?}", |
| 108 |
attr_list.identifier.0 |
| 109 |
), |
| 110 |
} |
| 111 |
} |
| 112 |
_ => anyhow::bail!("Attribute not supported: {:?}", weedle_attribute), |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
fn name_from_id_or_string(nm: &weedle::attribute::IdentifierOrString<'_>) -> String { |
| 118 |
match nm { |
| 119 |
weedle::attribute::IdentifierOrString::Identifier(identifier) => identifier.0.to_string(), |
| 120 |
weedle::attribute::IdentifierOrString::String(str_lit) => str_lit.0.to_string(), |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/// Parse a weedle `ExtendedAttributeList` into a list of `Attribute`s, |
| 125 |
/// erroring out on duplicates. |
| 126 |
fn parse_attributes<F>( |
| 127 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 128 |
validator: F, |
| 129 |
) -> Result<Vec<Attribute>> |
| 130 |
where |
| 131 |
F: Fn(&Attribute) -> Result<()>, |
| 132 |
{ |
| 133 |
let attrs = &weedle_attributes.body.list; |
| 134 |
|
| 135 |
let mut hash_set = std::collections::HashSet::new(); |
| 136 |
for attr in attrs { |
| 137 |
if !hash_set.insert(attr) { |
| 138 |
anyhow::bail!("Duplicated ExtendedAttribute: {:?}", attr); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
let attrs = attrs |
| 143 |
.iter() |
| 144 |
.map(Attribute::try_from) |
| 145 |
.collect::<Result<Vec<_>, _>>()?; |
| 146 |
|
| 147 |
for attr in &attrs { |
| 148 |
validator(attr)?; |
| 149 |
} |
| 150 |
|
| 151 |
Ok(attrs) |
| 152 |
} |
| 153 |
|
| 154 |
/// Attributes that can be attached to an `dictionary` definition in the UDL. |
| 155 |
#[derive(Debug, Clone, Checksum, Default)] |
| 156 |
pub(super) struct DictionaryAttributes(Vec<Attribute>); |
| 157 |
|
| 158 |
impl DictionaryAttributes { |
| 159 |
pub fn contains_remote(&self) -> bool { |
| 160 |
self.0.iter().any(|attr| matches!(attr, Attribute::Remote)) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for DictionaryAttributes { |
| 165 |
type Error = anyhow::Error; |
| 166 |
fn try_from( |
| 167 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 168 |
) -> Result<Self, Self::Error> { |
| 169 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 170 |
Attribute::Remote => Ok(()), |
| 171 |
_ => bail!(format!("{attr:?} not supported for dictionaries")), |
| 172 |
})?; |
| 173 |
Ok(Self(attrs)) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl<T: TryInto<DictionaryAttributes, Error = anyhow::Error>> TryFrom<Option<T>> |
| 178 |
for DictionaryAttributes |
| 179 |
{ |
| 180 |
type Error = anyhow::Error; |
| 181 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 182 |
match value { |
| 183 |
None => Ok(Default::default()), |
| 184 |
Some(v) => v.try_into(), |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Attributes that can be attached to an `enum` definition in the UDL. |
| 190 |
#[derive(Debug, Clone, Checksum, Default)] |
| 191 |
pub(super) struct EnumAttributes(Vec<Attribute>); |
| 192 |
|
| 193 |
impl EnumAttributes { |
| 194 |
pub fn contains_error_attr(&self) -> bool { |
| 195 |
self.0.iter().any(|attr| attr.is_error()) |
| 196 |
} |
| 197 |
|
| 198 |
pub fn contains_non_exhaustive_attr(&self) -> bool { |
| 199 |
self.0 |
| 200 |
.iter() |
| 201 |
.any(|attr| matches!(attr, Attribute::NonExhaustive)) |
| 202 |
} |
| 203 |
|
| 204 |
pub fn contains_remote(&self) -> bool { |
| 205 |
self.0.iter().any(|attr| matches!(attr, Attribute::Remote)) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for EnumAttributes { |
| 210 |
type Error = anyhow::Error; |
| 211 |
fn try_from( |
| 212 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 213 |
) -> Result<Self, Self::Error> { |
| 214 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 215 |
Attribute::Error => Ok(()), |
| 216 |
Attribute::NonExhaustive => Ok(()), |
| 217 |
Attribute::Remote => Ok(()), |
| 218 |
// Allow `[Enum]`, since we may be parsing an attribute list from an interface with the |
| 219 |
// `[Enum]` attribute. |
| 220 |
Attribute::Enum => Ok(()), |
| 221 |
_ => bail!(format!("{attr:?} not supported for enums")), |
| 222 |
})?; |
| 223 |
Ok(Self(attrs)) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
impl<T: TryInto<EnumAttributes, Error = anyhow::Error>> TryFrom<Option<T>> for EnumAttributes { |
| 228 |
type Error = anyhow::Error; |
| 229 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 230 |
match value { |
| 231 |
None => Ok(Default::default()), |
| 232 |
Some(v) => v.try_into(), |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/// Represents UDL attributes that might appear on a function. |
| 238 |
/// |
| 239 |
/// This supports: |
| 240 |
/// * `[Throws=ErrorName]` attribute for functions that can produce an error. |
| 241 |
/// * `[Async] for async functions |
| 242 |
#[derive(Debug, Clone, Checksum, Default)] |
| 243 |
pub(super) struct FunctionAttributes(Vec<Attribute>); |
| 244 |
|
| 245 |
impl FunctionAttributes { |
| 246 |
pub(super) fn get_throws_err(&self) -> Option<&str> { |
| 247 |
self.0.iter().find_map(|attr| match attr { |
| 248 |
// This will hopefully return a helpful compilation error |
| 249 |
// if the error is not defined. |
| 250 |
Attribute::Throws(inner) => Some(inner.as_ref()), |
| 251 |
_ => None, |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
pub(super) fn is_async(&self) -> bool { |
| 256 |
self.0.iter().any(|attr| matches!(attr, Attribute::Async)) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
impl FromIterator<Attribute> for FunctionAttributes { |
| 261 |
fn from_iter<T: IntoIterator<Item = Attribute>>(iter: T) -> Self { |
| 262 |
Self(Vec::from_iter(iter)) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for FunctionAttributes { |
| 267 |
type Error = anyhow::Error; |
| 268 |
fn try_from( |
| 269 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 270 |
) -> Result<Self, Self::Error> { |
| 271 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 272 |
Attribute::Throws(_) | Attribute::Async => Ok(()), |
| 273 |
_ => bail!(format!("{attr:?} not supported for functions")), |
| 274 |
})?; |
| 275 |
Ok(Self(attrs)) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
impl<T: TryInto<FunctionAttributes, Error = anyhow::Error>> TryFrom<Option<T>> |
| 280 |
for FunctionAttributes |
| 281 |
{ |
| 282 |
type Error = anyhow::Error; |
| 283 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 284 |
match value { |
| 285 |
None => Ok(Default::default()), |
| 286 |
Some(v) => v.try_into(), |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/// Represents UDL attributes that might appear on a function argument. |
| 292 |
/// |
| 293 |
/// This supports the `[ByRef]` attribute for arguments that should be passed |
| 294 |
/// by reference in the generated Rust scaffolding. |
| 295 |
#[derive(Debug, Clone, Checksum, Default)] |
| 296 |
pub(super) struct ArgumentAttributes(Vec<Attribute>); |
| 297 |
|
| 298 |
impl ArgumentAttributes { |
| 299 |
pub fn by_ref(&self) -> bool { |
| 300 |
self.0.iter().any(|attr| matches!(attr, Attribute::ByRef)) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for ArgumentAttributes { |
| 305 |
type Error = anyhow::Error; |
| 306 |
fn try_from( |
| 307 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 308 |
) -> Result<Self, Self::Error> { |
| 309 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 310 |
Attribute::ByRef => Ok(()), |
| 311 |
_ => bail!(format!("{attr:?} not supported for arguments")), |
| 312 |
})?; |
| 313 |
Ok(Self(attrs)) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
impl<T: TryInto<ArgumentAttributes, Error = anyhow::Error>> TryFrom<Option<T>> |
| 318 |
for ArgumentAttributes |
| 319 |
{ |
| 320 |
type Error = anyhow::Error; |
| 321 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 322 |
match value { |
| 323 |
None => Ok(Default::default()), |
| 324 |
Some(v) => v.try_into(), |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/// Represents UDL attributes that might appear on an `interface` definition. |
| 330 |
#[derive(Debug, Clone, Checksum, Default)] |
| 331 |
pub(super) struct InterfaceAttributes(Vec<Attribute>); |
| 332 |
|
| 333 |
impl InterfaceAttributes { |
| 334 |
pub fn contains_enum_attr(&self) -> bool { |
| 335 |
self.0.iter().any(|attr| attr.is_enum()) |
| 336 |
} |
| 337 |
|
| 338 |
pub fn contains_error_attr(&self) -> bool { |
| 339 |
self.0.iter().any(|attr| attr.is_error()) |
| 340 |
} |
| 341 |
|
| 342 |
pub fn contains_trait(&self) -> bool { |
| 343 |
self.0.iter().any(|attr| matches!(attr, Attribute::Trait)) |
| 344 |
} |
| 345 |
|
| 346 |
pub fn contains_remote(&self) -> bool { |
| 347 |
self.0.iter().any(|attr| matches!(attr, Attribute::Remote)) |
| 348 |
} |
| 349 |
|
| 350 |
pub fn contains_with_foreign(&self) -> bool { |
| 351 |
self.0 |
| 352 |
.iter() |
| 353 |
.any(|attr| matches!(attr, Attribute::WithForeign)) |
| 354 |
} |
| 355 |
|
| 356 |
pub fn object_impl(&self) -> Result<ObjectImpl> { |
| 357 |
Ok( |
| 358 |
match (self.contains_trait(), self.contains_with_foreign()) { |
| 359 |
(true, true) => ObjectImpl::CallbackTrait, |
| 360 |
(true, false) => ObjectImpl::Trait, |
| 361 |
(false, false) => ObjectImpl::Struct, |
| 362 |
(false, true) => bail!("WithForeign can't be specified without Trait"), |
| 363 |
}, |
| 364 |
) |
| 365 |
} |
| 366 |
pub fn get_traits(&self) -> Vec<String> { |
| 367 |
self.0 |
| 368 |
.iter() |
| 369 |
.find_map(|attr| match attr { |
| 370 |
Attribute::Traits(inner) => Some(inner.clone()), |
| 371 |
_ => None, |
| 372 |
}) |
| 373 |
.unwrap_or_default() |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for InterfaceAttributes { |
| 378 |
type Error = anyhow::Error; |
| 379 |
fn try_from( |
| 380 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 381 |
) -> Result<Self, Self::Error> { |
| 382 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 383 |
Attribute::Enum => Ok(()), |
| 384 |
Attribute::Error => Ok(()), |
| 385 |
Attribute::Trait => Ok(()), |
| 386 |
Attribute::WithForeign => Ok(()), |
| 387 |
Attribute::Traits(_) => Ok(()), |
| 388 |
Attribute::Remote => Ok(()), |
| 389 |
_ => bail!(format!("{attr:?} not supported for interface definition")), |
| 390 |
})?; |
| 391 |
if attrs.iter().any(|a| matches!(a, Attribute::Enum)) && attrs.len() != 1 { |
| 392 |
// If `[Enum]` is specified it must be the only attribute. |
| 393 |
bail!("conflicting attributes on interface definition"); |
| 394 |
} |
| 395 |
Ok(Self(attrs)) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
impl<T: TryInto<InterfaceAttributes, Error = anyhow::Error>> TryFrom<Option<T>> |
| 400 |
for InterfaceAttributes |
| 401 |
{ |
| 402 |
type Error = anyhow::Error; |
| 403 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 404 |
match value { |
| 405 |
None => Ok(Default::default()), |
| 406 |
Some(v) => v.try_into(), |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/// Represents UDL attributes that might appear on a constructor. |
| 412 |
/// |
| 413 |
/// This supports the `[Throws=ErrorName]` attribute for constructors that can produce |
| 414 |
/// an error, and the `[Name=MethodName]` for non-default constructors. |
| 415 |
#[derive(Debug, Clone, Checksum, Default)] |
| 416 |
pub(super) struct ConstructorAttributes(Vec<Attribute>); |
| 417 |
|
| 418 |
impl FromIterator<Attribute> for ConstructorAttributes { |
| 419 |
fn from_iter<T: IntoIterator<Item = Attribute>>(iter: T) -> Self { |
| 420 |
Self(Vec::from_iter(iter)) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
impl ConstructorAttributes { |
| 425 |
pub(super) fn get_throws_err(&self) -> Option<&str> { |
| 426 |
self.0.iter().find_map(|attr| match attr { |
| 427 |
// This will hopefully return a helpful compilation error |
| 428 |
// if the error is not defined. |
| 429 |
Attribute::Throws(inner) => Some(inner.as_ref()), |
| 430 |
_ => None, |
| 431 |
}) |
| 432 |
} |
| 433 |
|
| 434 |
pub(super) fn get_name(&self) -> Option<&str> { |
| 435 |
self.0.iter().find_map(|attr| match attr { |
| 436 |
Attribute::Name(inner) => Some(inner.as_ref()), |
| 437 |
_ => None, |
| 438 |
}) |
| 439 |
} |
| 440 |
|
| 441 |
pub(super) fn is_async(&self) -> bool { |
| 442 |
self.0.iter().any(|attr| matches!(attr, Attribute::Async)) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for ConstructorAttributes { |
| 447 |
type Error = anyhow::Error; |
| 448 |
fn try_from( |
| 449 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 450 |
) -> Result<Self, Self::Error> { |
| 451 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 452 |
Attribute::Throws(_) => Ok(()), |
| 453 |
Attribute::Name(_) => Ok(()), |
| 454 |
Attribute::Async => Ok(()), |
| 455 |
_ => bail!(format!("{attr:?} not supported for constructors")), |
| 456 |
})?; |
| 457 |
Ok(Self(attrs)) |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/// Represents UDL attributes that might appear on a method. |
| 462 |
/// |
| 463 |
/// This supports the `[Throws=ErrorName]` attribute for methods that can produce |
| 464 |
/// an error, and the `[Self=ByArc]` attribute for methods that take `Arc<Self>` as receiver. |
| 465 |
#[derive(Debug, Clone, Checksum, Default)] |
| 466 |
pub(super) struct MethodAttributes(Vec<Attribute>); |
| 467 |
|
| 468 |
impl MethodAttributes { |
| 469 |
pub(super) fn get_throws_err(&self) -> Option<&str> { |
| 470 |
self.0.iter().find_map(|attr| match attr { |
| 471 |
// This will hopefully return a helpful compilation error |
| 472 |
// if the error is not defined. |
| 473 |
Attribute::Throws(inner) => Some(inner.as_ref()), |
| 474 |
_ => None, |
| 475 |
}) |
| 476 |
} |
| 477 |
|
| 478 |
pub(super) fn is_async(&self) -> bool { |
| 479 |
self.0.iter().any(|attr| matches!(attr, Attribute::Async)) |
| 480 |
} |
| 481 |
|
| 482 |
pub(super) fn get_self_by_arc(&self) -> bool { |
| 483 |
self.0 |
| 484 |
.iter() |
| 485 |
.any(|attr| matches!(attr, Attribute::SelfType(SelfType::ByArc))) |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
impl FromIterator<Attribute> for MethodAttributes { |
| 490 |
fn from_iter<T: IntoIterator<Item = Attribute>>(iter: T) -> Self { |
| 491 |
Self(Vec::from_iter(iter)) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for MethodAttributes { |
| 496 |
type Error = anyhow::Error; |
| 497 |
fn try_from( |
| 498 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 499 |
) -> Result<Self, Self::Error> { |
| 500 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 501 |
Attribute::SelfType(_) | Attribute::Throws(_) | Attribute::Async => Ok(()), |
| 502 |
_ => bail!(format!("{attr:?} not supported for methods")), |
| 503 |
})?; |
| 504 |
Ok(Self(attrs)) |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
impl<T: TryInto<MethodAttributes, Error = anyhow::Error>> TryFrom<Option<T>> for MethodAttributes { |
| 509 |
type Error = anyhow::Error; |
| 510 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 511 |
match value { |
| 512 |
None => Ok(Default::default()), |
| 513 |
Some(v) => v.try_into(), |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
/// Represents the different possible types of method call receiver. |
| 519 |
/// |
| 520 |
/// Actually we only support one of these right now, `[Self=ByArc]`. |
| 521 |
/// We might add more in future, e.g. a `[Self=ByRef]` if there are cases |
| 522 |
/// where we need to force the receiver to be taken by reference. |
| 523 |
#[derive(Debug, Clone, Checksum)] |
| 524 |
pub(super) enum SelfType { |
| 525 |
ByArc, // Method receiver is `Arc<Self>`. |
| 526 |
} |
| 527 |
|
| 528 |
impl TryFrom<&weedle::attribute::IdentifierOrString<'_>> for SelfType { |
| 529 |
type Error = anyhow::Error; |
| 530 |
fn try_from(nm: &weedle::attribute::IdentifierOrString<'_>) -> Result<Self, Self::Error> { |
| 531 |
Ok(match nm { |
| 532 |
weedle::attribute::IdentifierOrString::Identifier(identifier) => match identifier.0 { |
| 533 |
"ByArc" => SelfType::ByArc, |
| 534 |
_ => bail!("Unsupported Self Type: {:?}", identifier.0), |
| 535 |
}, |
| 536 |
weedle::attribute::IdentifierOrString::String(_) => { |
| 537 |
bail!("Unsupported Self Type: {:?}", nm) |
| 538 |
} |
| 539 |
}) |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
/// Represents UDL attributes that might appear on a typedef |
| 544 |
/// |
| 545 |
/// This supports the `[External="crate_name"]` and `[Custom]` attributes for types. |
| 546 |
#[derive(Debug, Clone, Checksum, Default)] |
| 547 |
pub(super) struct TypedefAttributes(Vec<Attribute>); |
| 548 |
|
| 549 |
impl TypedefAttributes { |
| 550 |
pub(super) fn get_crate_name(&self) -> Option<String> { |
| 551 |
self.0.iter().find_map(|attr| match attr { |
| 552 |
Attribute::External { crate_name, .. } => Some(crate_name.clone()), |
| 553 |
Attribute::Custom { |
| 554 |
crate_name: Some(crate_name), |
| 555 |
.. |
| 556 |
} => Some(crate_name.clone()), |
| 557 |
_ => None, |
| 558 |
}) |
| 559 |
} |
| 560 |
|
| 561 |
pub(super) fn is_custom(&self) -> bool { |
| 562 |
self.0 |
| 563 |
.iter() |
| 564 |
.any(|attr| matches!(attr, Attribute::Custom { .. })) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for TypedefAttributes { |
| 569 |
type Error = anyhow::Error; |
| 570 |
fn try_from( |
| 571 |
weedle_attributes: &weedle::attribute::ExtendedAttributeList<'_>, |
| 572 |
) -> Result<Self, Self::Error> { |
| 573 |
let attrs = parse_attributes(weedle_attributes, |attr| match attr { |
| 574 |
Attribute::External { .. } | Attribute::Custom { .. } => Ok(()), |
| 575 |
_ => bail!(format!("{attr:?} not supported for typedefs")), |
| 576 |
})?; |
| 577 |
if attrs.len() > 1 { |
| 578 |
bail!("Can't be [Custom] and [External]"); |
| 579 |
} |
| 580 |
Ok(Self(attrs)) |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
impl<T: TryInto<TypedefAttributes, Error = anyhow::Error>> TryFrom<Option<T>> |
| 585 |
for TypedefAttributes |
| 586 |
{ |
| 587 |
type Error = anyhow::Error; |
| 588 |
fn try_from(value: Option<T>) -> Result<Self, Self::Error> { |
| 589 |
match value { |
| 590 |
None => Ok(Default::default()), |
| 591 |
Some(v) => v.try_into(), |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
#[cfg(test)] |
| 597 |
mod test { |
| 598 |
use super::*; |
| 599 |
use weedle::Parse; |
| 600 |
|
| 601 |
#[test] |
| 602 |
fn test_byref() -> Result<()> { |
| 603 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("ByRef").unwrap(); |
| 604 |
let attr = Attribute::try_from(&node)?; |
| 605 |
assert!(matches!(attr, Attribute::ByRef)); |
| 606 |
Ok(()) |
| 607 |
} |
| 608 |
|
| 609 |
#[test] |
| 610 |
fn test_enum() -> Result<()> { |
| 611 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Enum").unwrap(); |
| 612 |
let attr = Attribute::try_from(&node)?; |
| 613 |
assert!(matches!(attr, Attribute::Enum)); |
| 614 |
assert!(attr.is_enum()); |
| 615 |
Ok(()) |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn test_error() -> Result<()> { |
| 620 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Error").unwrap(); |
| 621 |
let attr = Attribute::try_from(&node)?; |
| 622 |
assert!(matches!(attr, Attribute::Error)); |
| 623 |
assert!(attr.is_error()); |
| 624 |
Ok(()) |
| 625 |
} |
| 626 |
|
| 627 |
#[test] |
| 628 |
fn test_name() -> Result<()> { |
| 629 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Name=Value").unwrap(); |
| 630 |
let attr = Attribute::try_from(&node)?; |
| 631 |
assert!(matches!(attr, Attribute::Name(nm) if nm == "Value")); |
| 632 |
|
| 633 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Name").unwrap(); |
| 634 |
let err = Attribute::try_from(&node).unwrap_err(); |
| 635 |
assert_eq!( |
| 636 |
err.to_string(), |
| 637 |
"ExtendedAttributeNoArgs not supported: \"Name\"" |
| 638 |
); |
| 639 |
|
| 640 |
Ok(()) |
| 641 |
} |
| 642 |
|
| 643 |
#[test] |
| 644 |
fn test_selftype() -> Result<()> { |
| 645 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Self=ByArc").unwrap(); |
| 646 |
let attr = Attribute::try_from(&node)?; |
| 647 |
assert!(matches!(attr, Attribute::SelfType(SelfType::ByArc))); |
| 648 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Self=ByMistake").unwrap(); |
| 649 |
let err = Attribute::try_from(&node).unwrap_err(); |
| 650 |
assert_eq!(err.to_string(), "Unsupported Self Type: \"ByMistake\""); |
| 651 |
Ok(()) |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn test_trait() -> Result<()> { |
| 656 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Trait").unwrap(); |
| 657 |
let attr = Attribute::try_from(&node)?; |
| 658 |
assert!(matches!(attr, Attribute::Trait)); |
| 659 |
Ok(()) |
| 660 |
} |
| 661 |
|
| 662 |
#[test] |
| 663 |
fn test_throws() -> Result<()> { |
| 664 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Throws=Name").unwrap(); |
| 665 |
let attr = Attribute::try_from(&node)?; |
| 666 |
assert!(matches!(attr, Attribute::Throws(nm) if nm == "Name")); |
| 667 |
|
| 668 |
let (_, node) = weedle::attribute::ExtendedAttribute::parse("Throws").unwrap(); |
| 669 |
let err = Attribute::try_from(&node).unwrap_err(); |
| 670 |
assert_eq!( |
| 671 |
err.to_string(), |
| 672 |
"ExtendedAttributeNoArgs not supported: \"Throws\"" |
| 673 |
); |
| 674 |
|
| 675 |
Ok(()) |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_unsupported() { |
| 680 |
let (_, node) = |
| 681 |
weedle::attribute::ExtendedAttribute::parse("UnsupportedAttribute").unwrap(); |
| 682 |
let err = Attribute::try_from(&node).unwrap_err(); |
| 683 |
assert_eq!( |
| 684 |
err.to_string(), |
| 685 |
"ExtendedAttributeNoArgs not supported: \"UnsupportedAttribute\"" |
| 686 |
); |
| 687 |
|
| 688 |
let (_, node) = |
| 689 |
weedle::attribute::ExtendedAttribute::parse("Unsupported=Attribute").unwrap(); |
| 690 |
let err = Attribute::try_from(&node).unwrap_err(); |
| 691 |
assert_eq!( |
| 692 |
err.to_string(), |
| 693 |
"Attribute identity Identifier not supported: \"Unsupported\"" |
| 694 |
); |
| 695 |
} |
| 696 |
|
| 697 |
#[test] |
| 698 |
fn test_other_attributes_not_supported_for_enums() { |
| 699 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Error, ByRef]").unwrap(); |
| 700 |
let err = EnumAttributes::try_from(&node).unwrap_err(); |
| 701 |
assert_eq!(err.to_string(), "ByRef not supported for enums"); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn test_function_attributes() { |
| 706 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Throws=Error]").unwrap(); |
| 707 |
let attrs = FunctionAttributes::try_from(&node).unwrap(); |
| 708 |
assert!(matches!(attrs.get_throws_err(), Some("Error"))); |
| 709 |
assert!(!attrs.is_async()); |
| 710 |
|
| 711 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[]").unwrap(); |
| 712 |
let attrs = FunctionAttributes::try_from(&node).unwrap(); |
| 713 |
assert!(attrs.get_throws_err().is_none()); |
| 714 |
assert!(!attrs.is_async()); |
| 715 |
|
| 716 |
let (_, node) = |
| 717 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, Async]").unwrap(); |
| 718 |
let attrs = FunctionAttributes::try_from(&node).unwrap(); |
| 719 |
assert!(matches!(attrs.get_throws_err(), Some("Error"))); |
| 720 |
assert!(attrs.is_async()); |
| 721 |
} |
| 722 |
|
| 723 |
#[test] |
| 724 |
fn test_other_attributes_not_supported_for_functions() { |
| 725 |
let (_, node) = |
| 726 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, ByRef]").unwrap(); |
| 727 |
let err = FunctionAttributes::try_from(&node).unwrap_err(); |
| 728 |
assert_eq!(err.to_string(), "ByRef not supported for functions"); |
| 729 |
|
| 730 |
let (_, node) = |
| 731 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, Self=ByArc]").unwrap(); |
| 732 |
let err = FunctionAttributes::try_from(&node).unwrap_err(); |
| 733 |
assert_eq!( |
| 734 |
err.to_string(), |
| 735 |
"SelfType(ByArc) not supported for functions" |
| 736 |
); |
| 737 |
} |
| 738 |
|
| 739 |
#[test] |
| 740 |
fn test_method_attributes() { |
| 741 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Throws=Error]").unwrap(); |
| 742 |
let attrs = MethodAttributes::try_from(&node).unwrap(); |
| 743 |
assert!(!attrs.get_self_by_arc()); |
| 744 |
assert!(matches!(attrs.get_throws_err(), Some("Error"))); |
| 745 |
assert!(!attrs.is_async()); |
| 746 |
|
| 747 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[]").unwrap(); |
| 748 |
let attrs = MethodAttributes::try_from(&node).unwrap(); |
| 749 |
assert!(!attrs.get_self_by_arc()); |
| 750 |
assert!(attrs.get_throws_err().is_none()); |
| 751 |
assert!(!attrs.is_async()); |
| 752 |
|
| 753 |
let (_, node) = |
| 754 |
weedle::attribute::ExtendedAttributeList::parse("[Self=ByArc, Throws=Error]").unwrap(); |
| 755 |
let attrs = MethodAttributes::try_from(&node).unwrap(); |
| 756 |
assert!(attrs.get_self_by_arc()); |
| 757 |
assert!(attrs.get_throws_err().is_some()); |
| 758 |
assert!(!attrs.is_async()); |
| 759 |
|
| 760 |
let (_, node) = |
| 761 |
weedle::attribute::ExtendedAttributeList::parse("[Self=ByArc, Throws=Error, Async]") |
| 762 |
.unwrap(); |
| 763 |
let attrs = MethodAttributes::try_from(&node).unwrap(); |
| 764 |
assert!(attrs.get_self_by_arc()); |
| 765 |
assert!(attrs.get_throws_err().is_some()); |
| 766 |
assert!(attrs.is_async()); |
| 767 |
|
| 768 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Self=ByArc]").unwrap(); |
| 769 |
let attrs = MethodAttributes::try_from(&node).unwrap(); |
| 770 |
assert!(attrs.get_self_by_arc()); |
| 771 |
assert!(attrs.get_throws_err().is_none()); |
| 772 |
assert!(!attrs.is_async()); |
| 773 |
} |
| 774 |
|
| 775 |
#[test] |
| 776 |
fn test_constructor_attributes() { |
| 777 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Throws=Error]").unwrap(); |
| 778 |
let attrs = ConstructorAttributes::try_from(&node).unwrap(); |
| 779 |
assert!(matches!(attrs.get_throws_err(), Some("Error"))); |
| 780 |
assert!(attrs.get_name().is_none()); |
| 781 |
|
| 782 |
let (_, node) = |
| 783 |
weedle::attribute::ExtendedAttributeList::parse("[Name=MyFactory]").unwrap(); |
| 784 |
let attrs = ConstructorAttributes::try_from(&node).unwrap(); |
| 785 |
assert!(attrs.get_throws_err().is_none()); |
| 786 |
assert!(matches!(attrs.get_name(), Some("MyFactory"))); |
| 787 |
|
| 788 |
let (_, node) = |
| 789 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, Name=MyFactory]") |
| 790 |
.unwrap(); |
| 791 |
let attrs = ConstructorAttributes::try_from(&node).unwrap(); |
| 792 |
assert!(matches!(attrs.get_throws_err(), Some("Error"))); |
| 793 |
assert!(matches!(attrs.get_name(), Some("MyFactory"))); |
| 794 |
assert!(!attrs.is_async()); |
| 795 |
|
| 796 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Async]").unwrap(); |
| 797 |
let attrs = ConstructorAttributes::try_from(&node).unwrap(); |
| 798 |
assert!(attrs.is_async()); |
| 799 |
} |
| 800 |
|
| 801 |
#[test] |
| 802 |
fn test_other_attributes_not_supported_for_constructors() { |
| 803 |
let (_, node) = |
| 804 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, ByRef]").unwrap(); |
| 805 |
let err = ConstructorAttributes::try_from(&node).unwrap_err(); |
| 806 |
assert_eq!(err.to_string(), "ByRef not supported for constructors"); |
| 807 |
|
| 808 |
let (_, node) = |
| 809 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, Self=ByArc]").unwrap(); |
| 810 |
let err = ConstructorAttributes::try_from(&node).unwrap_err(); |
| 811 |
assert_eq!( |
| 812 |
err.to_string(), |
| 813 |
"SelfType(ByArc) not supported for constructors" |
| 814 |
); |
| 815 |
} |
| 816 |
|
| 817 |
#[test] |
| 818 |
fn test_byref_attribute() { |
| 819 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[ByRef]").unwrap(); |
| 820 |
let attrs = ArgumentAttributes::try_from(&node).unwrap(); |
| 821 |
assert!(attrs.by_ref()); |
| 822 |
|
| 823 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[]").unwrap(); |
| 824 |
let attrs = ArgumentAttributes::try_from(&node).unwrap(); |
| 825 |
assert!(!attrs.by_ref()); |
| 826 |
} |
| 827 |
|
| 828 |
#[test] |
| 829 |
fn test_other_attributes_not_supported_for_arguments() { |
| 830 |
let (_, node) = |
| 831 |
weedle::attribute::ExtendedAttributeList::parse("[Throws=Error, ByRef]").unwrap(); |
| 832 |
let err = ArgumentAttributes::try_from(&node).unwrap_err(); |
| 833 |
assert_eq!( |
| 834 |
err.to_string(), |
| 835 |
"Throws(\"Error\") not supported for arguments" |
| 836 |
); |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
fn test_trait_attribute() { |
| 841 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait]").unwrap(); |
| 842 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 843 |
assert_eq!(attrs.object_impl().unwrap(), ObjectImpl::Trait); |
| 844 |
|
| 845 |
let (_, node) = |
| 846 |
weedle::attribute::ExtendedAttributeList::parse("[Trait, WithForeign]").unwrap(); |
| 847 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 848 |
assert_eq!(attrs.object_impl().unwrap(), ObjectImpl::CallbackTrait); |
| 849 |
|
| 850 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[]").unwrap(); |
| 851 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 852 |
assert_eq!(attrs.object_impl().unwrap(), ObjectImpl::Struct); |
| 853 |
|
| 854 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[WithForeign]").unwrap(); |
| 855 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 856 |
assert!(attrs.object_impl().is_err()) |
| 857 |
} |
| 858 |
|
| 859 |
#[test] |
| 860 |
fn test_dictionary_attributes() { |
| 861 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Remote]").unwrap(); |
| 862 |
let attrs = DictionaryAttributes::try_from(&node).unwrap(); |
| 863 |
assert!(attrs.contains_remote()); |
| 864 |
|
| 865 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait]").unwrap(); |
| 866 |
let err = DictionaryAttributes::try_from(&node).unwrap_err(); |
| 867 |
assert_eq!(err.to_string(), "Trait not supported for dictionaries"); |
| 868 |
} |
| 869 |
|
| 870 |
#[test] |
| 871 |
fn test_enum_attribute_on_interface() { |
| 872 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Enum]").unwrap(); |
| 873 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 874 |
assert!(attrs.contains_enum_attr()); |
| 875 |
|
| 876 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[]").unwrap(); |
| 877 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 878 |
assert!(!attrs.contains_enum_attr()); |
| 879 |
|
| 880 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait]").unwrap(); |
| 881 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 882 |
assert!(!attrs.contains_enum_attr()); |
| 883 |
|
| 884 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait, Enum]").unwrap(); |
| 885 |
let err = InterfaceAttributes::try_from(&node).unwrap_err(); |
| 886 |
assert_eq!( |
| 887 |
err.to_string(), |
| 888 |
"conflicting attributes on interface definition" |
| 889 |
); |
| 890 |
} |
| 891 |
|
| 892 |
// Test parsing attributes for enum definitions |
| 893 |
#[test] |
| 894 |
fn test_enum_attributes() { |
| 895 |
let (_, node) = |
| 896 |
weedle::attribute::ExtendedAttributeList::parse("[Error, NonExhaustive, Remote]") |
| 897 |
.unwrap(); |
| 898 |
let attrs = EnumAttributes::try_from(&node).unwrap(); |
| 899 |
assert!(attrs.contains_error_attr()); |
| 900 |
assert!(attrs.contains_non_exhaustive_attr()); |
| 901 |
assert!(attrs.contains_remote()); |
| 902 |
|
| 903 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait]").unwrap(); |
| 904 |
let err = EnumAttributes::try_from(&node).unwrap_err(); |
| 905 |
assert_eq!(err.to_string(), "Trait not supported for enums"); |
| 906 |
} |
| 907 |
|
| 908 |
// Test parsing attributes for interface definitions with the `[Enum]` attribute |
| 909 |
#[test] |
| 910 |
fn test_enum_attributes_from_interface() { |
| 911 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Enum]").unwrap(); |
| 912 |
assert!(EnumAttributes::try_from(&node).is_ok()); |
| 913 |
|
| 914 |
let (_, node) = |
| 915 |
weedle::attribute::ExtendedAttributeList::parse("[Enum, Error, NonExhaustive, Remote]") |
| 916 |
.unwrap(); |
| 917 |
let attrs = EnumAttributes::try_from(&node).unwrap(); |
| 918 |
assert!(attrs.contains_error_attr()); |
| 919 |
assert!(attrs.contains_non_exhaustive_attr()); |
| 920 |
assert!(attrs.contains_remote()); |
| 921 |
|
| 922 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Enum, Trait]").unwrap(); |
| 923 |
let err = EnumAttributes::try_from(&node).unwrap_err(); |
| 924 |
assert_eq!(err.to_string(), "Trait not supported for enums"); |
| 925 |
} |
| 926 |
|
| 927 |
#[test] |
| 928 |
fn test_interface_attributes() { |
| 929 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Remote]").unwrap(); |
| 930 |
let attrs = InterfaceAttributes::try_from(&node).unwrap(); |
| 931 |
assert!(attrs.contains_remote()); |
| 932 |
|
| 933 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Trait, ByRef]").unwrap(); |
| 934 |
let err = InterfaceAttributes::try_from(&node).unwrap_err(); |
| 935 |
assert_eq!( |
| 936 |
err.to_string(), |
| 937 |
"ByRef not supported for interface definition" |
| 938 |
); |
| 939 |
} |
| 940 |
|
| 941 |
#[test] |
| 942 |
fn test_typedef_attribute() { |
| 943 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[Custom]").unwrap(); |
| 944 |
let attrs = TypedefAttributes::try_from(&node).unwrap(); |
| 945 |
assert!(attrs.is_custom()); |
| 946 |
assert!(attrs.get_crate_name().is_none()); |
| 947 |
|
| 948 |
let (_, node) = |
| 949 |
weedle::attribute::ExtendedAttributeList::parse("[Custom=\"crate_name\"]").unwrap(); |
| 950 |
let attrs = TypedefAttributes::try_from(&node).unwrap(); |
| 951 |
assert!(attrs.is_custom()); |
| 952 |
assert_eq!(attrs.get_crate_name().unwrap(), "crate_name"); |
| 953 |
|
| 954 |
let (_, node) = |
| 955 |
weedle::attribute::ExtendedAttributeList::parse("[External=crate_name]").unwrap(); |
| 956 |
let attrs = TypedefAttributes::try_from(&node).unwrap(); |
| 957 |
assert!(!attrs.is_custom()); |
| 958 |
assert_eq!(attrs.get_crate_name().unwrap(), "crate_name"); |
| 959 |
} |
| 960 |
|
| 961 |
#[test] |
| 962 |
fn test_typedef_attributes_malformed() { |
| 963 |
let (_, node) = |
| 964 |
weedle::attribute::ExtendedAttributeList::parse("[External=foo, Custom]").unwrap(); |
| 965 |
let err = TypedefAttributes::try_from(&node).unwrap_err(); |
| 966 |
assert_eq!(err.to_string(), "Can't be [Custom] and [External]"); |
| 967 |
|
| 968 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[External]").unwrap(); |
| 969 |
let err = TypedefAttributes::try_from(&node).unwrap_err(); |
| 970 |
assert_eq!( |
| 971 |
err.to_string(), |
| 972 |
"ExtendedAttributeNoArgs not supported: \"External\"" |
| 973 |
); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn test_other_attributes_not_supported_for_typedef() { |
| 978 |
let (_, node) = weedle::attribute::ExtendedAttributeList::parse("[ByRef]").unwrap(); |
| 979 |
let err = TypedefAttributes::try_from(&node).unwrap_err(); |
| 980 |
assert_eq!(err.to_string(), "ByRef not supported for typedefs"); |
| 981 |
} |
| 982 |
} |