diff --git a/Cargo.lock b/Cargo.lock index bacc351..32aa519 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -399,7 +399,7 @@ dependencies = [ [[package]] name = "factorion-bot-discord" -version = "2.5.1" +version = "2.5.2" dependencies = [ "anyhow", "dotenvy", @@ -414,7 +414,7 @@ dependencies = [ [[package]] name = "factorion-bot-reddit" -version = "5.6.1" +version = "5.6.2" dependencies = [ "anyhow", "base64 0.22.1", @@ -434,7 +434,7 @@ dependencies = [ [[package]] name = "factorion-lib" -version = "5.0.1" +version = "5.0.2" dependencies = [ "arbtest", "chrono", @@ -447,7 +447,7 @@ dependencies = [ [[package]] name = "factorion-math" -version = "1.0.2" +version = "1.0.3" dependencies = [ "gmp-mpfr-sys", "rug", diff --git a/factorion-bot-discord/Cargo.toml b/factorion-bot-discord/Cargo.toml index 194f4c4..8e57bed 100644 --- a/factorion-bot-discord/Cargo.toml +++ b/factorion-bot-discord/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "factorion-bot-discord" -version = "2.5.1" +version = "2.5.2" edition = "2024" description = "factorion-bot (for factorials and related) on Discord" license = "MIT" @@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math", "discord"] categories = ["mathematics", "web-programming", "parser-implementations"] [dependencies] -factorion-lib = { path = "../factorion-lib", version = "5.0.1", features = ["serde", "influxdb"] } +factorion-lib = { path = "../factorion-lib", version = "5.0.2", features = ["serde", "influxdb"] } serenity = { version = "0.12", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] } tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "time"] } dotenvy = "^0.15.7" diff --git a/factorion-bot-reddit/Cargo.toml b/factorion-bot-reddit/Cargo.toml index c058176..a3ca97e 100644 --- a/factorion-bot-reddit/Cargo.toml +++ b/factorion-bot-reddit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "factorion-bot-reddit" -version = "5.6.1" +version = "5.6.2" edition = "2024" description = "factorion-bot (for factorials and related) on Reddit" license = "MIT" @@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math"] categories = ["mathematics", "web-programming", "parser-implementations"] [dependencies] -factorion-lib = {path = "../factorion-lib", version = "5.0.1", features = ["serde", "influxdb"]} +factorion-lib = {path = "../factorion-lib", version = "5.0.2", features = ["serde", "influxdb"]} reqwest = { version = "0.12.28", features = ["json", "native-tls"], default-features = false } serde = { version = "1.0.219", default-features = false, features = ["derive"] } serde_json = "1.0.140" diff --git a/factorion-bot-reddit/src/reddit_api.rs b/factorion-bot-reddit/src/reddit_api.rs index 5f7e830..bde285e 100644 --- a/factorion-bot-reddit/src/reddit_api.rs +++ b/factorion-bot-reddit/src/reddit_api.rs @@ -11,7 +11,7 @@ use crate::{ use anyhow::{Error, anyhow}; use base64::Engine; use base64::engine::general_purpose::STANDARD_NO_PAD; -use chrono::{DateTime, NaiveDateTime, Utc}; +use chrono::{DateTime, NaiveDateTime, TimeDelta, Utc}; use factorion_lib::comment::{Commands, Comment, CommentCalculated, CommentConstructed, Status}; use futures::future::OptionFuture; use id::{DenseId, id_to_dense}; @@ -126,16 +126,17 @@ impl RedditClient { subreddits.sort(); info!("Setting comments to be checked in: {subreddits:?}"); if !(subreddits.is_empty() || subreddits == [""]) { + let url = format!( + "{}/r/{}/comments", + REDDIT_OAUTH_URL, + subreddits + .into_iter() + .reduce(|a, e| format!("{a}+{e}")) + .unwrap_or_default(), + ); Some( - Url::parse(&format!( - "{}/r/{}/comments", - REDDIT_OAUTH_URL, - subreddits - .into_iter() - .reduce(|a, e| format!("{a}+{e}")) - .unwrap_or_default(), - )) - .expect("Failed to parse Url"), + Url::parse(&url) + .unwrap_or_else(|err| panic!("Failed to parse Url: {err} in {url}")), ) } else { None @@ -152,23 +153,25 @@ impl RedditClient { post_subreddits.sort(); info!("Setting posts to be checked in: {post_subreddits:?}"); if !(post_subreddits.is_empty() || post_subreddits == [""]) { + let url = format!( + "{}/r/{}/new", + REDDIT_OAUTH_URL, + post_subreddits + .into_iter() + .reduce(|a, e| format!("{a}+{e}")) + .unwrap_or_default(), + ); Some( - Url::parse(&format!( - "{}/r/{}/new", - REDDIT_OAUTH_URL, - post_subreddits - .into_iter() - .reduce(|a, e| format!("{a}+{e}")) - .unwrap_or_default(), - )) - .expect("Failed to parse Url"), + Url::parse(&url) + .unwrap_or_else(|err| panic!("Failed to parse Url: {err} in {url}")), ) } else { None } }); static MENTION_URL: LazyLock = LazyLock::new(|| { - Url::parse(&format!("{REDDIT_OAUTH_URL}/message/inbox")).expect("Failed to parse Url") + let url = format!("{REDDIT_OAUTH_URL}/message/inbox"); + Url::parse(&url).unwrap_or_else(|err| panic!("Failed to parse Url: {err} in {url}")) }); #[cfg(not(test))] if self.is_token_expired() { @@ -337,22 +340,23 @@ impl RedditClient { && !ids.is_empty() { 'get_summons: loop { + let url = format!( + "{}/api/info?id={}", + REDDIT_OAUTH_URL, + ids.iter() + .map(|(id, _)| id) + .fold(String::new(), |mut a, e| { + let _ = write!(a, "{e}"); + a + }) + ); let response = self .client - .get(format!( - "{}/api/info?id={}", - REDDIT_OAUTH_URL, - ids.iter() - .map(|(id, _)| id) - .fold(String::new(), |mut a, e| { - let _ = write!(a, "{e}"); - a - }) - )) + .get(&url) .bearer_auth(&self.token.access_token) .send() .await - .expect("Failed to get comment"); + .unwrap_or_else(|err| panic!("Failed to get comment: {err} on {url}")); if Self::check_response_status(&response).is_ok() { let (comments, _, t, _) = self .extract_comments( @@ -406,7 +410,7 @@ impl RedditClient { } fn is_token_expired(&self) -> bool { - let now = Utc::now(); + let now = Utc::now() + TimeDelta::seconds(1); now > self.token.expiration_time } @@ -456,8 +460,9 @@ impl RedditClient { let response_text = &response.text().await?; let response_text = response_text.as_str(); - let response_json = - from_str::(response_text).expect("Failed to convert response to json"); + let response_json = from_str::(response_text).unwrap_or_else(|err| { + panic!("Failed to convert response to json: {err} in {response_text}") + }); let response_status_err = !RedditClient::is_success(response_text); let error_message = RedditClient::get_error_message(response_json); @@ -518,8 +523,9 @@ impl RedditClient { } fn is_success(response_text: &str) -> bool { - let response_json = - from_str::(response_text).expect("Failed to convert response to json"); + let response_json = from_str::(response_text).unwrap_or_else(|err| { + panic!("Failed to convert response to json: {err} in {response_text}") + }); response_json["success"].as_bool().unwrap_or(false) } @@ -575,17 +581,18 @@ impl RedditClient { let jwt_payload = jwt[1]; let jwt_payload = STANDARD_NO_PAD .decode(jwt_payload.as_bytes()) - .expect("Failed to decode jwt payload"); + .unwrap_or_else(|err| panic!("Failed to decode jwt payload: {err} in {jwt_payload}")); let jwt_payload = String::from_utf8(jwt_payload).expect("Failed to convert jwt payload to string"); - let jwt_payload = - from_str::(&jwt_payload).expect("Failed to convert jwt payload to json"); + let jwt_payload = from_str::(&jwt_payload).unwrap_or_else(|err| { + panic!("Failed to convert jwt payload to json: {err} in {jwt_payload}") + }); let exp = jwt_payload["exp"] .as_f64() - .expect("Failed to get exp field"); + .unwrap_or_else(|| panic!("Failed to get exp field as f64 in {jwt_payload}")); let naive = NaiveDateTime::from_timestamp(exp as i64, 0); let datetime: DateTime = DateTime::from_utc(naive, Utc); diff --git a/factorion-lib/Cargo.toml b/factorion-lib/Cargo.toml index f8a61c4..a0b7436 100644 --- a/factorion-lib/Cargo.toml +++ b/factorion-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "factorion-lib" -version = "5.0.1" +version = "5.0.2" edition = "2024" description = "A library used to create bots to recognize and calculate factorials and related concepts" license = "MIT" @@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math"] categories = ["mathematics", "web-programming", "parser-implementations"] [dependencies] -factorion-math = {path = "../factorion-math", version = "1.0.2"} +factorion-math = {path = "../factorion-math", version = "1.0.3"} serde = {version = "1.0", features = ["derive"], optional = true} serde_json = {version = "1.0", optional = true} concat-idents = "1.1.4" diff --git a/factorion-lib/src/calculation_tasks.rs b/factorion-lib/src/calculation_tasks.rs index 354328c..db20c22 100644 --- a/factorion-lib/src/calculation_tasks.rs +++ b/factorion-lib/src/calculation_tasks.rs @@ -300,7 +300,9 @@ impl CalculationJob { factorial.1, ) } else { - let calc_num = calc_num.to_u64().expect("Failed to convert BigInt to u64"); + let calc_num = calc_num + .to_u64() + .unwrap_or_else(|| panic!("Failed to convert BigInt to u64: {calc_num}")); let factorial = math::factorial(calc_num, level as u32) * if negative % 2 != 0 { -1 } else { 1 }; CalculationResult::Exact(factorial) @@ -318,7 +320,9 @@ impl CalculationJob { factorial.1, ) } else { - let calc_num = calc_num.to_u64().expect("Failed to convert BigInt to u64"); + let calc_num = calc_num + .to_u64() + .unwrap_or_else(|| panic!("Failed to convert BigInt to u64: {calc_num}")); let factorial = math::subfactorial(calc_num) * if negative % 2 != 0 { -1 } else { 1 }; CalculationResult::Exact(factorial) diff --git a/factorion-math/Cargo.toml b/factorion-math/Cargo.toml index ac5aee6..2d3a054 100644 --- a/factorion-math/Cargo.toml +++ b/factorion-math/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "factorion-math" -version = "1.0.2" +version = "1.0.3" edition = "2024" description = "The math (factorials and related functions) used by factorion" license = "MIT" diff --git a/factorion-math/src/lib.rs b/factorion-math/src/lib.rs index 734d564..e14c99d 100644 --- a/factorion-math/src/lib.rs +++ b/factorion-math/src/lib.rs @@ -152,7 +152,7 @@ pub fn approximate_factorial_float(n: Float) -> (Float, Integer) { let ten_in_base = Float::with_val(prec, 10).ln() / base.clone().ln(); let (extra, _) = (n.clone() / ten_in_base.clone()) .to_integer_round(rug::float::Round::Down) - .expect("Got non-finite number, n is likely non-positive"); + .unwrap_or_else(|| panic!("Got non-finite number, n was {n}")); let exponent = n.clone() - ten_in_base * Float::with_val(prec, extra.clone()); let factorial = base.pow(exponent) * (Float::with_val(prec, rug::float::Constant::Pi) * Float::with_val(prec, 2) * n.clone()) @@ -313,9 +313,9 @@ pub fn approximate_multifactorial_digits_float(k: u32, n: Float) -> Integer { let k = Float::with_val(prec, k); let ln10 = Float::with_val(prec, 10).ln(); let base = n.clone().ln() / &ln10; - ((Float::with_val(prec, 0.5) + n.clone() / k.clone()) * base - n / k / ln10) + ((Float::with_val(prec, 0.5) + n.clone() / k.clone()) * base - n.clone() / k / ln10) .to_integer_round(rug::float::Round::Down) - .expect("Got non-finite number, n is likely non-positive") + .unwrap_or_else(|| panic!("Got non-finite number, n was {n}")) .0 + Integer::ONE } @@ -341,7 +341,7 @@ pub fn adjust_approximate((x, e): (Float, Integer)) -> (Float, Integer) { let prec = x.prec(); let (extra, _) = (x.clone().ln() / Float::with_val(prec, 10).ln()) .to_integer_round(rug::float::Round::Down) - .expect("Got non-finite number, x is likely not finite"); + .unwrap_or_else(|| panic!("Got non-finite number, x was {x}")); let x = x / (Float::with_val(prec, 10).pow(extra.clone())); let total_exponent = extra + e; (x, total_exponent)