From 5a535743668e88bdba55480a927c704e75ed8399 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 18 Oct 2021 18:06:42 +1100 Subject: [PATCH] Allow opting out of building default wasm bindings --- Cargo.toml | 4 ++++ src/lib.rs | 2 +- src/stv/mod.rs | 17 +++++++++-------- src/stv/wasm.rs | 38 +++++++++++++++++++------------------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7fe9a5f..4df5fba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 5eb0f7a..3bef7c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() } diff --git a/src/stv/mod.rs b/src/stv/mod.rs index a8c1c99..a73324c 100644 --- a/src/stv/mod.rs +++ b/src/stv/mod.rs @@ -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> From 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> From 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> From 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> From 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> From 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> From 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> From for ExclusionMethod { } /// Enum of options for [STVOptions::sample] -#[wasm_bindgen] +#[cfg_attr(feature = "wasm", wasm_bindgen)] #[derive(Clone, Copy)] #[derive(PartialEq)] pub enum SampleMethod { diff --git a/src/stv/wasm.rs b/src/stv/wasm.rs index 2a4f5cf..2f528e0 100644 --- a/src/stv/wasm.rs +++ b/src/stv/wasm.rs @@ -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 [](text: String) -> [] { // 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: &mut [], 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 [](election: &mut [], 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 [](state: &mut [], 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 [](state: &mut [], 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 [](election: &[], 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 [](filename: String, election: &[], 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 [](stage_num: usize, state: &[], 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 [](state: &[], 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 [](state: &[], 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 [](state: &[], 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<'static, $type>); - #[wasm_bindgen] + #[cfg_attr(feature = "wasm", wasm_bindgen)] impl [] { /// Create a new [CountState] wrapper pub fn new(election: &[]) -> 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>); - #[wasm_bindgen] + #[cfg_attr(feature = "wasm", wasm_bindgen)] impl [] { /// 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(