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 derive_more::Display;
5use serde::{Deserialize, Serialize};
6use std::time::Duration;
7
8/// Round duration, in minutes.
9#[derive(Copy, Debug, Display, Deserialize, Serialize)]
10#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
11#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
12pub struct RoundDuration(u16);
13
14impl RoundDuration {
15  pub const MIN: RoundDuration = Self(5);
16  pub const MAX: RoundDuration = Self(60 * 12);
17}
18
19impl RoundDuration {
20  pub const fn new(mins: u16) -> Self {
21    Self(mins.clamp(Self::MIN.0, Self::MAX.0))
22  }
23}
24
25impl const Default for RoundDuration {
26  fn default() -> Self {
27    Self::MIN
28  }
29}
30
31impl const From<u16> for RoundDuration {
32  fn from(value: u16) -> Self {
33    Self::new(value)
34  }
35}
36
37impl const From<Duration> for RoundDuration {
38  fn from(value: Duration) -> Self {
39    let mins = value.as_secs() / 60;
40    u16::try_from(mins)
41      .map(Self::new)
42      .unwrap_or(Self::MAX)
43  }
44}
45
46impl const From<RoundDuration> for Duration {
47  fn from(value: RoundDuration) -> Self {
48    Duration::from_mins(u64::from(value.0))
49  }
50}