diff --git a/plugins/rink/README.md b/plugins/rink/README.md index c438d962..2e2a5020 100644 --- a/plugins/rink/README.md +++ b/plugins/rink/README.md @@ -4,4 +4,15 @@ A simple calculator plugin powered by [Rink](https://github.com/tiffany352/rink- ## Usage -Just type in your calculations/unit conversions. \ No newline at end of file +Type in the configured prefix (default is no prefix), then just type in your calculations/unit conversions. + +## Configuration + +```ron +// /rink.ron +Config( + prefix: "", + // Pull currency conversions from https://rinkcalc.app/data/currency.json + pull_currencies: true, +) +``` diff --git a/plugins/rink/src/lib.rs b/plugins/rink/src/lib.rs index 8ecf7df5..a892108a 100644 --- a/plugins/rink/src/lib.rs +++ b/plugins/rink/src/lib.rs @@ -7,12 +7,14 @@ use std::fs; #[derive(Deserialize, Debug)] struct Config { prefix: String, + pull_currencies: bool, } impl Default for Config { fn default() -> Self { Config { prefix: "".to_string(), + pull_currencies: true, } } } @@ -24,31 +26,6 @@ struct State { #[init] fn init(config_dir: RString) -> State { - let mut ctx = rink_core::Context::new(); - - let units = gnu_units::parse_str(rink_core::DEFAULT_FILE.unwrap()); - let dates = date::parse_datefile(rink_core::DATES_FILE); - - let mut currency_defs = Vec::new(); - - match reqwest::blocking::get("https://rinkcalc.app/data/currency.json") { - Ok(response) => match response.json::() { - Ok(mut live_defs) => { - currency_defs.append(&mut live_defs.defs); - } - Err(why) => eprintln!("[rink] Error parsing currency json: {why}"), - }, - Err(why) => eprintln!("[rink] Error fetching up-to-date currency conversions: {why}",), - } - - currency_defs.append(&mut gnu_units::parse_str(CURRENCY_FILE).defs); - - ctx.load(units); - ctx.load(ast::Defs { - defs: currency_defs, - }); - ctx.load_dates(dates); - let config = match fs::read_to_string(format!("{config_dir}/rink.ron")) { Ok(content) => ron::from_str(&content).unwrap_or_else(|why| { eprintln!("[rink] Failed to parse config: {why}"); @@ -60,6 +37,34 @@ fn init(config_dir: RString) -> State { } }; + let mut ctx = rink_core::Context::new(); + + let units = gnu_units::parse_str(rink_core::DEFAULT_FILE.unwrap()); + let dates = date::parse_datefile(rink_core::DATES_FILE); + + if config.pull_currencies { + let mut currency_defs = Vec::new(); + + match reqwest::blocking::get("https://rinkcalc.app/data/currency.json") { + Ok(response) => match response.json::() { + Ok(mut live_defs) => { + currency_defs.append(&mut live_defs.defs); + } + Err(why) => eprintln!("[rink] Error parsing currency json: {why}"), + }, + Err(why) => eprintln!("[rink] Error fetching up-to-date currency conversions: {why}",), + } + + currency_defs.append(&mut gnu_units::parse_str(CURRENCY_FILE).defs); + + ctx.load(ast::Defs { + defs: currency_defs, + }); + } + + ctx.load(units); + ctx.load_dates(dates); + State { ctx, config } }