Allow opting out of building default wasm bindings

This commit is contained in:
RunasSudo 2021-10-18 18:06:42 +11:00
parent e78d06289a
commit 5a53574366
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 33 additions and 28 deletions

View File

@ -7,6 +7,10 @@ edition = "2018"
[lib]
crate-type = ["lib", "cdylib"]
[features]
default = ["wasm"]
wasm = [] # Build default wasm bindings
[dependencies]
anyhow = "1.0.44"
csv = "1.1.6"

View File

@ -50,5 +50,5 @@ use wasm_bindgen::prelude::wasm_bindgen;
pub const VERSION: &str = git_version!(args=["--always", "--dirty=-dev"], fallback="unknown");
/// Get [VERSION] as a String (for WebAssembly)
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub fn version() -> String { VERSION.to_string() }

View File

@ -38,6 +38,7 @@ use crate::ties::{self, TieStrategy};
use derive_builder::Builder;
use derive_more::Constructor;
use itertools::Itertools;
#[allow(unused_imports)]
use wasm_bindgen::prelude::wasm_bindgen;
use std::collections::HashMap;
@ -229,7 +230,7 @@ impl STVOptions {
}
/// Enum of options for [STVOptions::sum_surplus_transfers]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum SumSurplusTransfersMode {
@ -260,7 +261,7 @@ impl<S: AsRef<str>> From<S> for SumSurplusTransfersMode {
}
/// Enum of options for [STVOptions::quota]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum QuotaType {
@ -299,7 +300,7 @@ impl<S: AsRef<str>> From<S> for QuotaType {
}
/// Enum of options for [STVOptions::quota_criterion]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum QuotaCriterion {
@ -330,7 +331,7 @@ impl<S: AsRef<str>> From<S> for QuotaCriterion {
}
/// Enum of options for [STVOptions::quota_mode]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum QuotaMode {
@ -373,7 +374,7 @@ impl<S: AsRef<str>> From<S> for QuotaMode {
}
/// Enum of options for [STVOptions::surplus]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum SurplusMethod {
@ -429,7 +430,7 @@ impl<S: AsRef<str>> From<S> for SurplusMethod {
}
/// Enum of options for [STVOptions::surplus_order]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum SurplusOrder {
@ -460,7 +461,7 @@ impl<S: AsRef<str>> From<S> for SurplusOrder {
}
/// Enum of options for [STVOptions::exclusion]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum ExclusionMethod {
@ -503,7 +504,7 @@ impl<S: AsRef<str>> From<S> for ExclusionMethod {
}
/// Enum of options for [STVOptions::sample]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy)]
#[derive(PartialEq)]
pub enum SampleMethod {

View File

@ -50,13 +50,13 @@ macro_rules! wasm_error {
// Init
/// Wrapper for [Fixed::set_dps]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub fn fixed_set_dps(dps: usize) {
Fixed::set_dps(dps);
}
/// Wrapper for [GuardedFixed::set_dps]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub fn gfixed_set_dps(dps: usize) {
GuardedFixed::set_dps(dps);
}
@ -68,7 +68,7 @@ macro_rules! impl_type {
// Counting
/// Wrapper for [blt::parse_iterator]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<election_from_blt_$type>](text: String) -> [<Election$type>] {
// Install panic! hook
@ -82,21 +82,21 @@ macro_rules! impl_type {
}
/// Call [Constraints::from_con] and set [Election::constraints]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<election_load_constraints_$type>](election: &mut [<Election$type>], text: String) {
election.0.constraints = Some(Constraints::from_con(text.lines().map(|s| s.to_string()).into_iter()));
}
/// Wrapper for [stv::preprocess_election]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<preprocess_election_$type>](election: &mut [<Election$type>], opts: &STVOptions) {
stv::preprocess_election(&mut election.0, &opts.0);
}
/// Wrapper for [stv::count_init]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<count_init_$type>](state: &mut [<CountState$type>], opts: &STVOptions) {
match stv::count_init(&mut state.0, opts.as_static()) {
@ -106,7 +106,7 @@ macro_rules! impl_type {
}
/// Wrapper for [stv::count_one_stage]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<count_one_stage_$type>](state: &mut [<CountState$type>], opts: &STVOptions) -> bool {
match stv::count_one_stage::<[<$type>]>(&mut state.0, &opts.0) {
@ -118,42 +118,42 @@ macro_rules! impl_type {
// Reporting
/// Wrapper for [init_results_table]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<init_results_table_$type>](election: &[<Election$type>], opts: &STVOptions, report_style: &str) -> String {
return init_results_table(&election.0, &opts.0, report_style);
}
/// Wrapper for [describe_count]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<describe_count_$type>](filename: String, election: &[<Election$type>], opts: &STVOptions) -> String {
return describe_count(filename, &election.0, &opts.0);
}
/// Wrapper for [update_results_table]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<update_results_table_$type>](stage_num: usize, state: &[<CountState$type>], opts: &STVOptions, report_style: &str) -> Array {
return update_results_table(stage_num, &state.0, &opts.0, report_style);
}
/// Wrapper for [update_stage_comments]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<update_stage_comments_$type>](state: &[<CountState$type>], stage_num: usize) -> String {
return update_stage_comments(&state.0, stage_num);
}
/// Wrapper for [finalise_results_table]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<finalise_results_table_$type>](state: &[<CountState$type>], report_style: &str) -> Array {
return finalise_results_table(&state.0, report_style);
}
/// Wrapper for [final_result_summary]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn [<final_result_summary_$type>](state: &[<CountState$type>], opts: &STVOptions) -> String {
return final_result_summary(&state.0, &opts.0);
@ -165,9 +165,9 @@ macro_rules! impl_type {
///
/// This is required as `&'static` cannot be specified in wasm-bindgen: see [issue 1187](https://github.com/rustwasm/wasm-bindgen/issues/1187).
///
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct [<CountState$type>](CountState<'static, $type>);
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl [<CountState$type>] {
/// Create a new [CountState] wrapper
pub fn new(election: &[<Election$type>]) -> Self {
@ -187,9 +187,9 @@ macro_rules! impl_type {
///
/// This is required as `&'static` cannot be specified in wasm-bindgen: see [issue 1187](https://github.com/rustwasm/wasm-bindgen/issues/1187).
///
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct [<Election$type>](Election<$type>);
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl [<Election$type>] {
/// Return [Election::seats]
pub fn seats(&self) -> usize { self.0.seats }
@ -215,10 +215,10 @@ impl_type!(NativeFloat64);
impl_type!(Rational);
/// Wrapper for [stv::STVOptions]
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct STVOptions(stv::STVOptions);
#[wasm_bindgen]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl STVOptions {
/// Wrapper for [stv::STVOptions::new]
pub fn new(