Struct rstml::ParserConfig

source ·
pub struct ParserConfig { /* private fields */ }
Expand description

Configures the Parser behavior

Implementations§

source§

impl ParserConfig

source

pub fn new() -> ParserConfig

Create new ParserConfig with default config

source

pub fn flat_tree(self) -> Self

Return flat tree instead of nested tree

source

pub fn number_of_top_level_nodes(self, number: usize) -> Self

Exact number of required top level nodes

source

pub fn type_of_top_level_nodes(self, node_type: NodeType) -> Self

Enforce the NodeType of top level nodes

source

pub fn recover_block(self, recover_block: bool) -> Self

Try to parse invalid syn::Block. If set tot true, NodeBlock can return Invalid variant.

If NodeBlock is failed to parse as syn::Block it still usefull to emit it as expression. It will enhance IDE compatibility, and provide completion in cases of invalid blocks, for example {x.} is invalid expression, because after dot token } is unexpected. But for ide it is a marker that quick completion should be provided.

source

pub fn always_self_closed_elements( self, elements: HashSet<&'static str> ) -> Self

Set array of nodes that is known to be self closed, it also known as void element. Void elements has no child and must not have closing tag. Parser will not search for it closing tag, even if no slash at end of it open part was found.

Because we work in proc-macro context, we expect it as ’static refs.

Examples:

source

pub fn raw_text_elements(self, elements: HashSet<&'static str>) -> Self

Set array of nodes that is known to be parsed in two-phases, Parser will skip parsing of children nodes. and provide one child with RawText instead.

This is usefull when parsing <script> or <style> tags elements.

If you need fragment to be used in this context, empty string(“”) should be inserted.

Raw texts has few limitations, check out RawText documentation.

source

pub fn transform_block<F>(self, callback: F) -> Selfwhere F: Fn(ParseStream<'_>) -> Result<Option<TokenStream>> + 'static,

Transforms the value of all NodeType::Blocks with the given closure callback. The provided ParseStream is the content of the block.

When Some(TokenStream) is returned, the TokenStream is parsed as Rust block content. The ParseStream must be completely consumed in this case, meaning no tokens can be left in the stream.

If None is returned, parsing happens with the original ParseStream, since the tokens that are passend into the transform callback are a fork, which gets only advanced if Some is returned.

An example usage might be a custom syntax inside blocks which isn’t valid Rust. The given example simply translates the % character into the string percent

use quote::quote;
use syn::Token;
use rstml::{parse2_with_config, ParserConfig};

let tokens = quote! {
    <div>{%}</div>
};

let config = ParserConfig::new().transform_block(|input| {
    input.parse::<Token![%]>()?;
    Ok(Some(quote! { "percent" }))
});

parse2_with_config(tokens, config).unwrap();

Trait Implementations§

source§

impl Clone for ParserConfig

source§

fn clone(&self) -> ParserConfig

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ParserConfig

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for ParserConfig

source§

fn default() -> ParserConfig

Returns the “default value” for a type. Read more
source§

impl From<ParserConfig> for RecoveryConfig

source§

fn from(config: ParserConfig) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.