pub struct ParserConfig<C = Infallible> { /* private fields */ }Expand description
Configures the Parser behavior
Implementations§
Source§impl ParserConfig
impl ParserConfig
Sourcepub fn new() -> ParserConfig
pub fn new() -> ParserConfig
Create new ParserConfig with default config
Source§impl<C> ParserConfig<C>
impl<C> ParserConfig<C>
Sourcepub fn number_of_top_level_nodes(self, number: usize) -> Self
pub fn number_of_top_level_nodes(self, number: usize) -> Self
Exact number of required top level nodes
Sourcepub fn type_of_top_level_nodes(self, node_type: NodeType) -> Self
pub fn type_of_top_level_nodes(self, node_type: NodeType) -> Self
Enforce the NodeType of top level nodes
Sourcepub fn recover_block(self, recover_block: bool) -> Self
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.
Sourcepub fn always_self_closed_elements(
self,
elements: HashSet<&'static str>,
) -> Self
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:
Sourcepub fn raw_text_elements(self, elements: HashSet<&'static str>) -> Self
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.
Sourcepub fn transform_block<F>(self, callback: F) -> Self
pub fn transform_block<F>(self, callback: F) -> Self
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();Sourcepub fn element_close_wildcard<F>(self, predicate: F) -> Self
pub fn element_close_wildcard<F>(self, predicate: F) -> Self
Allows unmatched tag pairs where close tag matches a specified wildcard.
For example:
use quote::quote;
use rstml::{
node::{Node, NodeElement},
Parser, ParserConfig,
};
use syn::{Expr, ExprRange, RangeLimits, Stmt};
let config = ParserConfig::new()
.element_close_wildcard(|_open_tag, close_tag| close_tag.name.is_wildcard());
let tokens = quote! {
<{"OpenTag"}>{"Content"}</_>
};
Parser::new(config).parse_simple(tokens).unwrap();Sourcepub fn element_close_use_default_wildcard_ident(
self,
open_tag_should_be_block: bool,
) -> Self
pub fn element_close_use_default_wildcard_ident( self, open_tag_should_be_block: bool, ) -> Self
Allows unmatched tag pairs where close tag matches a specified wildcard.
Uses default wildcard ident (_), and optionally check whenewer
open_tag name is block.
Sourcepub fn custom_node<CN: CustomNode>(self) -> ParserConfig<CN>
pub fn custom_node<CN: CustomNode>(self) -> ParserConfig<CN>
Enables parsing for [Node::Custom] using a type implementing
CustomNode.