diff --git a/Cargo.lock b/Cargo.lock index 0fc0a15..95be35d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1515,6 +1515,7 @@ version = "0.1.0" dependencies = [ "abi_stable", "anyrun-plugin", + "reqwest", "rink-core", ] diff --git a/plugins/rink/Cargo.toml b/plugins/rink/Cargo.toml index 1e4a7a4..ecb54d6 100644 --- a/plugins/rink/Cargo.toml +++ b/plugins/rink/Cargo.toml @@ -12,3 +12,4 @@ crate-type = ["cdylib"] anyrun-plugin = { path = "../../anyrun-plugin" } abi_stable = "0.11.1" rink-core = "0.6" +reqwest = { version = "0.11.13", features = ["blocking", "json"] } diff --git a/plugins/rink/src/lib.rs b/plugins/rink/src/lib.rs index 6276ab6..8b24e23 100644 --- a/plugins/rink/src/lib.rs +++ b/plugins/rink/src/lib.rs @@ -1,7 +1,35 @@ use abi_stable::std_types::{ROption, RString, RVec}; use anyrun_plugin::{anyrun_interface::HandleResult, plugin, Match, PluginInfo}; +use rink_core::{ast, date, gnu_units, CURRENCY_FILE}; -fn init(_config_dir: RString) {} +fn init(_config_dir: RString) -> rink_core::Context { + 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) => println!("Error parsing currency json: {}", why), + }, + Err(why) => println!("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); + + ctx +} fn info() -> PluginInfo { PluginInfo { @@ -10,9 +38,8 @@ fn info() -> PluginInfo { } } -fn get_matches(input: RString, _: &mut ()) -> RVec { - let mut ctx = rink_core::simple_context().unwrap(); - match rink_core::one_line(&mut ctx, &input) { +fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec { + match rink_core::one_line(ctx, &input) { Ok(result) => vec![Match { title: result.into(), icon: ROption::RNone, @@ -24,8 +51,8 @@ fn get_matches(input: RString, _: &mut ()) -> RVec { } } -fn handler(selection: Match, _: &mut ()) -> HandleResult { +fn handler(selection: Match, _: &mut rink_core::Context) -> HandleResult { HandleResult::Copy(selection.title.into_bytes()) } -plugin!(init, info, get_matches, handler, ()); +plugin!(init, info, get_matches, handler, rink_core::Context);