Skip to content

Commit

Permalink
added slug generator to WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
nicelgueta committed Sep 27, 2024
1 parent 3e2076c commit 93847d3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
27 changes: 17 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
</head>
<body>
<script type="module" >
import init, { random_slugs, combinations } from "./pkg/rustyrs.js";
init();
import init, { random_slugs, combinations, SlugGenerator } from "./pkg/rustyrs.js";
await init();
let slug_gen = new SlugGenerator(2);
export const test = () => {
const word_length = document.getElementById("word_len_inp").value;
const n_outputs = document.getElementById("n_outputs").value;
Expand All @@ -17,14 +18,6 @@
}
const slugs = random_slugs(word_length, n_outputs);

// const word_combos = combinations(word_length);

// const wl = document.getElementById("wl");
// wl.textContent = word_length;

// const wlr = document.getElementById("wlr");
// wlr.textContent = word_combos;

const slugsElement = document.getElementById("slugs");
slugsElement.innerHTML = "";
try {
Expand All @@ -39,7 +32,15 @@
slugsElement.appendChild(li);
}
};
export const generate = () => {
const slug = slug_gen.next();
const slugsElement = document.getElementById("slugs");
const li = document.createElement("li");
li.textContent = slug;
slugsElement.appendChild(li);
};
document.getElementById("test").addEventListener("click", test);
document.getElementById("gen").addEventListener("click", generate);
</script>
<p>Enter length of slug in words (min 1, max 5)</p>
<input type="number" id="word_len_inp" max="5" min="1" />
Expand All @@ -49,7 +50,13 @@
<br>
<p>
<button style="padding-top: 10;" id="test">Go</button>
<button style="padding-top: 10;" onclick="location.reload();">Reset</button>
</p>
<br>
<div>
<p>OR generate unique slugs one-by-one</p>
<button id="gen">Generate</button>
</div>
<ul id="slugs"></ul>
</body>
</html>
53 changes: 50 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ pub use core::*;
mod wasm {
use crate::core::{
random_slugs as _random_slugs,
combinations as _combinations
combinations as _combinations,
get_words,
ADJ_FILE,
NOUN_FILE,
WordSelector
};
use wasm_bindgen::prelude::*;
use rand::seq::SliceRandom;

#[wasm_bindgen]
pub fn random_slugs(word_length: i32, num_outputs: Option<i32>) -> Option<Vec<String>> {
Expand All @@ -16,11 +21,53 @@ mod wasm {
}
}

#[wasm_bindgen]
pub struct SlugGenerator {
generator: WordSelector,
}

#[wasm_bindgen]
impl SlugGenerator {
#[wasm_bindgen(constructor)]
pub fn new(word_length: i32) -> Result<SlugGenerator, JsError> {
if word_length < 1 || word_length > 5 {
Err(JsError::new(
"word_length must be between 1 and 5"
))
} else {
let mut rng = rand::thread_rng();
let mut adjs = get_words(ADJ_FILE);
let mut nouns = get_words(NOUN_FILE);
adjs.shuffle(&mut rng);
nouns.shuffle(&mut rng);
let generator = if let Ok(gen) = WordSelector::new(
adjs, nouns,
word_length as usize
) {
gen
} else {
return Err(JsError::new("Failure creating WordSelector object"))
};
Ok(Self {generator})
}
}

// Calls the next item in the generator. Returns None when no more unique
// slugs can be generated
pub fn next(&mut self) -> Option<String> {
if let Ok(slug) = self.generator.choose() {
Some(slug)
} else {
None
}
}
}

// TODO: fix the fact integers overflow in wasm
#[wasm_bindgen]
pub fn combinations(word_length: i32) -> Option<i64> {
pub fn combinations(word_length: i32) -> Option<u64> {
match _combinations(word_length) {
Ok(v) => Some(v as i64),
Ok(v) => Some(v as u64),
Err(_e) => None
}
}
Expand Down

0 comments on commit 93847d3

Please sign in to comment.