uniffi_udl/
lib.rs

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//! # Uniffi support for webidl syntax, typically from a .udl file, as described by weedle.
6//!
7//! This library is dedicated to parsing a string in a webidl syntax, as described by
8//! weedle and with our own custom take on the attributes etc, pushing the boundaries
9//! of that syntax to describe a uniffi `MetadataGroup`.
10//!
11//! The output of this module is consumed by uniffi_bindgen to generate stuff.
12
13mod attributes;
14mod collectors;
15mod converters;
16mod finder;
17mod literal;
18mod resolver;
19
20use anyhow::Result;
21use collectors::{InterfaceCollector, TypeCollector};
22use uniffi_meta::Type;
23
24/// The single entry-point to this module.
25pub fn parse_udl(udl: &str, crate_name: &str) -> Result<uniffi_meta::MetadataGroup> {
26    Ok(InterfaceCollector::from_webidl(udl, crate_name)?.into())
27}
28
29#[cfg(test)]
30mod test {
31    use super::*;
32
33    #[test]
34    fn test_group() {
35        const UDL: &str = r#"
36            namespace test{};
37            dictionary Empty {};
38        "#;
39        let group = parse_udl(UDL, "crate_name").unwrap();
40        assert_eq!(group.namespace.name, "test");
41        assert_eq!(group.items.len(), 1);
42        assert!(matches!(
43            group.items.into_iter().next().unwrap(),
44            uniffi_meta::Metadata::Record(r) if r.module_path == "crate_name" && r.name == "Empty" && r.fields.is_empty()
45        ));
46    }
47}