Browse Source

Make attohttpc dependency optional, shrink WASM size from 1.3 MiB to 800 KiB

David Peter 1 year ago
parent
commit
84ae76626a

+ 1 - 1
numbat-cli/src/main.rs

@@ -370,7 +370,7 @@ impl Cli {
                                 println!();
                             }
                             _ => {
-                                if let Some(keyword) = line.strip_prefix("info") {
+                                if let Some(keyword) = line.strip_prefix("info ") {
                                     let help = self
                                         .context
                                         .lock()

+ 4 - 1
numbat-exchange-rates/Cargo.toml

@@ -10,5 +10,8 @@ license = "MIT OR Apache-2.0"
 rust-version = "1.70"
 
 [dependencies]
-attohttpc = { version = "0.26.0", default-features = false, features = ["tls-rustls-webpki-roots"] }
+attohttpc = { version = "0.26.0", default-features = false, features = ["tls-rustls-webpki-roots"], optional = true }
 quick-xml = "0.31.0"
+
+[features]
+fetch-exchangerates = ["dep:attohttpc"]

+ 4 - 0
numbat-exchange-rates/src/lib.rs

@@ -33,18 +33,22 @@ pub fn parse_exchange_rates(xml_content: &str) -> Option<ExchangeRates> {
     Some(rates)
 }
 
+#[cfg(feature = "fetch-exchangerates")]
 const ECB_XML_URL: &str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
 
+#[cfg(feature = "fetch-exchangerates")]
 fn fetch_ecb_xml() -> Option<String> {
     attohttpc::get(ECB_XML_URL).send().ok()?.text().ok()
 }
 
+#[cfg(feature = "fetch-exchangerates")]
 pub fn fetch_exchange_rates() -> Option<ExchangeRates> {
     let xml_content = fetch_ecb_xml()?;
     parse_exchange_rates(&xml_content)
 }
 
 #[cfg(test)]
+#[cfg(feature = "fetch-exchangerates")]
 mod tests {
     use super::*;
 

+ 0 - 177
numbat-wasm/Cargo.lock

@@ -8,19 +8,6 @@ version = "0.7.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
 
-[[package]]
-name = "attohttpc"
-version = "0.26.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0f77d243921b0979fbbd728dd2d5162e68ac8252976797c24eb5b3a6af9090dc"
-dependencies = [
- "http",
- "log",
- "rustls",
- "url",
- "webpki-roots",
-]
-
 [[package]]
 name = "autocfg"
 version = "1.1.0"
@@ -48,21 +35,6 @@ version = "3.13.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
 
-[[package]]
-name = "bytes"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
-
-[[package]]
-name = "cc"
-version = "1.0.83"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
-dependencies = [
- "libc",
-]
-
 [[package]]
 name = "cfg-if"
 version = "0.1.10"
@@ -150,21 +122,6 @@ version = "1.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
 
-[[package]]
-name = "fnv"
-version = "1.0.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
-
-[[package]]
-name = "form_urlencoded"
-version = "1.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
-dependencies = [
- "percent-encoding",
-]
-
 [[package]]
 name = "generic-array"
 version = "0.14.7"
@@ -204,27 +161,6 @@ dependencies = [
  "utf8-width",
 ]
 
-[[package]]
-name = "http"
-version = "0.2.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482"
-dependencies = [
- "bytes",
- "fnv",
- "itoa",
-]
-
-[[package]]
-name = "idna"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
-dependencies = [
- "unicode-bidi",
- "unicode-normalization",
-]
-
 [[package]]
 name = "itertools"
 version = "0.11.0"
@@ -351,7 +287,6 @@ dependencies = [
 name = "numbat-exchange-rates"
 version = "0.4.0"
 dependencies = [
- "attohttpc",
  "quick-xml",
 ]
 
@@ -375,12 +310,6 @@ version = "1.18.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
 
-[[package]]
-name = "percent-encoding"
-version = "2.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
-
 [[package]]
 name = "pretty_dtoa"
 version = "0.3.0"
@@ -437,21 +366,6 @@ dependencies = [
  "thiserror",
 ]
 
-[[package]]
-name = "ring"
-version = "0.16.20"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
-dependencies = [
- "cc",
- "libc",
- "once_cell",
- "spin",
- "untrusted",
- "web-sys",
- "winapi",
-]
-
 [[package]]
 name = "rust-embed"
 version = "8.0.0"
@@ -487,28 +401,6 @@ dependencies = [
  "walkdir",
 ]
 
-[[package]]
-name = "rustls"
-version = "0.21.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cd8d6c9f025a446bc4d18ad9632e69aec8f287aa84499ee335599fabd20c3fd8"
-dependencies = [
- "log",
- "ring",
- "rustls-webpki",
- "sct",
-]
-
-[[package]]
-name = "rustls-webpki"
-version = "0.101.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7d93931baf2d282fff8d3a532bbfd7653f734643161b87e3e01e59a04439bf0d"
-dependencies = [
- "ring",
- "untrusted",
-]
-
 [[package]]
 name = "ryu_floating_decimal"
 version = "0.1.0"
@@ -530,16 +422,6 @@ version = "1.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
 
-[[package]]
-name = "sct"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
-dependencies = [
- "ring",
- "untrusted",
-]
-
 [[package]]
 name = "sha2"
 version = "0.10.7"
@@ -560,12 +442,6 @@ dependencies = [
  "dirs",
 ]
 
-[[package]]
-name = "spin"
-version = "0.5.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
-
 [[package]]
 name = "strsim"
 version = "0.10.0"
@@ -612,48 +488,18 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "tinyvec"
-version = "1.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
-dependencies = [
- "tinyvec_macros",
-]
-
-[[package]]
-name = "tinyvec_macros"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
-
 [[package]]
 name = "typenum"
 version = "1.17.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
 
-[[package]]
-name = "unicode-bidi"
-version = "0.3.13"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
-
 [[package]]
 name = "unicode-ident"
 version = "1.0.11"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
 
-[[package]]
-name = "unicode-normalization"
-version = "0.1.22"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
-dependencies = [
- "tinyvec",
-]
-
 [[package]]
 name = "unicode-segmentation"
 version = "1.10.1"
@@ -666,23 +512,6 @@ version = "0.1.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
 
-[[package]]
-name = "untrusted"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
-
-[[package]]
-name = "url"
-version = "2.4.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5"
-dependencies = [
- "form_urlencoded",
- "idna",
- "percent-encoding",
-]
-
 [[package]]
 name = "utf8-width"
 version = "0.1.6"
@@ -811,12 +640,6 @@ dependencies = [
  "wasm-bindgen",
 ]
 
-[[package]]
-name = "webpki-roots"
-version = "0.25.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc"
-
 [[package]]
 name = "wee_alloc"
 version = "0.4.5"

+ 1 - 1
numbat-wasm/Cargo.toml

@@ -24,7 +24,7 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
 # allocator, however.
 wee_alloc = { version = "0.4.5", optional = true }
 
-numbat = { path = "../numbat" }
+numbat = { path = "../numbat", default-features = false }
 html-escape = "0.2.13"
 termcolor = "1.3.0"
 codespan-reporting = "0.11.1"

+ 4 - 0
numbat/Cargo.toml

@@ -30,6 +30,10 @@ rust-embed = { version = "8.0.0", features = ["interpolate-folder-path"] }
 num-format = "0.4.4"
 walkdir = "2"
 
+[features]
+default = ["fetch-exchangerates"]
+fetch-exchangerates = ["numbat-exchange-rates/fetch-exchangerates"]
+
 [dev-dependencies]
 approx = "0.5"
 glob = "0.3"

+ 8 - 2
numbat/src/currency.rs

@@ -1,6 +1,6 @@
 use std::sync::{Mutex, MutexGuard, OnceLock};
 
-use numbat_exchange_rates::{fetch_exchange_rates, parse_exchange_rates, ExchangeRates};
+use numbat_exchange_rates::{parse_exchange_rates, ExchangeRates};
 
 static EXCHANGE_RATES: OnceLock<Mutex<Option<ExchangeRates>>> = OnceLock::new();
 
@@ -22,10 +22,16 @@ impl ExchangeRatesCache {
             .unwrap();
     }
 
+    #[cfg(feature = "fetch-exchangerates")]
     pub fn fetch() -> MutexGuard<'static, Option<ExchangeRates>> {
         EXCHANGE_RATES
-            .get_or_init(|| Mutex::new(fetch_exchange_rates()))
+            .get_or_init(|| Mutex::new(numbat_exchange_rates::fetch_exchange_rates()))
             .lock()
             .unwrap()
     }
+
+    #[cfg(not(feature = "fetch-exchangerates"))]
+    pub fn fetch() -> MutexGuard<'static, Option<ExchangeRates>> {
+        EXCHANGE_RATES.get().unwrap().lock().unwrap()
+    }
 }