Skip to content

Commit

Permalink
Use directories crate to store the rates
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualdesigner committed Sep 23, 2023
1 parent ecf3fb4 commit f97821f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
28 changes: 28 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = "0.11.20"
tokio = { version = "1.32.0", features = ["full"] }
directories = "5.0.1"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
70 changes: 35 additions & 35 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,27 @@ pub mod commands;

use base::{CryptoRates, FiatRates, Storage};
use commands::{calculate_currency_total, calculate_exchange_rates, get_rates};
use directories::ProjectDirs;
use reqwest;
use std::io::{Read, Write};
use std::{collections::HashMap as Map, fs::File};
use std::{collections::HashMap as Map, fs::create_dir_all, fs::File};
use tokio;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command

const FIAT_RATES_FETCH_URL: &str = "https://raw.githubusercontent.com/ARK-Builders/ark-exchange-rates/main/fiat-rates.json";
const CRYPTO_RATES_FETCH_URL: &str = "https://raw.githubusercontent.com/ARK-Builders/ark-exchange-rates/main/crypto-rates.json";

const FIAT_RATES_FETCH_URL: &str =
"https://raw.githubusercontent.com/ARK-Builders/ark-exchange-rates/main/fiat-rates.json";
const CRYPTO_RATES_FETCH_URL: &str =
"https://raw.githubusercontent.com/ARK-Builders/ark-exchange-rates/main/crypto-rates.json";

async fn fetch_rates() -> Result<(Map<String, f32>, Vec<CryptoRates>), reqwest::Error> {
let fiat_rates = reqwest::get(
FIAT_RATES_FETCH_URL,
)
.await?
.text()
.await?;
let fiat_rates = reqwest::get(FIAT_RATES_FETCH_URL).await?.text().await?;

if fiat_rates.is_empty() {
panic!("No fiat rates found.");
}

let fiat_rates: FiatRates = serde_json::from_str(&fiat_rates).unwrap();

let crypto_rates = reqwest::get(
CRYPTO_RATES_FETCH_URL,
)
.await?
.text()
.await?;
let crypto_rates = reqwest::get(CRYPTO_RATES_FETCH_URL).await?.text().await?;

if crypto_rates.is_empty() {
panic!("No crypto rates found.");
Expand All @@ -49,27 +40,36 @@ async fn fetch_rates() -> Result<(Map<String, f32>, Vec<CryptoRates>), reqwest::
#[tokio::main]
async fn main() {
let mut rates: Map<String, f32>;
let mut rates_file = File::options()
.read(true)
.write(true)
.open("rates")
.unwrap_or_else(|_| File::create("rates").unwrap());
let result = fetch_rates().await;
if let Some(proj_dirs) = ProjectDirs::from("com", "ark-builders", "ark-rate-desktop") {
let path = proj_dirs.cache_dir();
let _ = create_dir_all(&path)
.expect("Failed to create the missing directories for storing the rates");
let path = path.join("rates");
let mut rates_file = File::options()
.read(true)
.write(true)
.open(&path)
.unwrap_or_else(|_| File::create(&path).unwrap());

let result = fetch_rates().await;

match result {
Ok((fiat_rates, crypto_rates)) => {
rates = fiat_rates;
for crypto in crypto_rates {
rates.insert(crypto.symbol.to_uppercase(), 1.0 / crypto.current_price);
match result {
Ok((fiat_rates, crypto_rates)) => {
rates = fiat_rates;
for crypto in crypto_rates {
rates.insert(crypto.symbol.to_uppercase(), 1.0 / crypto.current_price);
}
let bytes = serde_json::to_string(&rates).unwrap_or(String::new());
rates_file.write_all(bytes.as_bytes()).unwrap();
}
Err(_) => {
let mut unparsed_rates = String::new();
rates_file.read_to_string(&mut unparsed_rates).unwrap_or(0);
rates = serde_json::from_str(&unparsed_rates).unwrap_or_default();
}
let bytes = serde_json::to_string(&rates).unwrap_or(String::new());
rates_file.write_all(bytes.as_bytes()).unwrap();
}
Err(_) => {
let mut unparsed_rates = String::new();
rates_file.read_to_string(&mut unparsed_rates).unwrap_or(0);
rates = serde_json::from_str(&unparsed_rates).unwrap_or_default();
}
} else {
panic!("No valid home directory path could be retrieved from the operating system.")
}

tauri::Builder::default()
Expand Down

0 comments on commit f97821f

Please sign in to comment.