Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions factorion-bot-discord/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions factorion-bot-reddit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
87 changes: 47 additions & 40 deletions factorion-bot-reddit/src/reddit_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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<Url> = 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() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -456,8 +460,9 @@ impl RedditClient {

let response_text = &response.text().await?;
let response_text = response_text.as_str();
let response_json =
from_str::<Value>(response_text).expect("Failed to convert response to json");
let response_json = from_str::<Value>(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);
Expand Down Expand Up @@ -518,8 +523,9 @@ impl RedditClient {
}

fn is_success(response_text: &str) -> bool {
let response_json =
from_str::<Value>(response_text).expect("Failed to convert response to json");
let response_json = from_str::<Value>(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)
}
Expand Down Expand Up @@ -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::<Value>(&jwt_payload).expect("Failed to convert jwt payload to json");
let jwt_payload = from_str::<Value>(&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<Utc> = DateTime::from_utc(naive, Utc);

Expand Down
4 changes: 2 additions & 2 deletions factorion-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions factorion-lib/src/calculation_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion factorion-math/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 4 additions & 4 deletions factorion-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down