Skip to content

Commit

Permalink
chore: Refactor custom node visitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
vldm committed Jan 9, 2024
1 parent 24c3d64 commit 0471cfd
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 154 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ jobs:
- name: build
run: cargo build

- name: test
run: cargo test -p rstml --features "rawtext-stable-hack-module"

- name: clippy
run: cargo clippy --workspace

- name: test on Stable
run: cargo test --workspace

- name: Tests with rawtext hack
run: cargo test -p rstml --features "rawtext-stable-hack-module"

- name: Test extendable feature in rstml-control-flow
run: cargo test -p rstml-control-flow --features "extendable"

- uses: dtolnay/rust-toolchain@nightly

- name: test on Nightly
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ harness = false
path = "benches/bench.rs"

[workspace]
members = ["examples/html-to-string-macro", "rstml-controll-flow"]
members = ["examples/html-to-string-macro", "rstml-control-flow"]

[features]
default = ["colors"]
Expand Down
2 changes: 1 addition & 1 deletion examples/html-to-string-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rstml = { version = "0.11.0", path = "../../", features = [
] }
proc-macro2-diagnostics = "0.10"
derive-where = "1.2.5"
rstml-controll-flow = { version = "0.1.0", path = "../../rstml-controll-flow" }
rstml-control-flow = { version = "0.1.0", path = "../../rstml-control-flow" }

[dev-dependencies]
trybuild = "1.0"
13 changes: 7 additions & 6 deletions examples/html-to-string-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quote::{quote, quote_spanned, ToTokens};
use rstml::{
node::{Node, NodeAttribute, NodeName},
visitor::{visit_attributes, visit_nodes, Visitor},
Infallible, Parser, ParserConfig,
Parser, ParserConfig,
};
use syn::spanned::Spanned;
// mod escape;
Expand Down Expand Up @@ -46,9 +46,10 @@ impl WalkNodesOutput {
}
impl<'a> syn::visit_mut::VisitMut for WalkNodes<'a> {}

impl<'a> Visitor for WalkNodes<'a> {
type Custom = Infallible;

impl<'a, C> Visitor<C> for WalkNodes<'a>
where
C: rstml::node::CustomNode + 'static,
{
fn visit_doctype(&mut self, doctype: &mut rstml::node::NodeDoctype) -> bool {
let value = &doctype.value.to_token_stream_string();
self.output
Expand All @@ -67,7 +68,7 @@ impl<'a> Visitor for WalkNodes<'a> {
self.output.static_format.push_str(&node.to_string_best());
false
}
fn visit_fragment(&mut self, fragment: &mut rstml::node::NodeFragment<Self::Custom>) -> bool {
fn visit_fragment(&mut self, fragment: &mut rstml::node::NodeFragment<C>) -> bool {
let visitor = self.child_output();
let child_output = visit_nodes(&mut fragment.children, visitor);
self.output.extend(child_output.output);
Expand All @@ -85,7 +86,7 @@ impl<'a> Visitor for WalkNodes<'a> {
self.output.values.push(block.to_token_stream());
false
}
fn visit_element(&mut self, element: &mut rstml::node::NodeElement<Self::Custom>) -> bool {
fn visit_element(&mut self, element: &mut rstml::node::NodeElement<C>) -> bool {
let name = element.name().to_string();
self.output.static_format.push_str(&format!("<{}", name));
self.output
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rstml-controll-flow"
name = "rstml-control-flow"
version = "0.1.0"
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ let nodes = parse2_with_config(template, Default::default().with_custom_nodes::<
## Using multiple `CustomNode`s at once
It is also possible to use more than one `CustomNode` at once.
For example, if you want to use both `Conditions` and `EscapedCode` custom nodes.
`rstml-controll-flow` crate provides an `ExtendableCustomNode` struct that can be used to combine multiple `CustomNode`s into one. Check out `extendable.rs` docs and tests in `lib.rs` for more details.
`rstml-control-flow` crate provides an `ExtendableCustomNode` struct that can be used to combine multiple `CustomNode`s into one. Check out `extendable.rs` docs and tests in `lib.rs` for more details.


```rust
Loading

0 comments on commit 0471cfd

Please sign in to comment.