Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow boolean & update error for byte literals in html! #3441

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 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,18 @@ 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\nnote: remove the `b` prefix",
its-the-shrimp marked this conversation as resolved.
Show resolved Hide resolved
))
}
Lit::Verbatim(lit) => {
return Err(syn::Error::new(lit.span(), "unsupported literal"))
}
_ => (),
}
HtmlNode::Literal(Box::new(lit))
} else {
Expand Down
27 changes: 17 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,31 @@ 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(),
its-the-shrimp marked this conversation as resolved.
Show resolved Hide resolved
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::ByteStr(v) => String::from_utf8(v.value()).ok()?.into(),
its-the-shrimp marked this conversation as resolved.
Show resolved Hide resolved
Lit::Byte(v) => v.value().to_string().into(),
Copy link
Member

Choose a reason for hiding this comment

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

This is similar to ByteStr, however since a byte is u8 and u8 supports Display.
So I am not sure not allowing b'A' would be effective.

Lit::Verbatim(v) => v.to_string().into(),
its-the-shrimp marked this conversation as resolved.
Show resolved Hide resolved
_ => 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 +115,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
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
--> 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
--> 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