From f1a730e8859fec9dd62c7cd9d36613140082b3d3 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Tue, 1 Jun 2021 19:04:03 +1000 Subject: [PATCH] Update WebAssembly build for recent changes --- build_wasm.sh | 2 ++ pkg/test.html | 2 +- src/main.rs | 31 ++++++++----------------------- src/stv/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100755 build_wasm.sh diff --git a/build_wasm.sh b/build_wasm.sh new file mode 100755 index 0000000..50be928 --- /dev/null +++ b/build_wasm.sh @@ -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 diff --git a/pkg/test.html b/pkg/test.html index 495d306..a0c579c 100644 --- a/pkg/test.html +++ b/pkg/test.html @@ -27,7 +27,7 @@ async function run() { 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 state = wasm.CountStateRational.new(election); diff --git a/src/main.rs b/src/main.rs index ed56d8b..e1740fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,29 +125,14 @@ where for<'r> &'r N: ops::Neg { // Copy applicable options - let stv_opts = stv::STVOptions { - round_votes: cmd_opts.round_votes, - surplus: match cmd_opts.surplus.as_str() { - "wig" => stv::SurplusMethod::WIG, - "uig" => stv::SurplusMethod::UIG, - "eg" => stv::SurplusMethod::EG, - "meek" => stv::SurplusMethod::Meek, - _ => 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, - }; + let stv_opts = stv::STVOptions::new( + cmd_opts.round_votes, + &cmd_opts.surplus, + &cmd_opts.surplus_order, + cmd_opts.transferable_only, + &cmd_opts.exclusion, + cmd_opts.pp_decimals, + ); // Initialise count state let mut state = CountState::new(&election); diff --git a/src/stv/mod.rs b/src/stv/mod.rs index da6a454..2ab4d1f 100644 --- a/src/stv/mod.rs +++ b/src/stv/mod.rs @@ -38,6 +38,42 @@ pub struct STVOptions { pub pp_decimals: usize, } +#[wasm_bindgen] +impl STVOptions { + pub fn new( + round_votes: Option, + 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] #[derive(Clone, Copy)] pub enum SurplusMethod {