2021-06-16 12:31:03 -07:00
|
|
|
use std::fmt::Display;
|
2021-07-09 16:14:53 -07:00
|
|
|
use std::num::{NonZeroU16, NonZeroU64, ParseIntError};
|
2021-06-16 12:31:03 -07:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
/// Wrapper type for a port number.
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
|
|
|
pub struct Port(NonZeroU16);
|
|
|
|
|
2021-07-09 16:14:53 -07:00
|
|
|
impl Port {
|
|
|
|
pub const fn get(self) -> u16 {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Port {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(unsafe { NonZeroU16::new_unchecked(443) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:31:03 -07:00
|
|
|
impl FromStr for Port {
|
|
|
|
type Err = <NonZeroU16 as FromStr>::Err;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
NonZeroU16::from_str(s).map(Self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Port {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:18:43 -07:00
|
|
|
#[derive(Copy, Clone, Serialize, Deserialize, Default, Debug, Hash, Eq, PartialEq)]
|
2021-06-16 12:31:03 -07:00
|
|
|
pub struct Mebibytes(usize);
|
|
|
|
|
2021-07-09 16:14:53 -07:00
|
|
|
impl FromStr for Mebibytes {
|
|
|
|
type Err = ParseIntError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
s.parse::<usize>().map(Self)
|
2021-06-16 12:31:03 -07:00
|
|
|
}
|
2021-07-09 14:18:43 -07:00
|
|
|
}
|
2021-06-16 12:31:03 -07:00
|
|
|
|
2021-07-09 14:18:43 -07:00
|
|
|
pub struct Bytes(usize);
|
|
|
|
|
|
|
|
impl Bytes {
|
2021-06-16 12:31:03 -07:00
|
|
|
pub const fn get(&self) -> usize {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:18:43 -07:00
|
|
|
impl From<Mebibytes> for Bytes {
|
|
|
|
fn from(mib: Mebibytes) -> Self {
|
|
|
|
Self(mib.0 << 20)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:31:03 -07:00
|
|
|
#[derive(Copy, Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
|
2021-07-09 14:18:43 -07:00
|
|
|
pub struct KilobitsPerSecond(NonZeroU64);
|
|
|
|
|
2021-07-09 16:14:53 -07:00
|
|
|
impl FromStr for KilobitsPerSecond {
|
|
|
|
type Err = ParseIntError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
s.parse::<NonZeroU64>().map(Self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:18:43 -07:00
|
|
|
#[derive(Copy, Clone, Serialize, Debug, Hash, Eq, PartialEq)]
|
|
|
|
pub struct BytesPerSecond(NonZeroU64);
|
|
|
|
|
|
|
|
impl From<KilobitsPerSecond> for BytesPerSecond {
|
|
|
|
fn from(kbps: KilobitsPerSecond) -> Self {
|
|
|
|
Self(unsafe { NonZeroU64::new_unchecked(kbps.0.get() * 125) })
|
|
|
|
}
|
|
|
|
}
|