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#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
9pub struct RoundDuration(Minutes);
10
11impl RoundDuration {
12  pub const MIN: RoundDuration = Self(Minutes::new(5));
13  pub const MAX: RoundDuration = Self(Minutes::new(60 * 12));
14}
15
16impl RoundDuration {
17  pub fn new(mins: u64) -> Self {
18    Self::from(Minutes::new(mins))
19  }
20}
21
22impl Default for RoundDuration {
23  fn default() -> Self {
24    Self::MIN
25  }
26}
27
28impl From<Minutes> for RoundDuration {
29  fn from(mins: Minutes) -> Self {
30    Self(mins).clamp(Self::MIN, Self::MAX)
31  }
32}
33
34impl From<RoundDuration> for Minutes {
35  fn from(duration: RoundDuration) -> Self {
36    duration.0
37  }
38}
39
40impl From<Duration> for RoundDuration {
41  fn from(duration: Duration) -> Self {
42    Self::from(Minutes::from(duration))
43  }
44}
45
46impl From<RoundDuration> for Duration {
47  fn from(duration: RoundDuration) -> Self {
48    Duration::from(duration.0)
49  }
50}