Implement a functionality in Rust that processes HTML fragments and generates a specific output format, effectively transforming HTML elements into a structured representation resembling a seed syntax. This should be placed in the /app/ directory of your project.
Requirements:
Functionality:
Create a function html_to_seed(fragment: &str, use_typed_classes: bool) -> String that takes an HTML fragment as a string and a boolean flag use_typed_classes.
The function should parse the HTML fragment and generate a structured string that reflects the hierarchy of HTML elements while converting class attributes into a specific format when use_typed_classes is true.
HTML Parsing:
Utilize the html5ever crate to parse the HTML fragment within a function parse_fragment(fragment: &str) -> RcDom.
Handle different node types, particularly Document, Element, Text, and Comment. Ignore other node types.
Output Structure:
Convert each HTML div element to the format div![...].
For class attributes, when use_typed_classes is true, convert class names using:
Replace / with of
Replace - with _
Replace : with __
Generate a syntax representation for attributes in the format attrs!{...}.
Whitespace Management:
Remove excessive whitespace from text nodes, ensuring that only single spaces remain between words. Leading and trailing whitespace should be omitted.
Testing:
Ensure the implementation passes the provided Rust tests under the #[cfg(test)] module which validate the html_to_seed function and the to_typed_class helper function.
Error Handling:
Render appropriate errors or panic statements for invalid input or parsing issues as necessary, ensuring robustness.
Place your code implementation in /app/ and ensure it aligns with the outlined structure and requirements.