2025-02-23 18:29:12 -05:00

65 lines
1.6 KiB
Rust

use std::{
cmp::{Ord, Ordering, PartialOrd},
fmt::{self, Display},
};
use serde::{Deserialize, Serialize};
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[repr(transparent)]
#[cfg_attr(feature = "server-ssr", derive(sqlx::Type))]
#[cfg_attr(feature = "server-ssr", sqlx(transparent))]
pub struct Version(u16);
impl Version {
pub const fn new(maj: u8, min: u8) -> Self {
Self(((maj as u16) << 8u16) | (min as u16))
}
pub const fn major(&self) -> u8 {
(self.0 >> 8) as u8
}
pub const fn minor(&self) -> u8 {
(self.0 & 0xFF) as u8
}
}
impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Version {
fn cmp(&self, other: &Self) -> Ordering {
if self.major() == other.major() {
self.minor().cmp(&other.minor())
} else {
self.major().cmp(&other.major())
}
}
}
impl fmt::Debug for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Version({}.{})", self.major(), self.minor())
}
}
impl Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major(), self.minor())
}
}
/*#[cfg(feature = "server-ssr")]
impl<'r> sqlx::Decode<'r, sqlx::Sqlite> for Version {
fn decode(
value: sqlx::Sqlite::ValueRef<'r>,
) -> Result<Self, Box<dyn Error + 'static + Send + Sync>> {
let value = <u16 as sqlx::Decode<sqlx::Sqlite>>::decode(value)?;
Ok(Self(value))
}
}*/