pub trait IntoComponents {
type Components;
// Required method
fn into_components(self) -> Self::Components;
}Expand description
Enables destructuring a parsed element into its constituent components.
This trait provides a way to break down complex parsed elements into their individual parts, taking ownership of each component. This is particularly useful for transformation, analysis, or when building different representations of the parsed data.
§Design Philosophy
The trait uses an associated type rather than generic parameters to ensure that each implementing type has exactly one way to be decomposed. This provides type safety and makes the interface predictable for consumers.
§Usage Patterns
Common scenarios for using this trait:
- AST transformation: Converting parsed elements into different AST representations
- Analysis: Extracting specific components for validation or processing
- Serialization: Breaking down elements for custom serialization formats
- Testing: Accessing individual components for detailed assertions
§Examples
// Extracting components for transformation
let float_value: FloatValue<&str, SimpleSpan> = parse_float("3.14e-2")?;
let (span, int_part, frac_part, exp_part) = float_value.into_components();
// Building a custom representation
let custom_float = CustomFloat {
location: span,
integer: int_part,
fractional: frac_part,
exponent: exp_part,
};
// Component analysis
let int_literal: IntValue<&str, SimpleSpan> = parse_int("-42")?;
let (span, sign, digits) = int_literal.into_components();
if sign.is_some() {
println!("Found negative integer at {:?}", span);
}§Implementation Guidelines
When implementing this trait:
- Include all meaningful components of the parsed element
- Order components logically (typically: span first, then sub-components in source order)
- Use tuples for simple decomposition, custom structs for complex cases
- Ensure the decomposition is complete (no information loss)
- Document the component structure clearly
§Component Ordering Convention
To maintain consistency across implementations, follow this ordering:
- Overall span: The span covering the entire element
- Required components: Core parts that are always present
- Optional components: Parts that may or may not be present
- Sub-elements: Nested parsed elements in source order
Required Associated Types§
Sourcetype Components
type Components
The tuple or struct type containing the decomposed components.
This associated type defines the structure returned by into_components().
It should include all meaningful parts of the parsed element in a logical
order that makes sense for the specific element type.
Required Methods§
Sourcefn into_components(self) -> Self::Components
fn into_components(self) -> Self::Components
Consumes this element and returns its constituent components.
This method breaks down the parsed element into its individual parts,
providing owned access to each component. The exact structure of the
returned components is defined by the Components associated type.