From b3347ad3d4c5b978a67e9e9f2955845a5233e111 Mon Sep 17 00:00:00 2001 From: materus Date: Thu, 21 Mar 2024 11:33:32 +0100 Subject: [PATCH] Get rid of tokio and rust_decimal_macros, use blocking reqwest --- Cargo.lock | 223 +++++++++++++++++++++++++----------------------- Cargo.toml | 6 +- src/exchange.rs | 11 ++- src/main.rs | 18 ++-- src/requests.rs | 22 +++-- 5 files changed, 140 insertions(+), 140 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1a46e7..5ae07f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,9 +135,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] name = "bitvec" @@ -297,11 +297,9 @@ dependencies = [ "reqwest", "rusqlite", "rust_decimal", - "rust_decimal_macros", "rusty-money", "serde", "serde_json", - "tokio", ] [[package]] @@ -390,6 +388,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -398,6 +397,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-sink" version = "0.3.30" @@ -417,9 +422,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", + "futures-io", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -441,9 +450,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" -version = "0.3.25" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" +checksum = "51ee2dd2e4f378392eeff5d51618cd9a63166a2513846bbc55f21cfacd9199d4" dependencies = [ "bytes", "fnv", @@ -500,9 +509,9 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "http" -version = "0.2.12" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", "fnv", @@ -511,12 +520,24 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.6" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", "pin-project-lite", ] @@ -526,47 +547,60 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - [[package]] name = "hyper" -version = "0.14.28" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a" dependencies = [ "bytes", "futures-channel", - "futures-core", "futures-util", "h2", "http", "http-body", "httparse", - "httpdate", "itoa", "pin-project-lite", - "socket2", + "smallvec", "tokio", - "tower-service", - "tracing", "want", ] [[package]] name = "hyper-tls" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", + "http-body-util", "hyper", + "hyper-util", "native-tls", "tokio", "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "pin-project-lite", + "socket2", + "tokio", + "tower", + "tower-service", + "tracing", ] [[package]] @@ -639,16 +673,6 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" -[[package]] -name = "lock_api" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.21" @@ -745,7 +769,7 @@ version = "0.10.64" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "cfg-if", "foreign-types", "libc", @@ -783,35 +807,32 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets 0.48.5", -] - [[package]] name = "percent-encoding" version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.53", +] + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -942,15 +963,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "rend" version = "0.4.2" @@ -962,20 +974,23 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.26" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78bf93c4af7a8bb7d879d51cebe797356ff10ae8516ace542b5182d9dcac10b2" +checksum = "58b48d98d932f4ee75e541614d32a7f44c889b72bd9c2e04d95edd135989df88" dependencies = [ "base64", "bytes", "encoding_rs", + "futures-channel", "futures-core", "futures-util", "h2", "http", "http-body", + "http-body-util", "hyper", "hyper-tls", + "hyper-util", "ipnet", "js-sys", "log", @@ -1035,7 +1050,7 @@ version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "fallible-iterator", "fallible-streaming-iterator", "hashlink", @@ -1077,11 +1092,11 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "errno", "libc", "linux-raw-sys", @@ -1122,12 +1137,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "seahash" version = "4.1.0" @@ -1200,15 +1209,6 @@ dependencies = [ "serde", ] -[[package]] -name = "signal-hook-registry" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" -dependencies = [ - "libc", -] - [[package]] name = "simdutf8" version = "0.1.4" @@ -1226,9 +1226,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -1351,25 +1351,11 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", - "tokio-macros", "windows-sys 0.48.0", ] -[[package]] -name = "tokio-macros" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.53", -] - [[package]] name = "tokio-native-tls" version = "0.3.1" @@ -1411,6 +1397,28 @@ dependencies = [ "winnow", ] +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + [[package]] name = "tower-service" version = "0.3.2" @@ -1423,6 +1431,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-core", ] @@ -1482,9 +1491,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uuid" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" +checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" [[package]] name = "vcpkg" diff --git a/Cargo.toml b/Cargo.toml index 918f640..0699d0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,11 +7,9 @@ edition = "2021" [dependencies] clap = { version = "4.5.2", features = [ "derive" ] } -reqwest = "0.11.25" +reqwest = { version = "0.12.0", features = [ "blocking" ] } rusqlite = { version = "0.31.0", features = ["bundled", "array"] } rusty-money = "0.4.1" serde = { version ="1.0.197", features = ["derive"] } serde_json = "1.0.114" -tokio = { version = "1.36.0", features = ["full"] } -rust_decimal = "1.34" -rust_decimal_macros = "1.34" \ No newline at end of file +rust_decimal = "1.34" \ No newline at end of file diff --git a/src/exchange.rs b/src/exchange.rs index 0814240..c0dfb3c 100644 --- a/src/exchange.rs +++ b/src/exchange.rs @@ -1,12 +1,11 @@ use crate::*; use rust_decimal::prelude::*; use rusty_money::{iso::find, ExchangeRate, Money}; -pub async fn update_rate(code: &String) { +pub fn update_rate(code: &String) { if cache::get_next_update(code).expect("Error getting next update time from cache") <= config::get_current_time() { let status = requests::get_rates(code) - .await .expect("Error while fetching rates"); if status == requests::Status::INVALID { panic!("Invalid api key when getting rates") @@ -17,7 +16,7 @@ pub async fn update_rate(code: &String) { } } } -pub async fn get_rate(code_from: &String, code_to: &String) -> String { +pub fn get_rate(code_from: &String, code_to: &String) -> String { if !cache::check_code(code_from).expect("Error on getting code status") { panic!("Code {} doesn't exists, use correct code!", code_from); } @@ -28,16 +27,16 @@ pub async fn get_rate(code_from: &String, code_to: &String) -> String { || (cache::get_next_update(code_from).expect("Error getting next update time from cache") <= config::get_current_time()) { - update_rate(code_from).await; + update_rate(code_from); } cache::get_rate(code_from, code_to).expect("Error when getting cached rate") } -pub async fn convert_value(code_from: &String, code_to: &String, value: &String) { +pub fn convert_value(code_from: &String, code_to: &String, value: &String) { if value.parse::().is_err() { panic!("{} is not a number!", value); } - let text_rate = get_rate(code_from, code_to).await; + let text_rate = get_rate(code_from, code_to); let from_currency = find(code_from); if from_currency.is_none() { panic!("{} not found in ISO formats", code_from); diff --git a/src/main.rs b/src/main.rs index eba12d5..df7c86c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,9 +45,9 @@ struct Cli { #[arg(short = 'L', long = "list-rates", value_names = ["currency"])] list_rates: Option, } -async fn setup_key(key: String) -> Result> { +fn setup_key(key: String) -> Result> { set_api_key(key)?; - let status = get_currencies().await?; + let status = get_currencies()?; if status == requests::Status::INVALID { set_api_key("".to_string())?; println!("Api Key is invalid"); @@ -64,8 +64,7 @@ async fn setup_key(key: String) -> Result> { Ok(true) } -#[tokio::main] -async fn main() -> Result> { +fn main() -> Result> { let args = Cli::parse(); let all_args = args.currency_from.is_some() && args.currency_to.is_some() && args.value.is_some(); @@ -92,7 +91,6 @@ async fn main() -> Result> { None => {} Some(key) => { let res = setup_key(key) - .await .expect("Unknown error while setting up key"); if !res { return Ok(ExitCode::FAILURE); @@ -125,7 +123,7 @@ async fn main() -> Result> { println!("Code {} not found", code); return Ok(ExitCode::FAILURE); } - exchange::update_rate(&code).await; + exchange::update_rate(&code); let rates = cache::list_rates(&code)?; for rate in rates { println!("{} to {} rate: {}", code, rate[0], rate[1]); @@ -139,14 +137,13 @@ async fn main() -> Result> { &args.currency_to.unwrap().to_uppercase(), &args.value.unwrap(), ) - .await } } else { - interactive().await?; + interactive()?; } Ok(ExitCode::SUCCESS) } -async fn interactive() -> Result<(), Box> { +fn interactive() -> Result<(), Box> { let mut key_setup = cache::get_api_key() .expect("Error while getting api key") .len() @@ -159,7 +156,6 @@ async fn interactive() -> Result<(), Box> { .read_line(&mut key_string) .expect("Did not enter a correct string"); setup_key(key_string.trim().to_string()) - .await .expect("Unknown error while setting up key"); key_setup = cache::get_api_key() .expect("Error while getting api key") @@ -216,7 +212,7 @@ async fn interactive() -> Result<(), Box> { amount_check = true } } - convert_value(&code_from, &code_to, &amount).await; + convert_value(&code_from, &code_to, &amount); Ok(()) } diff --git a/src/requests.rs b/src/requests.rs index d562cf6..427cffa 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -29,18 +29,17 @@ struct Err { error_type: String, } -pub async fn get_rates(code: &String) -> Result { - let response = reqwest::get(format!( +pub fn get_rates(code: &String) -> Result { + let response = reqwest::blocking::get(format!( "{}{}{}{}", get_endpoint(), get_api_key().expect("Error when getting api key from cache"), "/latest/", code.to_uppercase() - )) - .await?; + ))?; if response.status().is_success() { let response: ConversionRates = - serde_json::from_str(&response.text().await?).expect("Error when deserializng"); + serde_json::from_str(&response.text()?).expect("Error when deserializng"); cache::add_rates( response.time_next_update_unix, &response.base_code, @@ -50,7 +49,7 @@ pub async fn get_rates(code: &String) -> Result { return Ok(Status::OK); } else { let err: Err = - serde_json::from_str(&response.text().await?).expect("Error when deserializng"); + serde_json::from_str(&response.text()?).expect("Error when deserializng"); if err.error_type == "invalid-key" { return Ok(Status::INVALID); } else if err.error_type == "quota-reached" { @@ -60,24 +59,23 @@ pub async fn get_rates(code: &String) -> Result { Ok(Status::ERROR) } -pub async fn get_currencies() -> Result { - let response = reqwest::get(format!( +pub fn get_currencies() -> Result { + let response = reqwest::blocking::get(format!( "{}{}{}", get_endpoint(), get_api_key().expect("Error when getting api key from cache"), "/codes" - )) - .await?; + ))?; if response.status().is_success() { let codes: CurrencyCodes = - serde_json::from_str(&response.text().await?).expect("Error when deserializng"); + serde_json::from_str(&response.text()?).expect("Error when deserializng"); for code in codes.supported_codes { cache::add_code(code).expect("Error when adding code to cache"); } return Ok(Status::OK); } else { let err: Err = - serde_json::from_str(&response.text().await?).expect("Error when deserializng"); + serde_json::from_str(&response.text()?).expect("Error when deserializng"); if err.error_type == "invalid-key" { return Ok(Status::INVALID); } else if err.error_type == "quota-reached" {