Skip to content

Commit fea47ed

Browse files
Merge pull request #213 from David-OConnor/style_enums
Style enums
2 parents e8e6d44 + 923af19 commit fea47ed

File tree

13 files changed

+697
-122
lines changed

13 files changed

+697
-122
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# --- Files ---
32
**/*.ts
43
**.wasm

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.4.1
4+
- Added more SVG `At` variants
5+
- Added the `St` enum, for style keys; similar to `At`
6+
- Improved ergonomics of `add_child`, `add_attr`, `add_class`,
7+
`add_style`, `replace_text`, and `add_text`, `Node` methods
8+
39
## v0.4.0
410
- `ElContainer`, imported in prelude, renamed to `View`. (Breaking)
511
- Internal refactor of `El`: Now wrapped in `Node`, along with

Cargo.lock

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile.toml

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
# https://github.com/rust-lang/rustup.rs/blob/d35f94183601a81489bce025c2abc35cd395c909/src/toolchain.rs#L296
44
RUST_RECURSION_COUNT = "0"
55

6+
# ---- SCRIPTS ----
7+
8+
[tasks.populate_all]
9+
description = "Populate styles, tags, attributes, etc."
10+
workspace = false
11+
dependencies = ["populate_styles"]
12+
13+
[tasks.populate_styles]
14+
description = "Populate styles"
15+
workspace = false
16+
command = "cargo"
17+
args = ["script", "./scripts/populate_styles.rs"]
18+
619
# ---- GENERAL ----
720

821
[tasks.verify]

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ And cargo-make: `cargo install --force cargo-make`
2727
To start, clone [the quickstart repo](https://github.com/David-OConnor/seed-quickstart):
2828
`git clone https://github.com/david-oconnor/seed-quickstart.git`,
2929
run `cargo make build` in a terminal to build the app, and `cargo make serve` to start a dev server
30-
on `127.0.0.0:8000`. If you'd like the compiler automatically check and recompile when you
31-
make changes, run `cargo make watch` instead of `cargo make all`.
30+
on `127.0.0.0:8000`. If you'd like the compiler to automatically check and recompile when you
31+
make changes, run `cargo make watch` instead of `cargo make build`.
3232

3333

3434
## A little deeper
@@ -150,27 +150,27 @@ fn view(model: &Model) -> impl View<Msg> {
150150

151151
// Attrs, Style, Events, and children may be defined separately.
152152
let outer_style = style!{
153-
"display" => "flex";
154-
"flex-direction" => "column";
155-
"text-align" => "center"
153+
St::Display => "flex";
154+
St::FlexDirection => "column";
155+
St::TextAlign => "center"
156156
};
157157

158158
div![ outer_style,
159159
h1![ "The Grand Total" ],
160160
div![
161161
style!{
162162
// Example of conditional logic in a style.
163-
"color" => if model.count > 4 {"purple"} else {"gray"};
164-
"border" => "2px solid #004422";
165-
"padding" => unit!(20, px);
163+
St::Color => if model.count > 4 {"purple"} else {"gray"};
164+
St::Border => "2px solid #004422";
165+
St::Padding => unit!(20, px);
166166
},
167167
// We can use normal Rust code and comments in the view.
168168
h3![ format!("{} {}{} so far", model.count, model.what_we_count, plural) ],
169169
button![ simple_ev(Ev::Click, Msg::Increment), "+" ],
170170
button![ simple_ev(Ev::Click, Msg::Decrement), "-" ],
171171

172172
// Optionally-displaying an element
173-
if model.count >= 10 { h2![ style!{"padding" => px(50)}, "Nice!" ] } else { empty![] }
173+
if model.count >= 10 { h2![ style!{St::Padding => px(50)}, "Nice!" ] } else { empty![] }
174174
],
175175
success_level(model.count), // Incorporating a separate component
176176

RELEASE_CHECKLIST.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ for updating the homepage are available [here](https://github.com/David-OConnor/
1111
to reflect API changes, and the new version
1212
1. Ensure the version listed in `Cargo.toml` is updated
1313
1. Update Rust tools: `rustup update`
14+
1. Run `cargo make populate_all` to synchronize `St`, `At` and other enums with official values
1415
1. Run `cargo make verify` to ensure tests pass, and `clippy` / `fmt` are run
1516
1. Commit and push the repo
1617
1. Check that CI pipeline passed

examples/orders/src/lib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ fn write_emoticon_after_delay(emoticon: String) -> impl Future<Item = Msg, Error
6767
fn view(model: &Model) -> impl View<Msg> {
6868
div![
6969
style![
70-
"display" => "flex",
71-
"justify-content" => "center",
72-
"align-items" => "center",
73-
"font-size" => vmin(5),
74-
"font-family" => "sans-serif",
75-
"height" => vmin(50),
70+
St::Display => "flex",
71+
St::JustifyContent => "center",
72+
St::AlignItems => "center",
73+
St::FontSize => vmin(5),
74+
St::FontFamily => "sans-serif",
75+
St::Height => vmin(50),
7676
],
7777
if model.greet_clicked {
7878
h1![model.title]
7979
} else {
8080
div![
8181
style![
82-
"background-color" => "lightgreen",
83-
"padding" => vmin(3),
84-
"border-radius" => vmin(3),
85-
"cursor" => "pointer",
86-
"box-shadow" => [vmin(0), vmin(0.5), vmin(0.5), "green".into()].join(" "),
82+
St::BackgroundColor => "lightgreen",
83+
St::Padding => vmin(3),
84+
St::BorderRadius => vmin(3),
85+
St::Cursor => "pointer",
86+
St::BoxShadow => [vmin(0), vmin(0.5), vmin(0.5), "green".into()].join(" "),
8787
],
8888
simple_ev(Ev::Click, Msg::Greet),
8989
"Greet!"

scripts/populate_styles.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//! ```cargo
2+
//! [dependencies]
3+
//! reqwest = "^0.9.20"
4+
//! serde = { version = "^1.0.92", features = ["derive"] }
5+
//! ```
6+
7+
//! Populate CSS styles
8+
9+
extern crate reqwest;
10+
extern crate serde;
11+
12+
use serde::Deserialize;
13+
use std::fs;
14+
15+
const STYLE_NAMES_FILE: &str = "./src/dom_entity_names/styles/style_names.rs";
16+
const STYLES_ENDPOINT: &str = "https://martinkavik.github.io/html-css-db/css_properties.json";
17+
18+
#[derive(Debug, Deserialize)]
19+
struct Style {
20+
name: StyleName
21+
}
22+
23+
#[derive(Debug, Deserialize)]
24+
struct StyleName {
25+
original: String,
26+
pascal_case: String,
27+
}
28+
29+
fn main() {
30+
let styles = fetch_styles();
31+
let content_for_style_names_file = generate_content_for_style_names_file(styles);
32+
fs::write(STYLE_NAMES_FILE, content_for_style_names_file)
33+
.expect("Writing into style names file failed.")
34+
}
35+
36+
/// Json example:
37+
///
38+
/// ```
39+
/// [
40+
/// {
41+
/// "name": {
42+
/// "original": "justify-content",
43+
/// "pascal_case": "JustifyContent"
44+
/// }
45+
/// }
46+
/// ]
47+
/// ```
48+
fn fetch_styles() -> Vec<Style> {
49+
reqwest::get(STYLES_ENDPOINT)
50+
.expect("Request to styles endpoints failed.")
51+
.json()
52+
.expect("Problem parsing CSS properties as JSON.")
53+
}
54+
55+
/// Example of generated content:
56+
///
57+
/// ```rust,no_run
58+
/// //! This file is generated automatically by `/scripts/populate_styles.rs`.
59+
/// //! It's not meant to be edited directly.
60+
///
61+
/// make_styles! {
62+
/// Display => "display",
63+
/// JustifyContent => "justify-content",
64+
/// }
65+
/// ```
66+
fn generate_content_for_style_names_file(styles: Vec<Style>) -> String {
67+
let style_pairs: String = styles
68+
.into_iter()
69+
.filter(|style| !style.name.pascal_case.is_empty())
70+
.map(|style| format!(" {} => \"{}\",\n", style.name.pascal_case, style.name.original))
71+
.collect();
72+
73+
format!(r#"//! This file is generated automatically by `/scripts/populate_styles.rs`.
74+
//! It's not meant to be edited directly.
75+
76+
make_styles! {{
77+
{}
78+
}}
79+
"#, style_pairs.trim().trim_end_matches(','))
80+
}

src/dom_entity_names/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod styles;

src/dom_entity_names/styles.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// Similar to tag population.
2+
macro_rules! make_styles {
3+
// Create shortcut macros for any style; populate these functions in the submodule.
4+
{ $($st_pascal_case:ident => $st:expr),+ } => {
5+
6+
/// The St enum restricts element-creation to only valid styles.
7+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8+
pub enum St {
9+
$(
10+
$st_pascal_case,
11+
)+
12+
Custom(String)
13+
}
14+
15+
impl St {
16+
pub fn as_str(&self) -> &str {
17+
match self {
18+
$ (
19+
St::$st_pascal_case => $st,
20+
) +
21+
St::Custom(val) => &val
22+
}
23+
}
24+
}
25+
26+
impl From<&str> for St {
27+
fn from(st: &str) -> Self {
28+
match st {
29+
$ (
30+
$st => St::$st_pascal_case,
31+
) +
32+
_ => {
33+
crate::error(&format!("Can't find this style: {}", st));
34+
St::Custom(st.to_owned())
35+
}
36+
}
37+
}
38+
}
39+
impl From<String> for St {
40+
fn from(st: String) -> Self {
41+
match st.as_ref() {
42+
$ (
43+
$st => St::$st_pascal_case,
44+
) +
45+
_ => {
46+
crate::error(&format!("Can't find this style: {}", st));
47+
St::Custom(st)
48+
}
49+
}
50+
}
51+
}
52+
53+
}
54+
}
55+
56+
mod style_names;
57+
pub use style_names::St;

0 commit comments

Comments
 (0)