Update WebAssembly build for recent changes

This commit is contained in:
RunasSudo 2021-06-01 19:04:03 +10:00
parent a4a28f3345
commit f1a730e885
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
4 changed files with 47 additions and 24 deletions

2
build_wasm.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
cargo build --lib --target wasm32-unknown-unknown && /home/runassudo/.cargo/bin/wasm-bindgen --target no-modules target/wasm32-unknown-unknown/debug/opentally.wasm --out-dir pkg --no-typescript

View File

@ -27,7 +27,7 @@
async function run() { async function run() {
await wasm_bindgen("opentally_bg.wasm"); await wasm_bindgen("opentally_bg.wasm");
let stv_opts = wasm.STVOptions.new(2, "one_round"); let stv_opts = wasm.STVOptions.new(null, "wig", "by_size", false, "single_stage", 2);
let election = wasm.election_from_blt_Rational(election_data); let election = wasm.election_from_blt_Rational(election_data);
let state = wasm.CountStateRational.new(election); let state = wasm.CountStateRational.new(election);

View File

@ -125,29 +125,14 @@ where
for<'r> &'r N: ops::Neg<Output=N> for<'r> &'r N: ops::Neg<Output=N>
{ {
// Copy applicable options // Copy applicable options
let stv_opts = stv::STVOptions { let stv_opts = stv::STVOptions::new(
round_votes: cmd_opts.round_votes, cmd_opts.round_votes,
surplus: match cmd_opts.surplus.as_str() { &cmd_opts.surplus,
"wig" => stv::SurplusMethod::WIG, &cmd_opts.surplus_order,
"uig" => stv::SurplusMethod::UIG, cmd_opts.transferable_only,
"eg" => stv::SurplusMethod::EG, &cmd_opts.exclusion,
"meek" => stv::SurplusMethod::Meek, cmd_opts.pp_decimals,
_ => panic!("Invalid --surplus"), );
},
surplus_order: match cmd_opts.surplus_order.as_str() {
"by_size" => stv::SurplusOrder::BySize,
"by_order" => stv::SurplusOrder::ByOrder,
_ => panic!("Invalid --surplus-order"),
},
transferable_only: cmd_opts.transferable_only,
exclusion: match cmd_opts.exclusion.as_str() {
"single_stage" => stv::ExclusionMethod::SingleStage,
"by_value" => stv::ExclusionMethod::ByValue,
"parcels_by_order" => stv::ExclusionMethod::ParcelsByOrder,
_ => panic!("Invalid --exclusion"),
},
pp_decimals: cmd_opts.pp_decimals,
};
// Initialise count state // Initialise count state
let mut state = CountState::new(&election); let mut state = CountState::new(&election);

View File

@ -38,6 +38,42 @@ pub struct STVOptions {
pub pp_decimals: usize, pub pp_decimals: usize,
} }
#[wasm_bindgen]
impl STVOptions {
pub fn new(
round_votes: Option<usize>,
surplus: &str,
surplus_order: &str,
transferable_only: bool,
exclusion: &str,
pp_decimals: usize,
) -> Self {
return STVOptions {
round_votes: round_votes,
surplus: match surplus {
"wig" => SurplusMethod::WIG,
"uig" => SurplusMethod::UIG,
"eg" => SurplusMethod::EG,
"meek" => SurplusMethod::Meek,
_ => panic!("Invalid --surplus"),
},
surplus_order: match surplus_order {
"by_size" => SurplusOrder::BySize,
"by_order" => SurplusOrder::ByOrder,
_ => panic!("Invalid --surplus-order"),
},
transferable_only: transferable_only,
exclusion: match exclusion {
"single_stage" => ExclusionMethod::SingleStage,
"by_value" => ExclusionMethod::ByValue,
"parcels_by_order" => ExclusionMethod::ParcelsByOrder,
_ => panic!("Invalid --exclusion"),
},
pp_decimals: pp_decimals,
};
}
}
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum SurplusMethod { pub enum SurplusMethod {