bufkit_data/site/
station_num.rs

1#[cfg(feature = "pylib")]
2use pyo3::prelude::*;
3use std::fmt::Display;
4
5/// New type wrapper for a station number.
6#[cfg_attr(feature = "pylib", pyclass(module = "bufkit_data"))]
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
8pub struct StationNumber {
9    num: u32,
10}
11
12impl From<u32> for StationNumber {
13    fn from(val: u32) -> Self {
14        StationNumber { num: val }
15    }
16}
17
18impl Into<u32> for StationNumber {
19    fn into(self) -> u32 {
20        self.num
21    }
22}
23
24impl Into<i64> for StationNumber {
25    fn into(self) -> i64 {
26        i64::from(self.num)
27    }
28}
29
30impl Display for StationNumber {
31    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
32        write!(formatter, "{}", self.num)
33    }
34}
35
36impl StationNumber {
37    /// Test to see if this is a valid station number.
38    pub fn is_valid(self) -> bool {
39        self.num > 0
40    }
41
42    /// Create a new one.
43    pub const fn new(num: u32) -> Self {
44        StationNumber { num }
45    }
46}
47
48#[cfg(feature = "pylib")]
49#[cfg_attr(feature = "pylib", pymethods)]
50impl StationNumber {
51
52    fn __repr__(&self) -> PyResult<String> {
53        Ok(format!("bufkit_data::StationNumber({})", self.num))
54    }
55
56    #[new]
57    fn py_new(num: u32) -> Self {
58        Self::new(num)
59    }
60
61    #[getter]
62    fn get_as_number(&self) -> u32 {
63        self.num
64    }
65}
66