Skip to content
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
13 changes: 12 additions & 1 deletion plugins/rink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ A simple calculator plugin powered by [Rink](https://github.com/tiffany352/rink-

## Usage

Just type in your calculations/unit conversions.
Type in the configured prefix (default is no prefix), then just type in your calculations/unit conversions.

## Configuration

```ron
// <Anyrun config dir>/rink.ron
Config(
prefix: "",
// Pull currency conversions from https://rinkcalc.app/data/currency.json
pull_currencies: true,
)
```
55 changes: 30 additions & 25 deletions plugins/rink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use std::fs;
#[derive(Deserialize, Debug)]
struct Config {
prefix: String,
pull_currencies: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a #[serde(default = ...)] annotation with a reasonable value (true should be a reasonable default, and preserves existing functionality). Otherwise existing configs would break.

}

impl Default for Config {
fn default() -> Self {
Config {
prefix: "".to_string(),
pull_currencies: true,
}
}
}
Expand All @@ -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::<ast::Defs>() {
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}");
Expand All @@ -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::<ast::Defs>() {
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 }
}

Expand Down
Loading