From 8b39e6fe69936e5f38e0d4fd8ed10cdf6183a2ae Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 15 Mar 2023 23:41:04 -0400 Subject: [PATCH] Make web-frontend a crate that automatically rebuilds --- Cargo.lock | 10 ++++ Cargo.toml | 1 + justfile | 13 +++-- src/lib.rs | 1 + web-frontend/Cargo.toml | 11 +++++ web-frontend/build.rs | 32 ++++++++++++ web-frontend/src/lib.rs | 5 ++ web/Cargo.toml | 1 + web/build.rs | 3 -- web/src/static_assets.rs | 5 +- words/src/lib.rs | 104 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 172 insertions(+), 14 deletions(-) create mode 100644 web-frontend/Cargo.toml create mode 100644 web-frontend/build.rs create mode 100644 web-frontend/src/lib.rs delete mode 100644 web/build.rs diff --git a/Cargo.lock b/Cargo.lock index da12c2d..e395316 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1738,9 +1738,19 @@ dependencies = [ "serde", "tokio", "ufmt", + "web-frontend", "xpin", ] +[[package]] +name = "web-frontend" +version = "0.1.0" +dependencies = [ + "rocket", + "rust-embed", + "xpin-wasm", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 30f45c4..7e5e44a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "./words", "./xpin-wasm/", "./web", + "./web-frontend/", ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/justfile b/justfile index bee4c21..ed69b59 100644 --- a/justfile +++ b/justfile @@ -1,5 +1,4 @@ -# Rust test needs to be built after because the web crate depends on js-build -build: fmt js-build rust-build rust-test +build: fmt rust-build rust-test push: podman push gitea.austen-wares.com/public/xpin-server @@ -8,7 +7,7 @@ rust-test: cargo test --all cargo fmt --all --check -rust-build: output-clean js-build +rust-build: output-clean cargo build --all cargo build -p web --release --target x86_64-unknown-linux-musl earthly +rust-image @@ -21,10 +20,9 @@ wasm-build: output-clean wordlist-build: . wordlist/venv/bin/activate && cd wordlist && for i in *.py; do "./${i}"; done -js-build: output-clean wasm-build docs-build - for f in "${PWD}/web-frontend/src/lib/docs/"*.html; do ./web-frontend/node_modules/.bin/inliner "${f}" >"${f}-2" && mv -v -- "${f}-2" "${f}"; done - yarn --cwd ./web-frontend/ build - rsync -ha ./web-frontend/build/ ./build/ +# js-build: output-clean wasm-build docs-build +# yarn --cwd ./web-frontend/ build +# # rsync -ha ./web-frontend/build/ ./build/ output-clean: rm -vrf ./build/ ./web-frontend/build/ ./web-frontend/src/lib/docs/ @@ -33,6 +31,7 @@ output-clean: docs-build: output-clean earthly +docs rsync --stats -ha ./build/docs/ ./web-frontend/src/lib/docs/ + for f in "${PWD}/web-frontend/src/lib/docs/"*.html; do ./web-frontend/node_modules/.bin/inliner "${f}" >"${f}-2" && mv -v -- "${f}-2" "${f}"; done cargo doc --all # TODO: Clippy diff --git a/src/lib.rs b/src/lib.rs index 05020e7..ea05297 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![allow(clippy::cast_possible_truncation, clippy::multiple_crate_versions)] #![allow(unused_imports)] +pub mod dense; pub mod v0; use conversions::lat_lon_to_cellid; pub use s2::s1::angle::Angle; diff --git a/web-frontend/Cargo.toml b/web-frontend/Cargo.toml new file mode 100644 index 0000000..2b57987 --- /dev/null +++ b/web-frontend/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "web-frontend" +version = "0.1.0" +edition = "2021" + +[dependencies] +rocket = {version="0.5.0-rc.2", features=["json"]} +rust-embed = { version = "6.6.0", features = ["rocket"] } + +[build-dependencies] +xpin-wasm={path="../xpin-wasm/"} diff --git a/web-frontend/build.rs b/web-frontend/build.rs new file mode 100644 index 0000000..ad4799c --- /dev/null +++ b/web-frontend/build.rs @@ -0,0 +1,32 @@ +use std::process::Command; +fn main() { + for path in [ + // ".svelte-kit", + "Cargo.toml", + "README.md", + // "build", + "build.rs", + "jsconfig.json", + // Excluding because it includes the module `xpin-wasm` + // "node_modules", + "package.json", + "postcss.config.cjs", + "src", + "static", + "svelte.config.js", + "tailwind.config.cjs", + "vite.config.js", + "yarn.lock", + ] { + println!("cargo:rerun-if-changed=./{path}"); + } + + if !Command::new("yarn") + .arg("build") + .status() + .expect("Could not spawn yarn build") + .success() + { + panic!("yarn build failed"); + } +} diff --git a/web-frontend/src/lib.rs b/web-frontend/src/lib.rs new file mode 100644 index 0000000..1de9c7b --- /dev/null +++ b/web-frontend/src/lib.rs @@ -0,0 +1,5 @@ +use rust_embed::RustEmbed; + +#[derive(RustEmbed)] +#[folder = "./build/"] +pub struct Asset; diff --git a/web/Cargo.toml b/web/Cargo.toml index 86f5efd..a2dab5b 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -12,3 +12,4 @@ xpin={path=".."} serde = {version="1", features=["derive"]} rust-embed = { version = "6.6.0", features = ["rocket"] } ufmt = "0.2.0" +web-frontend={path="../web-frontend/"} diff --git a/web/build.rs b/web/build.rs deleted file mode 100644 index 7c66d85..0000000 --- a/web/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("cargo:rerun-if-changed=../build"); -} diff --git a/web/src/static_assets.rs b/web/src/static_assets.rs index 274cd34..f07c043 100644 --- a/web/src/static_assets.rs +++ b/web/src/static_assets.rs @@ -5,10 +5,7 @@ use rust_embed::RustEmbed; use std::borrow::Cow; use std::ffi::OsStr; use std::path::PathBuf; - -#[derive(RustEmbed)] -#[folder = "../build/"] -struct Asset; +use web_frontend::Asset; #[get("/")] pub(crate) fn dist(file: PathBuf) -> Option<(ContentType, Cow<'static, [u8]>)> { diff --git a/words/src/lib.rs b/words/src/lib.rs index 984e783..d875657 100644 --- a/words/src/lib.rs +++ b/words/src/lib.rs @@ -54,6 +54,71 @@ where .copied() } +/// Gets multiple words given their index, but without repitition by skipping words if they would match +/// +/// For example, if the numeric value this should encode is [0, 0, 0], +/// the index of the words would be [0, 1, 2] +/// +/// [1, 2, 3] => word number [1, 3, 5] +/// [3, 2, 1] => word number [3, 2, 1] +/// [1, 1, 0] => word number [1, 2, 0] +pub fn get_words_multi(word_idx: [u16; N]) -> [&'static Word<'static>; N] { + // The vec of words to return + let mut words = Vec::with_capacity(N); + // The effective index of every word + let mut seen_effective_idx = Vec::with_capacity(N); + + for w_idx in word_idx { + let mut w_effective = w_idx; + seen_effective_idx.sort(); + + for other_effective_idx in seen_effective_idx.iter() { + if &w_effective >= other_effective_idx { + w_effective += 1; + } + } + + seen_effective_idx.push(w_effective); + words.push(NUMBER_TO_WORDS[w_effective as usize][0]); + } + + words + .try_into() + .expect("get_words_multi return vec did not match size") +} + +pub fn get_word_numbers_multi(words: [&Word<'static>; N]) -> [u16; N] { + let mut values = Vec::with_capacity(N); + let mut seen_effective_idx = words.iter().map(|w| w.number).collect::>(); + seen_effective_idx.sort(); + + for w_e in words.iter().rev().map(|w| w.number) { + let mut w_idx = w_e; + + if let Some(idx) = seen_effective_idx + .iter() + .position(|other_e| *other_e == w_idx) + { + seen_effective_idx.remove(idx); + } + + for other_e in seen_effective_idx.iter().rev() { + if w_idx >= *other_e { + w_idx -= 1; + } + } + + values.push(w_idx); + } + + values.reverse(); + + // pub fn get_idx_from_multi(words: &[&'static Word<'static>>]) -> Vec<&'static Word<'static>> { + values + .try_into() + .expect("get_words_multi return vec did not match size") +} + #[cfg(test)] mod tests { use super::*; @@ -96,4 +161,43 @@ mod tests { // te!(sent, scents); // te!(sent, cents); } + + #[test] + fn test_get_words_multi() { + for w1i in 0..=5 { + for w2i in 0..=5 { + for w3i in 0..=5 { + let input = [w1i, w2i, w3i]; + + eprintln!("Testing getting word {input:?}"); + + let effective = get_words_multi(input); + let [w1e, w2e, w3e] = effective; + + let output = get_word_numbers_multi(effective); + // let [w1o, w2o, w30] = output; + + eprintln!("{:?} => {:?} => {:?}", input, effective, output); + + // Make sure the effective values are not the same + assert_ne!(w1e, w2e); + assert_ne!(w2e, w3e); + assert_ne!(w1e, w3e); + + // Make sure the encoded owrds are not the same + assert_ne!(w1e.number, w2e.number); + assert_ne!(w2e.number, w3e.number); + assert_ne!(w1e.number, w3e.number); + assert_ne!(w1e.word, w2e.word); + assert_ne!(w2e.word, w3e.word); + assert_ne!(w1e.word, w3e.word); + + // Make sure that once decoded again, the results match + assert_eq!(input, output); + } + } + } + + assert!(false); + } }