Wrap ints in tiny types, implement corresponding functions for nice level
This commit is contained in:
124
src/types.rs
Normal file
124
src/types.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Priority(u32);
|
||||
|
||||
impl Priority {
|
||||
pub const MIN: u32 = 0;
|
||||
pub const MAX: u32 = 99;
|
||||
|
||||
/// Creates a new Priority, ensuring it is within valid bounds (0-99).
|
||||
pub fn new(value: u32) -> Option<Self> {
|
||||
if value <= Self::MAX {
|
||||
Some(Priority(value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the underlying priority value.
|
||||
pub fn value(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Priority> for u32 {
|
||||
fn from(priority: Priority) -> Self {
|
||||
priority.0
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u32> for Priority {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
||||
Priority::new(value).ok_or("Priority must be between 0 and 99")
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for Priority {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||
if value < 0 {
|
||||
return Err("Priority must be between 0 and 99");
|
||||
}
|
||||
Priority::new(value as u32).ok_or("Priority must be between 0 and 99")
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Priority {
|
||||
fn default() -> Self {
|
||||
Priority(99)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<u32> for Priority {
|
||||
fn as_ref(&self) -> &u32 {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Priority {
|
||||
type Target = u32;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct NiceLevel(i32);
|
||||
|
||||
impl NiceLevel {
|
||||
pub const MIN: i32 = -20;
|
||||
pub const MAX: i32 = 19;
|
||||
|
||||
/// Creates a new NiceLevel, ensuring it is within valid bounds (-20 to 19).
|
||||
pub fn new(value: i32) -> Option<Self> {
|
||||
if (Self::MIN..=Self::MAX).contains(&value) {
|
||||
Some(NiceLevel(value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the underlying nice level value.
|
||||
pub fn value(&self) -> i32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NiceLevel> for i32 {
|
||||
fn from(nice_level: NiceLevel) -> Self {
|
||||
nice_level.0
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for NiceLevel {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||
NiceLevel::new(value).ok_or("NiceLevel must be between -20 and 19")
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NiceLevel {
|
||||
fn default() -> Self {
|
||||
NiceLevel(Self::MIN)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<i32> for NiceLevel {
|
||||
fn as_ref(&self) -> &i32 {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for NiceLevel {
|
||||
type Target = i32;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user