cleverdog/protocol/
version.rs

1use core::{num::ParseIntError};
2use core::fmt::{self, Display, Formatter};
3
4#[derive(Debug, Clone, Copy)]
5pub struct Version([u16; 4]);
6
7impl Version {
8    #[inline]
9    pub fn new(buf: [u16; 4]) -> Self {
10        Self(buf)
11    }
12
13    pub fn from_str(v: &str) -> Result<Self, ParseIntError> {
14        let mut buf = [0u16; 4];
15        let mut idx = 0;
16
17        for b in v.split('.') {
18            if idx == 4 {
19                break;
20            }
21
22            buf[idx] = u16::from_str_radix(b, 10)?;
23            idx += 1;
24        }
25
26        Ok(Self::new(buf))
27    }
28}
29
30impl Display for Version {
31    fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
32        let Version([major, minor, patch, release]) = self;
33        write!(fmt, "{}.{}.{}.{}", major, minor, patch, release)
34    }
35}