Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins/rink: Let the user disable rink connecting to the internet #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion plugins/rink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ crate-type = ["cdylib"]
[dependencies]
anyrun-plugin = { path = "../../anyrun-plugin" }
abi_stable = "0.11.1"
rink-core = "0.6"
reqwest = { version = "0.11.13", default-features = false, features = ["blocking", "json", "rustls-tls"] }
rink-core = "0.6"
ron = "0.8.0"
serde = { version = "1.0.159", features = ["derive"] }
12 changes: 11 additions & 1 deletion plugins/rink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ A simple calculator plugin powered by [Rink](https://github.com/tiffany352/rink-

## Usage

Just type in your calculations/unit conversions.
Just type in your calculations/unit conversions.

## Configuration

```ron
// <Anyrun config dir>/rink.ron
Config(
// Pull currency conversions from https://rinkcalc.app/data/currency.json
pull_currencies: true,
)
```
70 changes: 51 additions & 19 deletions plugins/rink/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,67 @@
use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::*;
use rink_core::{ast, date, gnu_units, CURRENCY_FILE};
use serde::Deserialize;
use std::fs;

#[derive(Deserialize)]
pub struct Config {
pull_currencies: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
pull_currencies: true,
}
}
}

pub struct State {
config: Config,
ctx: rink_core::Context,
}

#[init]
fn init(_config_dir: RString) -> rink_core::Context {
fn init(config_dir: RString) -> State {

let config: Config = match fs::read_to_string(format!("{}/rink.ron", config_dir)) {
Ok(content) => ron::from_str(&content).unwrap_or_else(|why| {
eprintln!("Error parsing rink plugin config: {}", why);
Config::default()
}),
Err(why) => {
eprintln!("Error reading rink plugin config: {}", why);
Config::default()
}
};

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),
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::<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(ast::Defs {
defs: currency_defs,
});
}

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
State { config, ctx }
}

#[info]
Expand All @@ -41,8 +73,8 @@ fn info() -> PluginInfo {
}

#[get_matches]
fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec<Match> {
match rink_core::one_line(ctx, &input) {
fn get_matches(input: RString, state: &mut State) -> RVec<Match> {
match rink_core::one_line(&mut state.ctx, &input) {
Ok(result) => {
let (title, desc) = parse_result(result);
vec![Match {
Expand Down