Add up-to-date currency conversion to the rink plugin

This commit is contained in:
Kirottu
2023-01-03 19:29:54 +02:00
parent 06a95a2b06
commit 7394ce2dae
3 changed files with 35 additions and 6 deletions

1
Cargo.lock generated
View File

@@ -1515,6 +1515,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"abi_stable", "abi_stable",
"anyrun-plugin", "anyrun-plugin",
"reqwest",
"rink-core", "rink-core",
] ]

View File

@@ -12,3 +12,4 @@ crate-type = ["cdylib"]
anyrun-plugin = { path = "../../anyrun-plugin" } anyrun-plugin = { path = "../../anyrun-plugin" }
abi_stable = "0.11.1" abi_stable = "0.11.1"
rink-core = "0.6" rink-core = "0.6"
reqwest = { version = "0.11.13", features = ["blocking", "json"] }

View File

@@ -1,7 +1,35 @@
use abi_stable::std_types::{ROption, RString, RVec}; use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::{anyrun_interface::HandleResult, plugin, Match, PluginInfo}; 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::<ast::Defs>() {
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 { fn info() -> PluginInfo {
PluginInfo { PluginInfo {
@@ -10,9 +38,8 @@ fn info() -> PluginInfo {
} }
} }
fn get_matches(input: RString, _: &mut ()) -> RVec<Match> { fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec<Match> {
let mut ctx = rink_core::simple_context().unwrap(); match rink_core::one_line(ctx, &input) {
match rink_core::one_line(&mut ctx, &input) {
Ok(result) => vec![Match { Ok(result) => vec![Match {
title: result.into(), title: result.into(),
icon: ROption::RNone, icon: ROption::RNone,
@@ -24,8 +51,8 @@ fn get_matches(input: RString, _: &mut ()) -> RVec<Match> {
} }
} }
fn handler(selection: Match, _: &mut ()) -> HandleResult { fn handler(selection: Match, _: &mut rink_core::Context) -> HandleResult {
HandleResult::Copy(selection.title.into_bytes()) HandleResult::Copy(selection.title.into_bytes())
} }
plugin!(init, info, get_matches, handler, ()); plugin!(init, info, get_matches, handler, rink_core::Context);