| 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::EnumAttributes, converters::convert_docstring, InterfaceCollector}; |
| 7 |
use anyhow::{bail, Result}; |
| 8 |
|
| 9 |
use uniffi_meta::{EnumMetadata, EnumShape, VariantMetadata}; |
| 10 |
|
| 11 |
// Note that we have 2 `APIConverter` impls here - one for the `enum` case |
| 12 |
// (including an enum with `[Error]`), and one for the `[Error] interface` cas |
| 13 |
// (which is still an enum, but with different "flatness" characteristics.) |
| 14 |
impl APIConverter<EnumMetadata> for weedle::EnumDefinition<'_> { |
| 15 |
fn convert(&self, ci: &mut InterfaceCollector) -> Result<EnumMetadata> { |
| 16 |
let attributes = EnumAttributes::try_from(self.attributes.as_ref())?; |
| 17 |
let shape = if attributes.contains_error_attr() { |
| 18 |
EnumShape::Error { flat: true } |
| 19 |
} else { |
| 20 |
EnumShape::Enum |
| 21 |
}; |
| 22 |
Ok(EnumMetadata { |
| 23 |
module_path: ci.module_path(), |
| 24 |
name: self.identifier.0.to_string(), |
| 25 |
shape, |
| 26 |
remote: attributes.contains_remote(), |
| 27 |
discr_type: None, |
| 28 |
variants: self |
| 29 |
.values |
| 30 |
.body |
| 31 |
.list |
| 32 |
.iter() |
| 33 |
.map::<Result<_>, _>(|v| { |
| 34 |
Ok(VariantMetadata { |
| 35 |
name: v.value.0.to_string(), |
| 36 |
discr: None, |
| 37 |
fields: vec![], |
| 38 |
docstring: v.docstring.as_ref().map(|v| convert_docstring(&v.0)), |
| 39 |
}) |
| 40 |
}) |
| 41 |
.collect::<Result<Vec<_>>>()?, |
| 42 |
non_exhaustive: attributes.contains_non_exhaustive_attr(), |
| 43 |
docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)), |
| 44 |
}) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl APIConverter<EnumMetadata> for weedle::InterfaceDefinition<'_> { |
| 49 |
fn convert(&self, ci: &mut InterfaceCollector) -> Result<EnumMetadata> { |
| 50 |
if self.inheritance.is_some() { |
| 51 |
bail!("interface inheritance is not supported for enum interfaces"); |
| 52 |
} |
| 53 |
let attributes = EnumAttributes::try_from(self.attributes.as_ref())?; |
| 54 |
let shape = if attributes.contains_error_attr() { |
| 55 |
EnumShape::Error { flat: false } |
| 56 |
} else { |
| 57 |
EnumShape::Enum |
| 58 |
}; |
| 59 |
Ok(EnumMetadata { |
| 60 |
module_path: ci.module_path(), |
| 61 |
name: self.identifier.0.to_string(), |
| 62 |
shape, |
| 63 |
remote: attributes.contains_remote(), |
| 64 |
variants: self |
| 65 |
.members |
| 66 |
.body |
| 67 |
.iter() |
| 68 |
.map::<Result<VariantMetadata>, _>(|member| match member { |
| 69 |
weedle::interface::InterfaceMember::Operation(t) => Ok(t.convert(ci)?), |
| 70 |
_ => bail!( |
| 71 |
"interface member type {:?} not supported in enum interface", |
| 72 |
member |
| 73 |
), |
| 74 |
}) |
| 75 |
.collect::<Result<Vec<_>>>()?, |
| 76 |
discr_type: None, |
| 77 |
non_exhaustive: attributes.contains_non_exhaustive_attr(), |
| 78 |
docstring: self.docstring.as_ref().map(|v| convert_docstring(&v.0)), |
| 79 |
// Enums declared using the `[Enum] interface` syntax might have variants with fields. |
| 80 |
//flat: false, |
| 81 |
}) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
#[cfg(test)] |
| 86 |
mod test { |
| 87 |
use super::*; |
| 88 |
use uniffi_meta::Metadata; |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_duplicate_variants() { |
| 92 |
const UDL: &str = r#" |
| 93 |
namespace test{}; |
| 94 |
// Weird, but currently allowed! |
| 95 |
// We should probably disallow this... |
| 96 |
enum Testing { "one", "two", "one" }; |
| 97 |
"#; |
| 98 |
let mut ci = InterfaceCollector::from_webidl(UDL, "crate_name").unwrap(); |
| 99 |
assert_eq!(ci.types.namespace, "test"); |
| 100 |
assert_eq!(ci.types.module_path(), "crate_name"); |
| 101 |
assert_eq!(ci.items.len(), 1); |
| 102 |
let e = &ci.items.pop_first().unwrap(); |
| 103 |
match e { |
| 104 |
Metadata::Enum(e) => assert_eq!(e.variants.len(), 3), |
| 105 |
_ => unreachable!(), |
| 106 |
} |
| 107 |
} |
| 108 |
} |