Skip to content

Commit 950321a

Browse files
committed
Constants not enum WIP.
1 parent 028cc7d commit 950321a

File tree

3 files changed

+80
-70
lines changed

3 files changed

+80
-70
lines changed

bootstrap

Submodule bootstrap updated 903 files

src/generator.rs

+10-39
Original file line numberDiff line numberDiff line change
@@ -150,42 +150,17 @@ fn main() {
150150

151151
let variant_name = name.to_case(Case::UpperCamel);
152152
let variant = to_ident(&variant_name);
153-
variants.push(quote! {
154-
#[cfg(feature = #variant_name)]
155-
#variant
156-
});
157-
158-
collection_feature.children.push(variant_name.clone());
159-
features.push(Feature {
160-
name: variant_name.clone(),
161-
children: Vec::new(),
162-
});
163-
164-
// Don't need when export separate mods #[cfg(feature = #variant_name)]
165-
let tokens = quote! {
166-
use crate::IconProps;
167-
168-
#[inline(never)]
169-
pub fn #function_ident(IconProps{icon_id: _, title, width, height, onclick, oncontextmenu, class, style, role}: &IconProps) -> yew::Html {
170-
yew::html! {
171-
#svg_tokens
172-
}
173-
}
174-
};
175-
176-
let output = tokens.to_string(); // reformat(tokens.to_string(), true).unwrap();
177-
write(
178-
format!("src/generated/{}/{}.rs", feature_name, function_name),
179-
output,
180-
)
181-
.unwrap();
182-
183-
function_mods.push(quote! {
184-
#[cfg(feature = #variant_name)]
185-
pub mod #function_ident;
186-
});
187153

188154
cases.push(quote! {
155+
const #variant: IconData = IconData {
156+
name: #variant_name,
157+
html: #[inline(never)] |IconProps{icon_id: _, title, width, height, onclick, oncontextmenu, class, style, role}: &IconProps| -> yew::Html {
158+
yew::html! {
159+
#svg_tokens
160+
}
161+
},
162+
};
163+
189164
#[cfg(feature = #variant_name)]
190165
IconId::#variant => #feature_ident::#function_ident::#function_ident(props)
191166
});
@@ -294,11 +269,7 @@ fn main() {
294269
"simple-icons/icons",
295270
r##"From https://github.com/simple-icons/simple-icons - Licensed under CC0; check brand guidelines"##,
296271
);
297-
generate(
298-
"Extra",
299-
"extra",
300-
r##"Check brand guidelines"##,
301-
);
272+
generate("Extra", "extra", r##"Check brand guidelines"##);
302273

303274
let tokens = quote! {
304275
/// Identifies which icon to render. Variants are all disabled by default, but can be

src/lib.rs

+69-30
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,64 @@
22

33
#[cfg(not(feature = "generator"))]
44
mod generated;
5-
#[cfg(not(feature = "generator"))]
6-
pub use generated::{get_svg, IconId};
5+
6+
use std::cmp::Ordering;
7+
use std::fmt::Debug;
78

89
#[cfg(not(feature = "generator"))]
910
use yew::prelude::*;
1011

1112
#[cfg(not(feature = "generator"))]
1213
use yew::virtual_dom::AttrValue;
1314

15+
#[derive(Copy, Clone)]
16+
pub struct IconData {
17+
name: &'static str,
18+
html: fn(props: &IconProps) -> Html,
19+
}
20+
21+
impl IconData {
22+
const HELLO_WORLD: IconData = IconData {
23+
name: "HelloWorld",
24+
html: |props: &IconProps| {
25+
html! {
26+
<p>{"Hello world"}</p>
27+
}
28+
},
29+
};
30+
}
31+
32+
impl PartialEq for IconData {
33+
fn eq(&self, other: &Self) -> bool {
34+
self.name.eq(other.name)
35+
}
36+
}
37+
impl Eq for IconData {}
38+
39+
impl Ord for IconData {
40+
fn cmp(&self, other: &Self) -> Ordering {
41+
self.name.cmp(other.name)
42+
}
43+
}
44+
45+
impl PartialOrd for IconData {
46+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
47+
Some(self.cmp(other))
48+
}
49+
}
50+
51+
impl Debug for IconData {
52+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53+
f.write_str(self.name)
54+
}
55+
}
56+
1457
/// For customizing icon rendering. Only `icon_id` is required.
1558
#[cfg(not(feature = "generator"))]
1659
#[derive(Properties, PartialEq)]
1760
pub struct IconProps {
18-
/// Which icon to render. Enable icons with feature flags.
19-
pub icon_id: IconId,
61+
/// Which icon to render. Enable icon collections with feature flags.
62+
pub data: IconData,
2063
/// Tooltip text.
2164
#[prop_or(None)]
2265
pub title: Option<AttrValue>,
@@ -37,7 +80,7 @@ pub struct IconProps {
3780
pub class: Classes,
3881
/// For inline CSS.
3982
#[prop_or(None)]
40-
pub style: Option<AttrValue>,
83+
pub style: Option<AttrValue>,
4184
#[prop_or(None)]
4285
pub role: Option<AttrValue>,
4386
}
@@ -61,38 +104,34 @@ pub struct IconProps {
61104
#[cfg(not(feature = "generator"))]
62105
#[function_component(Icon)]
63106
pub fn icon(props: &IconProps) -> Html {
64-
get_svg(props)
107+
(props.data.html)(props)
65108
}
66109

67110
#[cfg(test)]
68111
mod test {
69-
use crate::{Icon, IconId, IconProps};
70-
use enum_iterator::IntoEnumIterator;
112+
use crate::{Icon, IconData, IconProps};
71113
use yew::prelude::*;
72114

73115
#[tokio::test]
74116
async fn test() {
75-
for icon_id in IconId::into_enum_iter() {
76-
println!("rendering icon {:?}", icon_id);
77-
let icon_id = icon_id.clone();
78-
let renderer = yew::ServerRenderer::<Icon>::with_props(move || IconProps {
79-
icon_id,
80-
width: "2em".into(),
81-
height: "3em".into(),
82-
onclick: Some(Callback::from(|_e: MouseEvent| {})),
83-
class: Classes::new(),
84-
oncontextmenu: None,
85-
style: None,
86-
title: None,
87-
role: Some("presentation".into()),
88-
});
89-
90-
let rendered = renderer.render().await;
91-
92-
assert!(rendered.contains("2em"), "{:?} {}", icon_id, rendered);
93-
assert!(rendered.contains("3em"), "{:?} {}", icon_id, rendered);
94-
95-
//println!("{:?} => {}", icon_id, rendered);
96-
}
117+
let data = IconData::HELLO_WORLD;
118+
let renderer = yew::ServerRenderer::<Icon>::with_props(move || IconProps {
119+
data,
120+
width: "2em".into(),
121+
height: "3em".into(),
122+
onclick: Some(Callback::from(|_e: MouseEvent| {})),
123+
class: Classes::new(),
124+
oncontextmenu: None,
125+
style: None,
126+
title: None,
127+
role: Some("presentation".into()),
128+
});
129+
130+
let rendered = renderer.render().await;
131+
132+
assert!(rendered.contains("2em"), "{data:?} {}", rendered);
133+
assert!(rendered.contains("3em"), "{data:?} {}", rendered);
134+
135+
//println!("{:?} => {}", icon_id, rendered);
97136
}
98137
}

0 commit comments

Comments
 (0)