### Parse Namespaces in Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Illustrates parsing WebIDL namespace definitions using the `weedle` crate. The example shows how to extract the namespace identifier and details about its members, such as operations and their argument counts. ```rust use weedle::{Parse, namespace::NamespaceDefinition}; fn main() { let webidl = " namespace console { undefined log(any... data); undefined warn(any... data); undefined error(any... data); }; "; let (_, parsed) = NamespaceDefinition::parse(webidl).unwrap(); println!("Namespace: {}", parsed.identifier.0); for member in &parsed.members.body { match member { weedle::namespace::NamespaceMember::Operation(op) => { if let Some(name) = &op.identifier { let arg_count = op.args.body.list.len(); println!(" operation {} with {} args", name.0, arg_count); } } _ => {} // Handle other namespace member types if necessary } } } ``` -------------------------------- ### Parse Enums in Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Illustrates parsing WebIDL enum definitions using the `weedle` crate. The example shows how to extract the enum's identifier and its associated string values. ```rust use weedle::Parse; fn main() { let webidl = r###" enum RequestMode { "navigate", "same-origin", "no-cors", "cors" }; "###; let parsed = weedle::parse(webidl).unwrap(); match &parsed[0] { weedle::Definition::Enum(enum_def) => { println!("Enum name: {}", enum_def.identifier.0); println!("Values:"); for value in &enum_def.values.body.list { println!(" - {}", value.0); } } _ => {} } } ``` -------------------------------- ### Parsing Interfaces with Inheritance Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates how to parse WebIDL interfaces, including those that extend other interfaces. It shows how to access interface members like attributes and operations. ```APIDOC ## Parsing Interfaces with Inheritance ### Description Parses WebIDL interface definitions, including inheritance, and iterates through its members (attributes, operations, etc.). ### Method `weedle::interface::InterfaceDefinition::parse(webidl: &str) -> Result<(&str, weedle::interface::InterfaceDefinition), _> ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use weedle::{Parse, interface::InterfaceDefinition}; fn main() { let webidl = "\n interface HTMLElement : Element {\n readonly attribute DOMString title;\n attribute DOMString innerHTML;\n undefined focus();\n };\n "; let (remaining, parsed) = InterfaceDefinition::parse(webidl).unwrap(); assert!(remaining.is_empty()); println!("Interface: {}", parsed.identifier.0); // Check for inheritance if let Some(inheritance) = &parsed.inheritance { println!("Extends: {}", inheritance.identifier.0); } // Iterate through members for member in &parsed.members.body { match member { weedle::interface::InterfaceMember::Attribute(attr) => { let readonly = if attr.readonly.is_some() { "readonly " } else { "" }; println!(" {}attribute: {}", readonly, attr.identifier.0); } weedle::interface::InterfaceMember::Operation(op) => { if let Some(name) = &op.identifier { println!(" operation: {}", name.0); } } _ => {} } } } ``` ### Response #### Success Response (200) - **parsed** (InterfaceDefinition) - The parsed interface definition. - **remaining** (&str) - The remaining unparsed portion of the input string. #### Response Example (See code example for structure of `parsed`) ``` -------------------------------- ### Parse Callbacks and Callback Interfaces in Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates parsing WebIDL callback function types and callback interfaces using the `weedle` crate. It shows how to extract the name, arguments of callbacks, and the identifier and members of callback interfaces. ```rust use weedle::Parse; fn main() { // Parse a callback function type let callback_webidl = " callback EventHandlerNonNull = any (Event event); "; let parsed = weedle::parse(callback_webidl).unwrap(); match &parsed[0] { weedle::Definition::Callback(callback) => { println!("Callback name: {}", callback.identifier.0); println!("Number of arguments: {}", callback.arguments.body.list.len()); } _ => {} } // Parse a callback interface let callback_interface_webidl = " callback interface EventListener { undefined handleEvent(Event event); }; "; let parsed = weedle::parse(callback_interface_webidl).unwrap(); match &parsed[0] { weedle::Definition::CallbackInterface(iface) => { println!("Callback interface: {}", iface.identifier.0); println!("Methods: {}", iface.members.body.len()); } _ => {} } } ``` -------------------------------- ### Parse WebIDL Definitions with Weedle - Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates the main entry point `parse` function in Weedle to parse a complete WebIDL string into a `Vec`. It shows how to access parsed interface information. ```rust use weedle; fn main() { let webidl_source = " interface Window { readonly attribute Storage sessionStorage; undefined alert(DOMString message); }; "; let parsed = weedle::parse(webidl_source).unwrap(); // parsed is a Vec containing all top-level definitions println!("Parsed {} definitions", parsed.len()); match &parsed[0] { weedle::Definition::Interface(interface_def) => { println!("Interface name: {}", interface_def.identifier.0); println!("Number of members: {}", interface_def.members.body.len()); } _ => {} } } ``` -------------------------------- ### Parse Function - Main Entry Point Source: https://context7.com/wasm-bindgen/weedle/llms.txt The `parse` function provides a convenient way to parse complete WebIDL definitions from a string. It returns a `Vec` containing all top-level definitions found in the input. ```APIDOC ## Parse Function - Main Entry Point ### Description Parses a complete WebIDL definition from a string. ### Method `weedle::parse(source: &str) -> Result, _> ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use weedle; fn main() { let webidl_source = "\n interface Window {\n readonly attribute Storage sessionStorage;\n undefined alert(DOMString message);\n };\n "; let parsed = weedle::parse(webidl_source).unwrap(); println!(\"Parsed {} definitions\", parsed.len()); match &parsed[0] { weedle::Definition::Interface(interface_def) => { println!(\"Interface name: {}\", interface_def.identifier.0); println!(\"Number of members: {}\", interface_def.members.body.len()); } _ => {} } } ``` ### Response #### Success Response (200) - **parsed** (Vec) - A vector containing all top-level parsed definitions. #### Response Example (See code example for structure of `parsed`) ``` -------------------------------- ### Parse Interface Constructors with Extended Attributes (Rust) Source: https://context7.com/wasm-bindgen/weedle/llms.txt Illustrates parsing of WebIDL interfaces that include constructor operations, potentially with extended attributes like '[Exposed=Window]'. It iterates through interface members to identify and count arguments for each constructor. ```rust use weedle::Parse; fn main() { let webidl = " [Exposed=Window] interface Image { constructor(); constructor(unsigned long width, unsigned long height); attribute DOMString src; }; "; let parsed = weedle::parse(webidl).unwrap(); if let weedle::Definition::Interface(interface) = &parsed[0] { for member in &interface.members.body { if let weedle::interface::InterfaceMember::Constructor(ctor) = member { let arg_count = ctor.args.body.list.len(); println!("Constructor with {} arguments", arg_count); } } } } ``` -------------------------------- ### Parsing Dictionaries with Required Fields Source: https://context7.com/wasm-bindgen/weedle/llms.txt Illustrates parsing WebIDL dictionaries, highlighting how to identify required fields and fields with default values. ```APIDOC ## Parsing Dictionaries with Required Fields ### Description Parses WebIDL dictionary definitions and identifies fields, including whether they are required or have default values. ### Method `weedle::dictionary::DictionaryDefinition::parse(webidl: &str) -> Result<(&str, weedle::dictionary::DictionaryDefinition), _> ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use weedle::{Parse, dictionary::DictionaryDefinition}; fn main() { let webidl = "\n dictionary RequestInit {\n required DOMString method;\n HeadersInit headers;\n DOMString body;\n required RequestMode mode = \"cors\";\n };\n "; let (remaining, parsed) = DictionaryDefinition::parse(webidl).unwrap(); assert!(remaining.is_empty()); println!("Dictionary: {}", parsed.identifier.0); for member in &parsed.members.body { let is_required = member.required.is_some(); let has_default = member.default.is_some(); println!(" Field: {} (required: {}, has_default: {})", member.identifier.0, is_required, has_default); } } ``` ### Response #### Success Response (200) - **parsed** (DictionaryDefinition) - The parsed dictionary definition. - **remaining** (&str) - The remaining unparsed portion of the input string. #### Response Example (See code example for structure of `parsed`) ``` -------------------------------- ### Parse Interface Mixins and Includes (Rust) Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates parsing of WebIDL interface mixins and includes statements. It shows how to extract the mixin identifier and its members, as well as the interface and mixin identifiers involved in an 'includes' declaration. ```rust use weedle::Parse; fn main() { let webidl = " interface mixin WindowLocalStorage { readonly attribute Storage localStorage; }; Window includes WindowLocalStorage; "; let parsed = weedle::parse(webidl).unwrap(); // First definition is the mixin if let weedle::Definition::InterfaceMixin(mixin) = &parsed[0] { println!("Mixin: {}", mixin.identifier.0); println!("Members: {}", mixin.members.body.len()); } // Second definition is the includes statement if let weedle::Definition::IncludesStatement(includes) = &parsed[1] { println!("{} includes {}", includes.lhs_identifier.0, includes.rhs_identifier.0); } } ``` -------------------------------- ### Rust: Handle Full and Partial WebIDL Parse Errors Source: https://context7.com/wasm-bindgen/weedle/llms.txt This Rust code snippet demonstrates robust error handling for both full and partial parsing of WebIDL strings using the `weedle` library. It shows how to match on `Result` types to differentiate between successful parsing, specific parsing errors (including remaining input and error kinds), and incomplete input. It also illustrates partial parsing using the `Parse` trait. ```rust use weedle; fn main() { let invalid_webidl = "interface Window { invalid syntax here };"; match weedle::parse(invalid_webidl) { Ok(definitions) => { println!("Successfully parsed {} definitions", definitions.len()); } Err(err) => { match err { weedle::Err::Error((remaining, kind)) | weedle::Err::Failure((remaining, kind)) => { println!("Parse error at: {}", remaining); println!("Error kind: {:?}", kind); } weedle::Err::Incomplete(_) => { println!("Incomplete input"); } } } } // For partial parsing using the Parse trait let partial_webidl = "interface Window {};"; let result = weedle::interface::InterfaceDefinition::parse(partial_webidl); match result { Ok((remaining, parsed)) => { println!("Parsed interface: {}", parsed.identifier.0); println!("Remaining input: '{}'", remaining); } Err(_) => println!("Failed to parse"), } } ``` -------------------------------- ### Parse Inheriting Interfaces with Weedle - Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Shows how to parse WebIDL interfaces that extend other interfaces using `InterfaceDefinition::parse`. It extracts the interface name, checks for inheritance, and iterates through its members (attributes and operations). ```rust use weedle::{Parse, interface::InterfaceDefinition}; fn main() { let webidl = " interface HTMLElement : Element { readonly attribute DOMString title; attribute DOMString innerHTML; undefined focus(); }; "; let (remaining, parsed) = InterfaceDefinition::parse(webidl).unwrap(); assert!(remaining.is_empty()); println!("Interface: {}", parsed.identifier.0); // Check for inheritance if let Some(inheritance) = &parsed.inheritance { println!("Extends: {}", inheritance.identifier.0); } // Iterate through members for member in &parsed.members.body { match member { weedle::interface::InterfaceMember::Attribute(attr) => { let readonly = if attr.readonly.is_some() { "readonly " } else { "" }; println!(" {}attribute: {}", readonly, attr.identifier.0); } weedle::interface::InterfaceMember::Operation(op) => { if let Some(name) = &op.identifier { println!(" operation: {}", name.0); } } _ => {} } } } ``` -------------------------------- ### Parse Extended Attributes in Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates parsing WebIDL extended attributes attached to interface definitions using the `weedle` crate. It shows how to identify and extract attributes with different argument formats, like named lists and no arguments. ```rust use weedle::Parse; fn main() { let webidl = " [Exposed=(Window,Worker), SecureContext] interface Crypto { [Throws] ArrayBufferView getRandomValues(ArrayBufferView array); }; "; let parsed = weedle::parse(webidl).unwrap(); match &parsed[0] { weedle::Definition::Interface(interface) => { if let Some(attrs) = &interface.attributes { println!("Interface has {} extended attributes", attrs.body.list.len()); for attr in &attrs.body.list { match attr { weedle::attribute::ExtendedAttribute::IdentList(list) => { println!(" {}=({})", list.identifier.0, list.list.body.list.len()); } weedle::attribute::ExtendedAttribute::NoArgs(name) => { println!(" {}", name.0.0); } _ => {} // Handle other attribute types if necessary } } } } _ => {} // Handle other definition types if necessary } } ``` -------------------------------- ### Parse WebIDL Interface using Weedle in Rust Source: https://github.com/wasm-bindgen/weedle/blob/master/README.md This Rust code snippet demonstrates the basic usage of the Weedle crate to parse a WebIDL interface definition. It takes a string containing WebIDL as input, calls the `weedle::parse` function, and prints the resulting parsed data structure. Error handling is done using `.unwrap()` for simplicity. ```rust fn main() { let parsed = weedle::parse(" interface Window { readonly attribute Storage sessionStorage; }; ").unwrap(); println!("{:?}", parsed); } ``` -------------------------------- ### Parse Dictionaries with Required Fields using Weedle - Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates parsing WebIDL dictionaries, including those with required fields and default values, using `DictionaryDefinition::parse`. It iterates through fields to identify requirements and default value presence. ```rust use weedle::{Parse, dictionary::DictionaryDefinition}; fn main() { let webidl = " dictionary RequestInit { required DOMString method; HeadersInit headers; DOMString body; required RequestMode mode = \"cors\"; }; "; let (remaining, parsed) = DictionaryDefinition::parse(webidl).unwrap(); assert!(remaining.is_empty()); println!("Dictionary: {}", parsed.identifier.0); for member in &parsed.members.body { let is_required = member.required.is_some(); let has_default = member.default.is_some(); println!(" Field: {} (required: {}, has_default: {})", member.identifier.0, is_required, has_default); } } ``` -------------------------------- ### Parse Typedef Declarations in Rust Source: https://context7.com/wasm-bindgen/weedle/llms.txt Shows how to parse WebIDL typedef declarations using the `weedle` crate. The code demonstrates extracting the alias name for various type definitions, including unions and sequences. ```rust use weedle::Parse; fn main() { let webidl = " typedef (Float32Array or sequence) Float32List; typedef unsigned long long EpochTimeStamp; "; let parsed = weedle::parse(webidl).unwrap(); for definition in parsed { if let weedle::Definition::Typedef(typedef) = definition { println!("Type alias: {}", typedef.identifier.0); } } } ``` -------------------------------- ### Parse Complex WebIDL Types (Rust) Source: https://context7.com/wasm-bindgen/weedle/llms.txt Parses various complex WebIDL types including union types, sequences (potentially nested with Promises), records, and nullable types using the weedle crate. It demonstrates type matching after parsing. ```rust use weedle::{Parse, types::Type}; fn main() { // Parse union type let union_type = "(DOMString or sequence)"; let (_, parsed) = Type::parse(union_type).unwrap(); match parsed { Type::Union(_) => println!("Successfully parsed union type"), _ => {} } // Parse sequence type let sequence_type = "sequence>"; let (_, parsed) = Type::parse(sequence_type).unwrap(); println!("Parsed nested generic type"); // Parse record type let record_type = "record"; let (_, parsed) = Type::parse(record_type).unwrap(); println!("Parsed record type"); // Parse nullable type let nullable_type = "DOMString?"; let (_, parsed) = Type::parse(nullable_type).unwrap(); println!("Parsed nullable type"); } ``` -------------------------------- ### Parse Iterable and Maplike Declarations in Interfaces (Rust) Source: https://context7.com/wasm-bindgen/weedle/llms.txt Demonstrates parsing of WebIDL interfaces that are declared as iterable or maplike. The code checks for these specific member types and prints information about whether an interface is iterable or maplike, including its readonly status. ```rust use weedle::Parse; fn main() { let webidl = " interface NodeList { readonly attribute unsigned long length; iterable; }; interface Headers { readonly maplike; }; "; let parsed = weedle::parse(webidl).unwrap(); // First interface has iterable if let weedle::Definition::Interface(interface) = &parsed[0] { for member in &interface.members.body { match member { weedle::interface::InterfaceMember::Iterable(_) => { println!("{} is iterable", interface.identifier.0); } _ => {} } } } // Second interface has maplike if let weedle::Definition::Interface(interface) = &parsed[1] { for member in &interface.members.body { match member { weedle::interface::InterfaceMember::Maplike(maplike) => { let readonly = maplike.readonly.is_some(); println!("{} is maplike (readonly: {})", interface.identifier.0, readonly); } _ => {} } } } } ``` -------------------------------- ### Add Weedle Dependency to Cargo.toml Source: https://github.com/wasm-bindgen/weedle/blob/master/README.md This snippet shows how to add the Weedle crate as a dependency in your Rust project's Cargo.toml file. It specifies the version of Weedle to be used, ensuring compatibility and managing project dependencies. ```toml [dependencies] weedle = "0.9.0" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.