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
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
4 changed files with 33 additions and 28 deletions

View File

@ -7,6 +7,10 @@ edition = "2018"
[lib] [lib]
crate-type = ["lib", "cdylib"] crate-type = ["lib", "cdylib"]
[features]
default = ["wasm"]
wasm = [] # Build default wasm bindings
[dependencies] [dependencies]
anyhow = "1.0.44" anyhow = "1.0.44"
csv = "1.1.6" 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"); pub const VERSION: &str = git_version!(args=["--always", "--dirty=-dev"], fallback="unknown");
/// Get [VERSION] as a String (for WebAssembly) /// Get [VERSION] as a String (for WebAssembly)
#[wasm_bindgen] #[cfg_attr(feature = "wasm", wasm_bindgen)]
pub fn version() -> String { VERSION.to_string() } pub fn version() -> String { VERSION.to_string() }

View File

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

View File

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