Skip to content

Commit

Permalink
Allow boolean & update error for byte literals in html! (#3441)
Browse files Browse the repository at this point in the history
* made byte & boolean literals accepted by html

* improved error message, outruled byte-string to HTML conversion

* update tests
  • Loading branch information
its-the-shrimp authored Oct 12, 2024
1 parent 197e2d5 commit 4978b99
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
16 changes: 13 additions & 3 deletions packages/yew-macro/src/html_tree/html_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ pub enum HtmlNode {
impl Parse for HtmlNode {
fn parse(input: ParseStream) -> Result<Self> {
let node = if HtmlNode::peek(input.cursor()).is_some() {
let lit: Lit = input.parse()?;
if matches!(lit, Lit::ByteStr(_) | Lit::Byte(_) | Lit::Verbatim(_)) {
return Err(syn::Error::new(lit.span(), "unsupported type"));
let lit = input.parse()?;
match lit {
Lit::ByteStr(lit) => {
return Err(syn::Error::new(
lit.span(),
"byte-strings can't be converted to HTML text
note: remove the `b` prefix or convert this to a `String`",
))
}
Lit::Verbatim(lit) => {
return Err(syn::Error::new(lit.span(), "unsupported literal"))
}
_ => (),
}
HtmlNode::Literal(Box::new(lit))
} else {
Expand Down
26 changes: 16 additions & 10 deletions packages/yew-macro/src/stringify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::borrow::Cow;
use std::mem::size_of;

use proc_macro2::{Span, TokenStream};
use quote::{quote_spanned, ToTokens};
use syn::spanned::Spanned;
Expand Down Expand Up @@ -75,26 +78,30 @@ impl Stringify for LitStr {
}
}
}

impl Stringify for Lit {
fn try_into_lit(&self) -> Option<LitStr> {
let s = match self {
let mut buf = [0; size_of::<char>()];
let s: Cow<'_, str> = match self {
Lit::Str(v) => return v.try_into_lit(),
Lit::Char(v) => v.value().to_string(),
Lit::Int(v) => v.base10_digits().to_string(),
Lit::Float(v) => v.base10_digits().to_string(),
Lit::Bool(_) | Lit::ByteStr(_) | Lit::Byte(_) | Lit::Verbatim(_) => return None,
_ => unreachable!("unknown Lit"),
Lit::Char(v) => (&*v.value().encode_utf8(&mut buf)).into(),
Lit::Int(v) => v.base10_digits().into(),
Lit::Float(v) => v.base10_digits().into(),
Lit::Bool(v) => if v.value() { "true" } else { "false" }.into(),
Lit::Byte(v) => v.value().to_string().into(),
Lit::Verbatim(_) | Lit::ByteStr(_) => return None,
_ => unreachable!("unknown Lit {:?}", self),
};
Some(LitStr::new(&s, self.span()))
}

fn stringify(&self) -> TokenStream {
self.try_into_lit()
.as_ref()
.map(Stringify::stringify)
.unwrap_or_else(|| stringify_at_runtime(self))
.map_or_else(|| stringify_at_runtime(self), Stringify::stringify)
}
}

impl Stringify for Expr {
fn try_into_lit(&self) -> Option<LitStr> {
if let Expr::Lit(v) = self {
Expand All @@ -107,7 +114,6 @@ impl Stringify for Expr {
fn stringify(&self) -> TokenStream {
self.try_into_lit()
.as_ref()
.map(Stringify::stringify)
.unwrap_or_else(|| stringify_at_runtime(self))
.map_or_else(|| stringify_at_runtime(self), Stringify::stringify)
}
}
18 changes: 4 additions & 14 deletions packages/yew-macro/tests/html_macro/node-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@ error: unexpected token, expected `}`
5 | html! { <span>{ "valid" "invalid" }</span> };
| ^^^^^^^^^

error: unsupported type
--> tests/html_macro/node-fail.rs:10:14
|
10 | html! { b'a' };
| ^^^^

error: unsupported type
error: byte-strings can't be converted to HTML text
note: remove the `b` prefix or convert this to a `String`
--> tests/html_macro/node-fail.rs:11:14
|
11 | html! { b"str" };
| ^^^^^^

error: unsupported type
--> tests/html_macro/node-fail.rs:12:22
|
12 | html! { <span>{ b'a' }</span> };
| ^^^^

error: unsupported type
error: byte-strings can't be converted to HTML text
note: remove the `b` prefix or convert this to a `String`
--> tests/html_macro/node-fail.rs:13:22
|
13 | html! { <span>{ b"str" }</span> };
Expand Down
8 changes: 4 additions & 4 deletions packages/yew-macro/tests/html_macro/node-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ pub struct u8;
pub struct usize;

fn main() {
_ = ::yew::html! { "" };
_ = ::yew::html! { b'b' };
_ = ::yew::html! { 'a' };
_ = ::yew::html! { "hello" };
_ = ::yew::html! { "42" };
_ = ::yew::html! { "1.234" };
_ = ::yew::html! { "true" };
_ = ::yew::html! { 42 };
_ = ::yew::html! { 1.234 };
_ = ::yew::html! { true };

_ = ::yew::html! { <span>{ "" }</span> };
_ = ::yew::html! { <span>{ 'a' }</span> };
Expand Down

1 comment on commit 4978b99

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: 4978b99 Previous: 197e2d5 Ratio
yew-hooks-v0.21.0-keyed 01_run1k 186.1 200.6 0.93
yew-hooks-v0.21.0-keyed 02_replace1k 211.3 232.4 0.91
yew-hooks-v0.21.0-keyed 03_update10th1k_x16 86.3 101 0.85
yew-hooks-v0.21.0-keyed 04_select1k 38.1 43 0.89
yew-hooks-v0.21.0-keyed 05_swap1k 97.6 112.3 0.87
yew-hooks-v0.21.0-keyed 06_remove-one-1k 76 85.6 0.89
yew-hooks-v0.21.0-keyed 07_create10k 2171 2276.6 0.95
yew-hooks-v0.21.0-keyed 08_create1k-after1k_x2 211.9 230.8 0.92
yew-hooks-v0.21.0-keyed 09_clear1k_x8 82.5 90.8 0.91
yew-hooks-v0.21.0-keyed 21_ready-memory 2.1226301193237305 2.1483068466186523 0.99
yew-hooks-v0.21.0-keyed 22_run-memory 6.260809898376465 6.259356498718262 1.00
yew-hooks-v0.21.0-keyed 23_update5-memory 6.571136474609375 6.62249755859375 0.99
yew-hooks-v0.21.0-keyed 25_run-clear-memory 5.14837646484375 5.147143363952637 1.00
yew-hooks-v0.21.0-keyed 26_run-10k-memory 42.82802867889404 42.795040130615234 1.00
yew-hooks-v0.21.0-keyed 41_size-uncompressed 168 168 1
yew-hooks-v0.21.0-keyed 42_size-compressed 54.6 54.6 1
yew-hooks-v0.21.0-keyed 43_first-paint 440.2 455.9 0.97
yew-v0.21.0-keyed 01_run1k 188.6 196 0.96
yew-v0.21.0-keyed 02_replace1k 209.4 222.7 0.94
yew-v0.21.0-keyed 03_update10th1k_x16 64.6 72.4 0.89
yew-v0.21.0-keyed 04_select1k 14.8 17.8 0.83
yew-v0.21.0-keyed 05_swap1k 73.3 83.7 0.88
yew-v0.21.0-keyed 06_remove-one-1k 63.5 73.6 0.86
yew-v0.21.0-keyed 07_create10k 2153.4 2247.7 0.96
yew-v0.21.0-keyed 08_create1k-after1k_x2 205.6 223.9 0.92
yew-v0.21.0-keyed 09_clear1k_x8 82.7 91 0.91
yew-v0.21.0-keyed 21_ready-memory 2.1184768676757812 2.133543014526367 0.99
yew-v0.21.0-keyed 22_run-memory 6.225220680236816 6.247433662414551 1.00
yew-v0.21.0-keyed 23_update5-memory 6.371580123901367 6.338129043579102 1.01
yew-v0.21.0-keyed 25_run-clear-memory 4.891103744506836 4.954008102416992 0.99
yew-v0.21.0-keyed 26_run-10k-memory 41.544267654418945 41.54412651062012 1.00
yew-v0.21.0-keyed 41_size-uncompressed 166 166 1
yew-v0.21.0-keyed 42_size-compressed 54.4 54.4 1
yew-v0.21.0-keyed 43_first-paint 433 427.5 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.