Skip to content

Commit c46f618

Browse files
authored
Merge pull request #6 from mohe2015/attribute-with-dash
implement attributes with dash
2 parents 389e645 + 9e0e6e4 commit c46f618

File tree

5 files changed

+98
-12
lines changed

5 files changed

+98
-12
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

async-zero-cost-templating-proc-macro2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ syn = { default-features = false, version = "2.0", features = [
1818
] }
1919
tracing = "0.1"
2020
tracing-subscriber = "0.3"
21+
itertools = "0.13"

async-zero-cost-templating-proc-macro2/src/intermediate.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use itertools::Itertools as _;
12
use proc_macro2::{Span, TokenStream};
23
use syn::{
34
spanned::Spanned,
45
token::{Brace, Paren},
56
};
67

78
use crate::parse::{
8-
HtmlElement, HtmlForLoop, HtmlIf, HtmlInAttributeContext, HtmlInAttributeValueContext,
9-
HtmlInElementContext, HtmlWhile,
9+
DashOrColon, HtmlElement, HtmlForLoop, HtmlIf, HtmlInAttributeContext, HtmlInAttributeValueContext, HtmlInElementContext, HtmlWhile
1010
};
1111

1212
pub enum Intermediate {
@@ -23,8 +23,17 @@ impl From<HtmlInAttributeContext> for Vec<Intermediate> {
2323
match value {
2424
HtmlInAttributeContext::Literal(key, value) => Vec::from_iter(
2525
[Intermediate::Literal(
26-
" ".to_owned() + &key.to_string(),
27-
key.span(),
26+
" ".to_owned() + &key.pairs()
27+
.map(|p| {
28+
p.value().to_string()
29+
+ match p.punct() {
30+
Some(DashOrColon::Colon(_)) => ":",
31+
Some(DashOrColon::Dash(_)) => "-",
32+
None => "",
33+
}
34+
})
35+
.join(""),
36+
key.first().unwrap().span(),
2837
)]
2938
.into_iter()
3039
.chain(

async-zero-cost-templating-proc-macro2/src/parse.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ use std::{
77
use proc_macro2::{Delimiter, TokenStream, TokenTree};
88
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt};
99
use syn::{
10-
braced, bracketed,
11-
ext::IdentExt,
12-
parenthesized,
13-
parse::{Parse, ParseStream},
14-
spanned::Spanned,
15-
token::{Brace, Bracket, Else, For, If, In, Paren, While},
16-
Ident, LitStr, Token,
10+
braced, bracketed, ext::IdentExt, parenthesized, parse::{Parse, ParseStream}, punctuated::Punctuated, spanned::Spanned, token::{Brace, Bracket, Else, For, If, In, Paren, While}, Ident, LitStr, Token
1711
};
1812
use tracing::instrument;
1913
use tracing::{error, level_filters::LevelFilter};
@@ -197,9 +191,16 @@ pub enum HtmlInAttributeValueContext {
197191
For(HtmlForLoop<Vec<HtmlInAttributeValueContext>>),
198192
}
199193

194+
195+
#[derive(Debug)]
196+
pub enum DashOrColon {
197+
Dash(Token![-]),
198+
Colon(Token![:]),
199+
}
200+
200201
#[derive(Debug)]
201202
pub enum HtmlInAttributeContext {
202-
Literal(Ident, Option<(Token![=], Vec<HtmlInAttributeValueContext>)>),
203+
Literal(Punctuated<Ident, DashOrColon>, Option<(Token![=], Vec<HtmlInAttributeValueContext>)>),
203204
Computation((Brace, TokenStream)),
204205
If(HtmlIf<Vec<HtmlInAttributeContext>>),
205206
While(HtmlWhile<Vec<HtmlInAttributeContext>>),
@@ -422,6 +423,48 @@ impl MyParse<HtmlInAttributeValueContext> for ParseStream<'_> {
422423
}
423424
}
424425

426+
impl MyParse<DashOrColon> for ParseStream<'_> {
427+
fn inner_my_parse(self) -> Result<(DashOrColon, Vec<Diagnostic>), Vec<Diagnostic>> {
428+
let lookahead = self.lookahead1();
429+
let dash_or_colon = if lookahead.peek(Token![-]) {
430+
self.parse().map(DashOrColon::Dash).map_err(|err| Vec::from([Diagnostic::from(err)]))?
431+
} else if lookahead.peek(Token![:]) {
432+
self.parse().map(DashOrColon::Colon).map_err(|err| Vec::from([Diagnostic::from(err)]))?
433+
} else {
434+
return Err(Vec::from([Diagnostic::from(lookahead.error())]))
435+
};
436+
Ok((dash_or_colon, vec![]))
437+
}
438+
}
439+
440+
impl MyParse<Punctuated<Ident, DashOrColon>> for ParseStream<'_> {
441+
fn inner_my_parse(self) -> Result<(Punctuated<Ident, DashOrColon>, Vec<Diagnostic>), Vec<Diagnostic>> {
442+
let mut diagnostics = Vec::new();
443+
let mut ident: Punctuated<Ident, DashOrColon> = Punctuated::new();
444+
ident.push_value({
445+
let value: Ident;
446+
(value, diagnostics) =
447+
MyParse::my_parse(self, identity, identity, diagnostics)?;
448+
value
449+
});
450+
while self.peek(Token![-]) || self.peek(Token![:]) {
451+
ident.push_punct({
452+
let value: DashOrColon;
453+
(value, diagnostics) =
454+
MyParse::my_parse(self, identity, identity, diagnostics)?;
455+
value
456+
});
457+
ident.push_value({
458+
let value: Ident;
459+
(value, diagnostics) =
460+
MyParse::my_parse(self, identity, identity, diagnostics)?;
461+
value
462+
});
463+
}
464+
Ok((ident, diagnostics))
465+
}
466+
}
467+
425468
impl MyParse<HtmlInAttributeContext> for ParseStream<'_> {
426469
#[instrument(err(Debug), ret, name = "Html<Inner>")]
427470
fn inner_my_parse(self) -> Result<(HtmlInAttributeContext, Vec<Diagnostic>), Vec<Diagnostic>> {
@@ -452,6 +495,7 @@ impl MyParse<HtmlInAttributeContext> for ParseStream<'_> {
452495
diagnostics,
453496
)?)
454497
} else if lookahead.peek(Ident::peek_any) {
498+
// here
455499
Ok((
456500
HtmlInAttributeContext::Literal(
457501
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extern crate alloc;
2+
3+
use async_zero_cost_templating::html;
4+
use async_zero_cost_templating::TemplateToStream;
5+
use core::pin::pin;
6+
use futures_util::stream::StreamExt;
7+
8+
#[tokio::test]
9+
async fn test() {
10+
let stream = html! {
11+
<a aria-current="true">
12+
</a>
13+
};
14+
let result: String = stream.collect().await;
15+
assert_eq!(result, r#"<a aria-current="true"></a>"#)
16+
}

0 commit comments

Comments
 (0)