Skip to content

Commit

Permalink
Move element types into a separate module
Browse files Browse the repository at this point in the history
This avoids conflicts with names like `Option` if importing `el::*`.
  • Loading branch information
kmicklas committed Jun 29, 2024
1 parent fa9d2de commit b168abd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gloo-utils = "0.2.0"
log = "0.4.21"
paste = "1.0.15"
ravel = { version = "0.2.0", path = "./ravel" }
ravel-web = { version = "0.3.0", path = "./ravel-web" }
ravel-web = { version = "0.4.0", path = "./ravel-web" }
wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"
web-sys = "0.3.69"
2 changes: 1 addition & 1 deletion ravel-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ravel-web"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
description = "An experimental approach to UI in Rust with a focus on ergonomics, efficiency, and simplicity."
license = "MIT"
Expand Down
13 changes: 10 additions & 3 deletions ravel-web/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ fn main() {
for name in config.element.keys() {
let t = title_case(name);
writeln!(&mut src, "make_el!({name}, {t}, create_{name}());").unwrap();
}

std::fs::write(out_dir.join("gen_el_types.rs"), src).unwrap();

let mut src = String::new();

// Ideally this would be part of `make_el`, but rust-analyzer can't
for name in config.element.keys() {
let t = title_case(name);
// Ideally this would be generated by a macro, but rust-analyzer can't
// seem to handle doc attributes generated by a macro generated by a
// build script.
writeln!(&mut src, "/// [`<{name}>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/{name}) element.").unwrap();
writeln!(
&mut src,
"pub fn {name}<Body>(body: Body) -> {t}<Body> {{ {t}(body) }}"
"pub fn {name}<Body>(body: Body) -> types::{t}<Body> {{ types::{t}(body) }}"
)
.unwrap();
}

std::fs::write(out_dir.join("el_gen.rs"), src).unwrap();
std::fs::write(out_dir.join("gen_el.rs"), src).unwrap();

println!("cargo::rerun-if-changed=generate.toml");
}
Expand Down
17 changes: 17 additions & 0 deletions ravel-web/src/el/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! HTML elements.

use std::marker::PhantomData;

use self::types::{El, ElKind};

pub mod types;

/// An arbitrary element.
pub fn el<Kind: ElKind, Body>(_: Kind, body: Body) -> El<Kind, Body> {
El {
kind: PhantomData,
body,
}
}

include!(concat!(env!("OUT_DIR"), "/gen_el.rs"));
18 changes: 6 additions & 12 deletions ravel-web/src/el.rs → ravel-web/src/el/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! HTML elements.
//! HTML element types.
//!
//! Usually you shouldn't need to import or reference these directly.

use std::marker::PhantomData;

Expand All @@ -17,8 +19,8 @@ pub trait ElKind: 'static {
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct El<Kind: ElKind, Body> {
kind: PhantomData<Kind>,
body: Body,
pub(crate) kind: PhantomData<Kind>,
pub(crate) body: Body,
}

impl<Kind: ElKind, Body: Builder<Web>> Builder<Web> for El<Kind, Body> {
Expand Down Expand Up @@ -56,14 +58,6 @@ where

impl<S> ViewMarker for ElState<S> {}

/// An arbitrary element.
pub fn el<Kind: ElKind, Body>(_: Kind, body: Body) -> El<Kind, Body> {
El {
kind: PhantomData,
body,
}
}

fn create_element(kind: &'static str) -> web_sys::Element {
gloo_utils::document().create_element(kind).unwrap_throw()
}
Expand Down Expand Up @@ -122,4 +116,4 @@ macro_rules! make_el {
};
}

include!(concat!(env!("OUT_DIR"), "/el_gen.rs"));
include!(concat!(env!("OUT_DIR"), "/gen_el_types.rs"));

0 comments on commit b168abd

Please sign in to comment.