Skip to content
This repository was archived by the owner on Mar 24, 2024. It is now read-only.

Commit 8f32730

Browse files
committed
use humantime to parse patience duration
1 parent 6e16d8e commit 8f32730

5 files changed

Lines changed: 53 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ tinyvec = { version = "1.5", features = ["alloc"] }
4444
tracing = "0.1"
4545
tracing-opentelemetry = { version = "0.18", optional = true }
4646
tracing-subscriber = { version = "0.3", features = ["json"] }
47+
humantime-serde = "1.1.1"
4748

4849
[dependencies.config]
4950
version = "0.13"

example_config.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ private = false
1212
[behavior]
1313
# Maximum amount of keywords users can subscribe to
1414
max_keywords = 100
15-
# Amount of seconds to wait for activity before sending a notification
16-
patience_seconds = 120
15+
# Amount of time to wait for activity before sending a notification
16+
# Other examples: "1m 30sec", "5minutes"
17+
# See https://docs.rs/humantime/latest/humantime/fn.parse_duration.html for complete list
18+
patience = "2m"
1719

1820
[logging]
1921
# Discord webhook to send errors and panics to

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 ThatsNoMoon
1+
// Copyright 2023 ThatsNoMoon
22
// Licensed under the Open Software License version 3.0
33

44
//! Highlights is a simple but flexible keyword highlighting bot for Discord.
@@ -9,6 +9,9 @@
99
#![allow(clippy::tabs_in_doc_comments)]
1010

1111
use anyhow::Result;
12+
use tracing::warn;
13+
14+
use crate::settings::settings;
1215

1316
pub(crate) mod db;
1417

@@ -29,6 +32,14 @@ async fn main() -> Result<()> {
2932

3033
logging::init()?;
3134

35+
if settings().behavior.patience_seconds.is_some() {
36+
warn!(
37+
"Your configuration includes behavior.patience_seconds. \
38+
This setting is deprecated; please use behavior.patience instead. \
39+
For example, patience = \"2m\"."
40+
);
41+
}
42+
3243
db::init().await?;
3344

3445
#[cfg(feature = "bot")]

src/settings.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 joshyrobot, ThatsNoMoon
1+
// Copyright 2023 joshyrobot, ThatsNoMoon
22
// Licensed under the Open Software License version 3.0
33

44
//! Handling of bot configuration for hosters.
@@ -48,11 +48,11 @@ mod duration_de {
4848
}
4949
pub(super) fn deserialize_duration<'de, D>(
5050
d: D,
51-
) -> Result<Duration, D::Error>
51+
) -> Result<Option<Duration>, D::Error>
5252
where
5353
D: Deserializer<'de>,
5454
{
55-
d.deserialize_u64(DurationVisitor)
55+
d.deserialize_u64(DurationVisitor).map(Some)
5656
}
5757
}
5858

@@ -254,12 +254,18 @@ pub(crate) struct BehaviorSettings {
254254
pub(crate) max_keywords: u32,
255255

256256
/// Duration to wait for activity before sending a notification.
257+
#[serde(with = "humantime_serde")]
258+
#[cfg(feature = "bot")]
259+
pub(crate) patience: Duration,
260+
261+
/// Deprecated method to specify patience.
257262
#[serde(
258-
rename = "patience_seconds",
259-
deserialize_with = "deserialize_duration"
263+
deserialize_with = "deserialize_duration",
264+
alias = "patienceseconds",
265+
default
260266
)]
261267
#[cfg(feature = "bot")]
262-
pub(crate) patience: Duration,
268+
pub(crate) patience_seconds: Option<Duration>,
263269
}
264270

265271
/// Settings for the account of the bot.
@@ -344,7 +350,7 @@ impl Settings {
344350

345351
#[cfg(feature = "bot")]
346352
let b = b.set_default("behavior.max_keywords", 100i64)?
347-
.set_default("behavior.patience_seconds", 60i64 * 2)?
353+
.set_default("behavior.patience", "2m")?
348354
.set_default("bot.private", false)?;
349355

350356
#[cfg(feature = "monitoring")]
@@ -371,6 +377,12 @@ impl Settings {
371377
b.add_source(Environment::with_prefix("HIGHLIGHTS").separator("_"))
372378
.build()?
373379
.try_deserialize()
380+
.map(|mut settings: Settings| {
381+
if let Some(old) = settings.behavior.patience_seconds {
382+
settings.behavior.patience = old;
383+
}
384+
settings
385+
})
374386
}
375387
}
376388

0 commit comments

Comments
 (0)