Skip to content

Commit

Permalink
Attemt wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Jan 13, 2024
1 parent be51ec4 commit 011e7c0
Show file tree
Hide file tree
Showing 18 changed files with 2,720 additions and 4 deletions.
162 changes: 160 additions & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ categories = [
"value-formatting",
]

[workspace]
members = ["wasm"]

[dependencies]
decimal = { version = "2.1", default-features = false, features = [
"serde",
Expand Down
4 changes: 2 additions & 2 deletions src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ macro_rules! create_units {
),*
}
}
pub(crate) fn singular(&self) -> &str {
pub fn singular(&self) -> &str {
match self {
$(
Unit::$variant => $properties.2
),*
}
}
pub(crate) fn plural(&self) -> &str {
pub fn plural(&self) -> &str {
match self {
$(
Unit::$variant => $properties.3
Expand Down
20 changes: 20 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cpc-wasm"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
cpc = { path = "../" }
decimal = { version = "2.1", default-features = false, features = [
"serde",
"ord_subset",
] }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.6"
wasm-bindgen = "0.2"

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz"]
32 changes: 32 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use decimal::d128;
use serde::Serialize;
use wasm_bindgen::prelude::*;

#[derive(Serialize)]
pub struct NumberWasm {
pub value: String,
pub quantity: String,
pub unit: String,
}

#[wasm_bindgen]
pub fn eval_wasm(input: &str) -> Result<JsValue, String> {
let number = cpc::eval(input, true, false)?;
// 0.2/0.01 results in 2E+1, but if we add zero it becomes 20
let quantity = number.value + d128!(0);
let word = match quantity == d128!(1) {
true => number.unit.singular(),
false => number.unit.plural(),
};
let output_text = match word {
"" => format!("{quantity}"),
_ => format!("{quantity} {word}"),
};
let number_wasm = NumberWasm {
value: output_text,
quantity: quantity.to_string(),
unit: word.into(),
};
let js_value = serde_wasm_bindgen::to_value(&number_wasm).unwrap();
Ok(js_value)
}
10 changes: 10 additions & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions web/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
38 changes: 38 additions & 0 deletions web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```bash
# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
Loading

0 comments on commit 011e7c0

Please sign in to comment.