Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ license = "MIT"
render_macros = { path = "../render_macros", version = "0.3.1" }

[dev-dependencies]
pretty_assertions = "0.6"
pretty_assertions = "1.4"
1 change: 1 addition & 0 deletions render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,5 @@ pub use self::render::Render;
pub use fragment::Fragment;
pub use render_macros::{component, html, rsx};
pub use simple_element::SimpleElement;
pub use simple_element::ToAttribute;
pub use text_element::Raw;
77 changes: 70 additions & 7 deletions render/src/simple_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,68 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{Result, Write};

type Attributes<'a> = Option<HashMap<&'a str, Cow<'a, str>>>;
type AV<'a> = Option<Cow<'a, str>>;

pub trait ToAttribute<'a> {
fn from_value(self) -> AV<'a>;
}

impl<'a> ToAttribute<'a> for Option<Cow<'a, str>> {
fn from_value(self) -> AV<'a> {
self
}
}

impl<'a> ToAttribute<'a> for () {
fn from_value(self) -> AV<'a> {
None
}
}

impl<'a> ToAttribute<'a> for String {
fn from_value(self) -> AV<'a> {
Some(Cow::Owned(self))
}
}

impl<'a> ToAttribute<'a> for &'a str {
fn from_value(self) -> AV<'a> {
Some(Cow::Borrowed(self))
}
}

impl<'a> ToAttribute<'a> for Option<&'a str> {
fn from_value(self) -> AV<'a> {
self.map(|v| Cow::Borrowed(v))
}
}

impl<'a> ToAttribute<'a> for Option<String> {
fn from_value(self) -> AV<'a> {
self.map(|v| Cow::Owned(v))
}
}

macro_rules! impl_primitive {
[$($num: ty),+] => {
$(
impl<'a> ToAttribute<'a> for $num {
fn from_value(self) -> AV<'a> {
Some(Cow::Owned(self.to_string()))
}
}
)+
};
}
impl_primitive![u8, u16, u32, u64, i8, i16, i32, i64, f32, f64, bool];

impl<'a> ToAttribute<'a> for Cow<'a, str> {
fn from_value(self) -> AV<'a> {
Some(self)
}
}

type Attributes<'a> = Option<HashMap<&'a str, AV<'a>>>;

/// Simple HTML element tag
#[derive(Debug)]
Expand All @@ -15,14 +76,16 @@ pub struct SimpleElement<'a, T: Render> {
pub contents: Option<T>,
}

fn write_attributes<'a, W: Write>(maybe_attributes: Attributes<'a>, writer: &mut W) -> Result {
match maybe_attributes {
fn write_attributes<'a, W: Write>(attributes: Attributes<'a>, writer: &mut W) -> Result {
match attributes {
None => Ok(()),
Some(mut attributes) => {
for (key, value) in attributes.drain() {
write!(writer, " {}=\"", key)?;
escape_html(&value, writer)?;
write!(writer, "\"")?;
for (key, maybe_value) in attributes.drain() {
if let Some(value) = maybe_value {
write!(writer, " {}=\"", key)?;
escape_html(&value, writer)?;
write!(writer, "\"")?;
}
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions render_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ license = "MIT"
proc-macro = true

[dependencies]
syn = { version = "1.0", features = ["full"] }
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
proc-macro-error = "1.0"

[dev-dependencies]
render = { path = "../render", version = "0.3" }
pretty_assertions = "0.6"
pretty_assertions = "1.4"
2 changes: 2 additions & 0 deletions render_macros/src/element_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ impl Parse for ElementAttribute {
}

input.parse::<syn::Token![=]>()?;
// TODO: Add support for literals
// TODO: Add support for bool expr disable attribute
let value = input.parse::<syn::Block>()?;

Ok(Self::WithValue(name, value))
Expand Down
4 changes: 2 additions & 2 deletions render_macros/src/element_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ impl<'a> ToTokens for SimpleElementAttributes<'a> {
let value = attribute.value_tokens();

quote! {
hm.insert(#ident, ::std::borrow::Cow::from(#value));
hm.insert(#ident, ::render::ToAttribute::from_value(#value));
}
})
.collect();

let hashmap_declaration = quote! {{
let mut hm = std::collections::HashMap::<&str, ::std::borrow::Cow<'_, str>>::new();
let mut hm = std::collections::HashMap::<&str, Option<::std::borrow::Cow<'_, str>>>::new();
#(#attrs)*
Some(hm)
}};
Expand Down
4 changes: 2 additions & 2 deletions render_macros/src/function_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub fn create_function_component(f: syn::ItemFn) -> TokenStream {

TokenStream::from(quote! {
#[derive(Debug)]
#vis struct #struct_name#impl_generics #inputs_block
#vis struct #struct_name #impl_generics #inputs_block

impl#impl_generics ::render::Render for #struct_name #ty_generics #where_clause {
impl #impl_generics ::render::Render for #struct_name #ty_generics #where_clause {
fn render_into<W: std::fmt::Write>(self, w: &mut W) -> std::fmt::Result {
let result = {
#inputs_reading
Expand Down
2 changes: 1 addition & 1 deletion render_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ repository = "https://github.com/Schniz/render.rs"
render = { path = "../render" }

[dev-dependencies]
pretty_assertions = "0.6"
pretty_assertions = "1.4"
trybuild = "1.0"