IntoComponents

Trait IntoComponents 

Source
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:

  1. Overall span: The span covering the entire element
  2. Required components: Core parts that are always present
  3. Optional components: Parts that may or may not be present
  4. Sub-elements: Nested parsed elements in source order

Required Associated Types§

Source

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§

Source

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.

Implementors§

Source§

impl<D, S> IntoComponents for Spanned<D, S>

Source§

impl<D, S, Lang> IntoComponents for Lit<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitBinary<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitBool<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitByte<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitByteString<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitChar<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitDecimal<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitFloat<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitHex<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitHexFloat<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitMultilineString<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitNull<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitOctal<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitRawString<D, S, Lang>

Source§

impl<D, S, Lang> IntoComponents for LitString<D, S, Lang>

Source§

impl<D, Sp, Sl> IntoComponents for Located<D, Sp, Sl>

Source§

impl<D, Src> IntoComponents for Sliced<D, Src>

Source§

impl<Open, Close, Data, S> IntoComponents for Delimited<Open, Close, Data, S>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Ampersand<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Angle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Arrow<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for At<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for BackSlash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Brace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Bracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CarriageReturn<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CarriageReturnNewline<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CloseAngle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CloseBrace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CloseBracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for CloseParen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Colon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for ColonEq<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Comma<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Dollar<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Dot<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for DoubleColon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Equal<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for FatArrow<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Hash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Hyphen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Newline<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for OpenAngle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for OpenBrace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for OpenBracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for OpenParen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Paren<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Percent<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Pipe<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Semicolon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Slash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Space<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Tab<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Tilde<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Trivia<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> IntoComponents for Underscore<S, C, Lang>

Source§

impl<S, Lang> IntoComponents for Ident<S, Lang>

Source§

impl<S, Lang> IntoComponents for Keyword<S, Lang>