mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-12-20 03:10:38 +01:00
breakout async_main
This commit is contained in:
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -1236,21 +1236,9 @@ dependencies = [
|
|||||||
"mio",
|
"mio",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tokio-macros"
|
|
||||||
version = "2.6.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-native-tls"
|
name = "tokio-native-tls"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|||||||
@@ -34,4 +34,4 @@ reqwest = "0.12"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
smol_str = { version = "0.3", features = ["serde"] }
|
smol_str = { version = "0.3", features = ["serde"] }
|
||||||
tokio = { version = "1.48", features = ["macros"] }
|
tokio = "1.48"
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use std::io::{BufReader, BufWriter};
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use super::CacheLine;
|
use super::CacheLine;
|
||||||
use crate::os::Os;
|
|
||||||
use crate::View;
|
use crate::View;
|
||||||
|
use crate::os::Os;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use chrono::serde::ts_seconds;
|
|||||||
use chrono::{DateTime, Datelike, FixedOffset, Local, NaiveDate, TimeDelta, Utc, Weekday};
|
use chrono::{DateTime, Datelike, FixedOffset, Local, NaiveDate, TimeDelta, Utc, Weekday};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::models::ExchangeRateResult;
|
|
||||||
use crate::Hollidays;
|
use crate::Hollidays;
|
||||||
|
use crate::models::ExchangeRateResult;
|
||||||
|
|
||||||
const CET: FixedOffset = unsafe { FixedOffset::east_opt(3600).unwrap_unchecked() };
|
const CET: FixedOffset = unsafe { FixedOffset::east_opt(3600).unwrap_unchecked() };
|
||||||
|
|
||||||
|
|||||||
66
src/main.rs
66
src/main.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use anyhow::Context;
|
||||||
use clap::Parser as _;
|
use clap::Parser as _;
|
||||||
use ecb_rates::caching::{Cache, CacheLine};
|
use ecb_rates::caching::{Cache, CacheLine};
|
||||||
use ecb_rates::HeaderDescription;
|
use ecb_rates::HeaderDescription;
|
||||||
@@ -17,22 +18,40 @@ async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result<Vec<ExchangeRateResu
|
|||||||
parse(&xml_content)
|
parse(&xml_content)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
fn main() -> ExitCode {
|
||||||
async fn main() -> ExitCode {
|
let cli = Cli::parse();
|
||||||
let mut cli = Cli::parse();
|
|
||||||
|
let runtime = match tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
{
|
||||||
|
Ok(runtime) => runtime,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Failed to initialize asynchronous runtime: {:?}", e);
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match runtime.block_on(async_main(cli)) {
|
||||||
|
Ok(_) => ExitCode::SUCCESS,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Fatal: {:?}", e);
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn async_main(mut cli: Cli) -> anyhow::Result<()> {
|
||||||
if cli.force_color {
|
if cli.force_color {
|
||||||
colored::control::set_override(true);
|
colored::control::set_override(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut header_description = HeaderDescription::new();
|
let mut header_description = HeaderDescription::new();
|
||||||
let use_cache = !cli.no_cache;
|
let use_cache = !cli.no_cache;
|
||||||
let view = match cli.show_days.to_view() {
|
let view = cli
|
||||||
Some(v) => v,
|
.show_days
|
||||||
None => {
|
.to_view()
|
||||||
eprintln!("It doesn't make any sence to fetch 0 days right?");
|
.context("It doesn't make any sence to fetch 0 days right?")?;
|
||||||
return ExitCode::SUCCESS;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut cache = if use_cache { Cache::load(&view) } else { None };
|
let mut cache = if use_cache { Cache::load(&view) } else { None };
|
||||||
let cache_ok = cache.as_ref().map_or_else(
|
let cache_ok = cache.as_ref().map_or_else(
|
||||||
|| false,
|
|| false,
|
||||||
@@ -48,14 +67,9 @@ async fn main() -> ExitCode {
|
|||||||
.exchange_rate_results
|
.exchange_rate_results
|
||||||
.clone()
|
.clone()
|
||||||
} else {
|
} else {
|
||||||
let parsed = match get_and_parse(view.to_ecb_url()).await {
|
let parsed = get_and_parse(view.to_ecb_url())
|
||||||
Ok(k) => k,
|
.await
|
||||||
Err(e) => {
|
.context("Failed to get/parse data from ECB")?;
|
||||||
eprintln!("Failed to get/parse data from ECB: {}", e);
|
|
||||||
return ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if !cache_ok {
|
if !cache_ok {
|
||||||
let not_equal_cache = cache.as_ref().map_or_else(
|
let not_equal_cache = cache.as_ref().map_or_else(
|
||||||
|| true,
|
|| true,
|
||||||
@@ -66,13 +80,10 @@ async fn main() -> ExitCode {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if not_equal_cache
|
if not_equal_cache && let Some(cache_safe) = cache.as_mut() {
|
||||||
&& let Some(cache_safe) = cache.as_mut() {
|
|
||||||
let cache_line = CacheLine::new(parsed.clone());
|
let cache_line = CacheLine::new(parsed.clone());
|
||||||
cache_safe.set_cache_line(cache_line);
|
cache_safe.set_cache_line(cache_line);
|
||||||
if let Err(e) = cache_safe.save() {
|
cache_safe.save()?;
|
||||||
eprintln!("Failed to save to cache with: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parsed
|
parsed
|
||||||
@@ -81,11 +92,8 @@ async fn main() -> ExitCode {
|
|||||||
cli.perspective = cli.perspective.map(|s| s.to_uppercase_smolstr());
|
cli.perspective = cli.perspective.map(|s| s.to_uppercase_smolstr());
|
||||||
if let Some(currency) = cli.perspective.as_ref() {
|
if let Some(currency) = cli.perspective.as_ref() {
|
||||||
header_description.replace_eur(currency);
|
header_description.replace_eur(currency);
|
||||||
let error_occured = change_perspective(&mut parsed, currency).is_none();
|
change_perspective(&mut parsed, currency)
|
||||||
if error_occured {
|
.context("The currency wasn't in the data from the ECB!")?;
|
||||||
eprintln!("The currency wasn't in the data from the ECB!");
|
|
||||||
return ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.should_invert {
|
if cli.should_invert {
|
||||||
@@ -160,5 +168,5 @@ async fn main() -> ExitCode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
println!("{}", &output);
|
println!("{}", &output);
|
||||||
ExitCode::SUCCESS
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use quick_xml::events::Event;
|
|
||||||
use quick_xml::Reader;
|
use quick_xml::Reader;
|
||||||
|
use quick_xml::events::Event;
|
||||||
use smol_str::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
use crate::models::ExchangeRateResult;
|
use crate::models::ExchangeRateResult;
|
||||||
@@ -68,8 +68,7 @@ pub fn parse(xml: &str) -> anyhow::Result<Vec<ExchangeRateResult>> {
|
|||||||
*inside_cube_time = true;
|
*inside_cube_time = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if *inside_cube_time
|
if *inside_cube_time && let (Some(c), Some(r_str)) = (currency_attr, rate_attr) {
|
||||||
&& let (Some(c), Some(r_str)) = (currency_attr, rate_attr) {
|
|
||||||
let r = r_str.parse::<f64>()?;
|
let r = r_str.parse::<f64>()?;
|
||||||
current_rates.insert(c, r);
|
current_rates.insert(c, r);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ use std::fmt::Display;
|
|||||||
|
|
||||||
use smol_str::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
|
use crate::DEFAULT_WIDTH;
|
||||||
use crate::cli::SortBy;
|
use crate::cli::SortBy;
|
||||||
use crate::models::ExchangeRateResult;
|
use crate::models::ExchangeRateResult;
|
||||||
use crate::DEFAULT_WIDTH;
|
|
||||||
|
|
||||||
use super::table_display::helper_table_print;
|
use super::table_display::helper_table_print;
|
||||||
use super::{TableGet, TableTrait};
|
use super::{TableGet, TableTrait};
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use crate::DEFAULT_WIDTH;
|
||||||
use crate::cli::SortBy;
|
use crate::cli::SortBy;
|
||||||
use crate::models::ExchangeRateResult;
|
use crate::models::ExchangeRateResult;
|
||||||
use crate::DEFAULT_WIDTH;
|
|
||||||
|
|
||||||
|
use super::Table;
|
||||||
use super::table_display::helper_table_print;
|
use super::table_display::helper_table_print;
|
||||||
use super::table_getter::TableGet;
|
use super::table_getter::TableGet;
|
||||||
use super::table_trait::TableTrait;
|
use super::table_trait::TableTrait;
|
||||||
use super::Table;
|
|
||||||
|
|
||||||
pub struct TableRef<'a> {
|
pub struct TableRef<'a> {
|
||||||
header: Option<&'a str>,
|
header: Option<&'a str>,
|
||||||
|
|||||||
Reference in New Issue
Block a user