oxedyne/fe2o3/fe2o3_jdat/src/version.rs
2.6 KiB, 11 runs
created by r1870400018:493, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::prelude::*; |
| 2 | |
| 3 | use oxedyne_fe2o3_core::{ |
| 4 | prelude::*, |
| 5 | byte::{ |
| 6 | FromBytes, |
| 7 | ToBytes, |
| 8 | }, |
| 9 | mem::Extract, |
| 10 | }; |
| 11 | |
| 12 | use std::{ |
| 13 | fmt, |
| 14 | str, |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | #[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)] |
| 19 | pub struct SemVer { |
| 20 | pub major: u8, |
| 21 | pub minor: u8, |
| 22 | pub patch: u8, |
| 23 | } |
| 24 | |
| 25 | impl fmt::Debug for SemVer { |
| 26 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | write!(f, "{}.{}.{}", self.major, self.minor, self.patch) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl fmt::Display for SemVer { |
| 32 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 33 | write!(f, "{:?}", self) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl str::FromStr for SemVer { |
| 38 | type Err = Error<ErrTag>; |
| 39 | |
| 40 | fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 41 | let parts: Vec<&str> = s.split('.').collect(); |
| 42 | if parts.len() != 3 { |
| 43 | return Err(err!( |
| 44 | "SemVer expects 3 numbers, found {}.", parts.len(); |
| 45 | Input, Invalid)); |
| 46 | } |
| 47 | |
| 48 | let major = res!(parts[0].parse::<u8>()); |
| 49 | let minor = res!(parts[1].parse::<u8>()); |
| 50 | let patch = res!(parts[2].parse::<u8>()); |
| 51 | |
| 52 | Ok(Self { major, minor, patch }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl ToBytes for SemVer { |
| 57 | fn to_bytes(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> { |
| 58 | buf.push(self.major); |
| 59 | buf.push(self.minor); |
| 60 | buf.push(self.patch); |
| 61 | Ok(buf) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl FromBytes for SemVer { |
| 66 | fn from_bytes(buf: &[u8]) -> Outcome<(Self, usize)> { |
| 67 | if buf.len() < Self::BYTE_LEN { |
| 68 | return Err(err!( |
| 69 | "Not enough bytes to decode, require at least {}, found only {}.", |
| 70 | Self::BYTE_LEN, buf.len(); |
| 71 | Bytes, Input, Decode, Missing)); |
| 72 | } |
| 73 | Ok((Self { |
| 74 | major: buf[0], |
| 75 | minor: buf[1], |
| 76 | patch: buf[2], |
| 77 | }, |
| 78 | 3)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl ToDat for SemVer { |
| 83 | fn to_dat(&self) -> Outcome<Dat> { |
| 84 | Ok(tup3dat![ |
| 85 | dat!(self.major), |
| 86 | dat!(self.minor), |
| 87 | dat!(self.patch), |
| 88 | ]) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl FromDat for SemVer { |
| 93 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 94 | let mut result = Self::default(); |
| 95 | let mut v = try_extract_tup3dat!(dat); |
| 96 | result.major = try_extract_dat!(v[0].extract(), U8); |
| 97 | result.minor = try_extract_dat!(v[1].extract(), U8); |
| 98 | result.patch = try_extract_dat!(v[2].extract(), U8); |
| 99 | Ok(result) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl SemVer { |
| 104 | |
| 105 | pub const BYTE_LEN: usize = 3; |
| 106 | |
| 107 | pub const fn new(major: u8, minor: u8, patch: u8) -> Self { |
| 108 | Self { |
| 109 | major, |
| 110 | minor, |
| 111 | patch, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } |