Skip to content

Commit

Permalink
switch to html5ever and markup5ever parser
Browse files Browse the repository at this point in the history
- html parser
- mv rustfmt ignore to attr on module
  • Loading branch information
DougAnderson444 committed Jan 13, 2025
1 parent 8c5527a commit 21cf9b8
Show file tree
Hide file tree
Showing 21 changed files with 1,024 additions and 721 deletions.
528 changes: 241 additions & 287 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ members = [
".",
"crates/html-egui-bindgen",
"crates/html-to-egui",
"crates/validate_rhai_proc_macro",
"examples/counter",
"examples/datetime",
"examples/login",
Expand All @@ -16,7 +15,7 @@ version = "0.2.1"
authors = ["Doug Anderson <doug@peerpiper.io>"]
edition = "2021"
include = ["LICENSE-APACHE", "LICENSE-MIT", "**/*.rs", "Cargo.toml"]
rust-version = "1.78" # Minimum version required to build this project, for wasm-wsip1 target, 1.78 minimum is required.
rust-version = "1.78"
default-run = "rdx"

[package.metadata.docs.rs]
Expand Down Expand Up @@ -52,8 +51,11 @@ anyhow = "1.0.93"
futures-lite = "2.5.0"
send_wrapper = "0.6"
parking_lot = "0.12"
scraper = { version = "0.22", default-features = false }
html5ever = "0.27"
markup5ever_rcdom = "0.3.0"
markup5ever = "0.12.0"
html-to-egui = { path = "crates/html-to-egui" }
ahash = "0.8.11"

# For native builds:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -74,13 +76,13 @@ js_wasm_runtime_layer = "0.4.0"
anyhow = "1.0"
send_wrapper = { version = "0.6.0", features = ["futures"] }
web-time = "1.1.0"
lol_alloc = "0.4.1"

[build-dependencies]
wasmparser = "0.221"

[dev-dependencies]
html-egui-bindgen = { path = "crates/html-egui-bindgen" }
test-log = "0.2.16"

[profile.release]
opt-level = 2 # fast and small wasm
Expand Down
21 changes: 21 additions & 0 deletions crates/html-to-egui/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Formatter};

use super::*;

/// Enumerate the action handlers, such as on-click, on-change, etc.
Expand All @@ -9,11 +11,18 @@ use super::*;
/// This enum enumerates the 'on-click', 'on-change', etc. so that
/// the html crate can use this enum to build the html text programmatically
/// in a type safe way, without typo errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
OnClick,
OnChange,
}

impl Display for Action {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

impl Action {
// Define constants for the attribute strings
const ON_CLICK: &'static str = "on-click";
Expand All @@ -28,6 +37,18 @@ impl Action {
}
}

impl TryFrom<&str> for Action {
type Error = Error;

fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
Action::ON_CLICK => Ok(Action::OnClick),
Action::ON_CHANGE => Ok(Action::OnChange),
_ => Err(Error::InvalidConversion(s.to_string())),
}
}
}

impl From<Action> for Attribute {
fn from(val: Action) -> Self {
match val {
Expand Down
31 changes: 31 additions & 0 deletions crates/html-to-egui/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

/// These are the data attibutes asscoiated with the action handlers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Attribute {
DataOnClick,
DataOnChange,
Expand All @@ -20,6 +21,36 @@ impl Attribute {
}
}

impl From<Attribute> for Action {
fn from(attr: Attribute) -> Self {
match attr {
Attribute::DataOnClick => Action::OnClick,
Attribute::DataOnChange => Action::OnChange,
}
}
}

impl From<&Attribute> for Action {
fn from(attr: &Attribute) -> Self {
match attr {
Attribute::DataOnClick => Action::OnClick,
Attribute::DataOnChange => Action::OnChange,
}
}
}

impl TryFrom<&str> for Attribute {
type Error = Error;

fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
Attribute::DATA_ON_CLICK => Ok(Attribute::DataOnClick),
Attribute::DATA_ON_CHANGE => Ok(Attribute::DataOnChange),
_ => Err(Error::InvalidConversion(s.to_string())),
}
}
}

impl From<Attribute> for &'static str {
fn from(val: Attribute) -> Self {
val.as_str()
Expand Down
8 changes: 8 additions & 0 deletions crates/html-to-egui/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Crate Errors
pub enum Error {
/// Parsing error
Parse(String),
/// Invalid action
InvalidConversion(String),
}
3 changes: 3 additions & 0 deletions crates/html-to-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ pub use selectors::*;

use std::fmt::Display;
use std::ops::{Deref, DerefMut};

mod error;
pub use error::Error;
1 change: 1 addition & 0 deletions crates/html-to-egui/src/selectors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

/// Slectors available to use with the Div element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DivSelectors {
/// No selector.
None,
Expand Down
9 changes: 5 additions & 4 deletions examples/datetime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[allow(warnings)]
#[cfg_attr(rustfmt, rustfmt_skip)]
mod bindings;

mod reactor;
use reactor::Reactor;

Expand All @@ -6,9 +10,6 @@ pub use block_on::{block_on, noop_waker};

mod polling;

#[allow(warnings)]
mod bindings;

use bindings::component::plugin::host::{emit, now, subscribe_duration};
use bindings::component::plugin::types::Event;
use bindings::exports::component::plugin::run::Guest;
Expand All @@ -29,7 +30,6 @@ impl Guest for Component {
render(`
<div>
<span>Seconds since unix was invented: {{datetime}}</span>
<button data-on-click="ticker()">1s Refresh</button>
</div>
`)
"#
Expand All @@ -46,6 +46,7 @@ impl Guest for Component {
}

/// This function calls now() every second by
/// Only works in native, breaks in wasm
fn ticker() {
block_on(|reactor| async move {
// we use sleep to wait for 1 second in between updates to datetime.
Expand Down
1 change: 0 additions & 1 deletion examples/login/rustfmt.toml

This file was deleted.

3 changes: 2 additions & 1 deletion examples/login/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[allow(warnings)]
#[cfg_attr(rustfmt, rustfmt_skip)]
mod bindings;

use bindings::component::plugin::host::emit;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl Guest for Component {
<div id="login1">
<input value="{{username}}" data-on-change="username(username)">
<input value="{{password}}" data-on-change="password(password)">
<button on_click=login()>Login</button>
<button class="" data-on-click=login()>Login</button>
</div>
`)
"#
Expand Down
1 change: 0 additions & 1 deletion examples/random/rustfmt.toml

This file was deleted.

1 change: 1 addition & 0 deletions examples/random/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[allow(warnings)]
#[cfg_attr(rustfmt, rustfmt_skip)]
mod bindings;

use bindings::component::plugin::host::{emit, random_byte};
Expand Down
25 changes: 15 additions & 10 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# for each dir in crates which has a `wit` directory in it, AND has src/bindings.rs, build it
build-wits:
for dir in crates/*; do \
if ([ -d $dir/wit ] && [ -f $dir/src/bindings.rs ]); then \
cargo component build --manifest-path=$dir/Cargo.toml --release; \
fi \
done
for dir in crates/*; do \
if [ -d $dir/wit ] && [ -f $dir/src/bindings.rs ]; then \
echo "Processing $dir"; \
(cd $dir && cargo component build); \
(cd $dir && cargo component build --release); \
fi; \
done

# build all wit examples in examples/ directory
build-examples:
for dir in examples/*; do \
if ([ -d $dir/wit ] && [ -f $dir/src/bindings.rs ]); then \
cargo component build --manifest-path=$dir/Cargo.toml --target wasm32-unknown-unknown --release; \
fi \
if [ -d $dir/wit ] && [ -f $dir/src/bindings.rs ]; then \
echo "Processing $dir"; \
(cd $dir && cargo component build --target wasm32-unknown-unknown); \
(cd $dir && cargo component build --target wasm32-unknown-unknown --release); \
fi; \
done

build: build-wits build-examples
build: build-examples
cargo build

test: build
cargo test
Expand All @@ -32,7 +37,7 @@ check32:
RUSTFLAGS="--deny warnings" cargo check --target wasm32-unknown-unknown

build32:
cargo +nightly build -Z build-std --release--target wasm32-unknown-unknown
cargo +nightly build -Z build-std --target wasm32-unknown-unknown

force:
cargo run --bin force-build-wasm-bins
5 changes: 4 additions & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# to the user in the error, instead of "error: invalid channel name '[toolchain]'".

[toolchain]
# channel = "1.82" # Avoid specifying a patch version here; see https://github.com/emilk/eframe_template/issues/145
# wasm-bindgen works with rust 1.78 + wasm-bindgen 2.92 onward. see: https://github.com/prisma/prisma-engines/pull/4867
# after v1.78 there appears to be a dlmalloc issue with wasm32-unknown-unknown with [scraper] crate
# so we are sticking with 1.78 for now
# channel = "1.80" # Avoid specifying a patch version here; see https://github.com/emilk/eframe_template/issues/145.
components = [ "rustfmt", "clippy" ]
targets = [ "wasm32-unknown-unknown" ]
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ pub enum Error {
#[error("Anyhow Error: {0}")]
Anyhow(#[from] anyhow::Error),

/// Selector Kind error
#[error("Selector Kind Error: {0}")]
Selector(#[from] scraper::error::SelectorErrorKind<'static>),

/// Parse error
#[error("Parse Error: {0}")]
Parse(String),
Expand Down
Loading

0 comments on commit 21cf9b8

Please sign in to comment.