Skip to main content

nil_server_types/
round.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::time::Minutes;
5use serde::{Deserialize, Serialize};
6use std::time::Duration;
7
8/// Round duration, in minutes.
9#[derive(Copy, Debug, Deserialize, Serialize)]
10#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
11#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
12pub struct RoundDuration(Minutes);
13
14impl RoundDuration {
15  pub const MIN: RoundDuration = Self(Minutes::new(5));
16  pub const MAX: RoundDuration = Self(Minutes::new(60 * 12));
17}
18
19impl RoundDuration {
20  pub const fn new(mins: u64) -> Self {
21    Self::from(Minutes::new(mins))
22  }
23}
24
25impl const Default for RoundDuration {
26  fn default() -> Self {
27    Self::MIN
28  }
29}
30
31impl const From<Minutes> for RoundDuration {
32  fn from(mins: Minutes) -> Self {
33    Self(mins.clamp(Self::MIN.0, Self::MAX.0))
34  }
35}
36
37impl const From<RoundDuration> for Minutes {
38  fn from(duration: RoundDuration) -> Self {
39    duration.0
40  }
41}
42
43impl const From<Duration> for RoundDuration {
44  fn from(duration: Duration) -> Self {
45    Self::from(Minutes::from(duration))
46  }
47}
48
49impl const From<RoundDuration> for Duration {
50  fn from(duration: RoundDuration) -> Self {
51    Duration::from(duration.0)
52  }
53}