Skip to content

Commit

Permalink
Add wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
wezm committed May 4, 2024
1 parent efd406a commit 0531fb6
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
env:
PATH: "$HOME/.cargo/bin:$PATH"
RUST_VERSION: '1.70.0' # Needs to be <= FreeBSD version
WASM_VERSION: '1.73.0'
AWS_ACCESS_KEY_ID: ENCRYPTED[5c3f77d4196c6b47340a4f07f6dec015753bf26df6f7323467217282d614988db7568c67785129a46a6de7fe8117c2af]
AWS_SECRET_ACCESS_KEY: ENCRYPTED[4c49da1c017860dd66710621efb3edb855c7da0dcad438a7bf9a3dbf41adfd15905cbe14df679ba2a83cff5a9ec2afdc]

Expand Down Expand Up @@ -48,6 +49,22 @@ task:
fi
before_cache_script: rm -rf $HOME/.cargo/registry/index

task:
name: Build (Web Assembly)
container:
image: debian:12-slim
cpu: 4
cargo_cache:
folder: $HOME/.cargo/registry
fingerprint_script: cat Cargo.lock
install_script:
- apt-get update && apt-get install -y --no-install-recommends git ca-certificates curl gcc libc6-dev
- curl https://sh.rustup.rs -sSf | sh -s -- -y --profile minimal --default-toolchain ${WASM_VERSION}
- rustup target add wasm32-unknown-unknown
build_script:
- cargo build --lib --target wasm32-unknown-unknown
before_cache_script: rm -rf $HOME/.cargo/registry/index

task:
name: Build (FreeBSD)
freebsd_instance:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
/wasm
**/*.rs.bk
114 changes: 114 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@ license = "MIT"
keywords = ["title", "case", "capitalization", "capitalisation", "capitalize"]
categories = ["text-processing"]

[lib]
# cdylib is for WASM
crate-type = ["cdylib", "rlib"]

[dependencies]
regex = { version = "1.10", default-features = false, features = ["std", "perf", "unicode-perl"]}

[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = "0.2.92"
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
all: wasm/titlecase.js

wasm/titlecase.js: target/wasm32-unknown-unknown/release/titlecase.wasm
wasm-bindgen target/wasm32-unknown-unknown/release/titlecase.wasm --target web --out-dir wasm

target/wasm32-unknown-unknown/release/titlecase.wasm:
cargo build --lib --target wasm32-unknown-unknown --release

.PHONY: target/wasm32-unknown-unknown/release/titlecase.wasm
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ by Rust including Linux, macOS, FreeBSD, NetBSD, OpenBSD, and Windows.
[![Documentation](https://docs.rs/titlecase/badge.svg)][crate-docs]
[![License](https://img.shields.io/crates/l/titlecase.svg)][MIT]

## Try Online

<https://7bit.org/titlecase/>

## Command Line Usage

`titlecase` reads lines of text from **stdin** and prints title cased versions
Expand Down Expand Up @@ -56,6 +60,27 @@ install the most recently released `titlecase` with cargo:

See the [crate documentation][crate-docs].

## Building for WebAssembly

### Pre-requisites

- Rust 1.73.0+
- Rust `wasm32-unknown-unknown` target
(`rustup target add wasm32-unknown-unknown` or `rust-wasm` package on Chimera Linux)
- [wasm-bindgen]
(`wasm-bindgen` package on Arch, or `cargo install wasm-bindgen-cli --version 0.2.92`)
- `make` (GNU or BSD should work)

### Building

There is a `Makefile` that automates building for WebAssembly.

make

The output is put into a `wasm` directory. See
<https://github.com/wezm/7bit.org/tree/main/public/titlecase> for an
example that uses the wasm build.

## Style

Instead of simply capitalizing each word `titlecase` does the following
Expand All @@ -81,3 +106,4 @@ Pagaltzis], and [David Gouch].
[MIT]: https://github.com/wezm/titlecase/blob/master/LICENSE
[rustup]: https://www.rust-lang.org/tools/install
[style]: https://daringfireball.net/2008/05/title_case
[wasm-bindgen]: https://github.com/rustwasm/wasm-bindgen
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use std::sync::OnceLock;

use regex::{Captures, Regex};

#[cfg(target_family = "wasm")]
mod wasm;

#[rustfmt::skip]
const SMALL_WORDS: &[&str] = &[
"a",
Expand Down
6 changes: 6 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn titlecase(text: &str) -> String {
crate::titlecase(text)
}

0 comments on commit 0531fb6

Please sign in to comment.