use std::{
fmt::{Debug, Display},
str::FromStr,
};
use serde::{Deserialize, Deserializer};
#[derive(Debug, Clone)]
pub struct PrimaryKey<T>(T);
impl<T> PrimaryKey<T> {
pub fn new(value: T) -> Self {
PrimaryKey(value)
}
pub fn get(&self) -> &T {
&self.0
}
}
impl<T: Default> Default for PrimaryKey<T> {
fn default() -> Self {
PrimaryKey(T::default())
}
}
impl<T: Debug + Display> Display for PrimaryKey<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T: Debug + FromStr> FromStr for PrimaryKey<T> {
type Err = T::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<T>() {
Ok(value) => Ok(PrimaryKey(value)),
Err(err) => Err(err),
}
}
}
impl<'de, T> Deserialize<'de> for PrimaryKey<T>
where
T: FromStr + Debug,
<T as FromStr>::Err: Debug + Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value =
T::from_str(&String::deserialize(deserializer)?).map_err(serde::de::Error::custom)?;
Ok(PrimaryKey(value))
}
}
#[derive(Debug, Clone)]
pub struct AutoIncrementPrimaryKey<T>(Option<T>);
impl<T> AutoIncrementPrimaryKey<T> {
pub fn new(value: Option<T>) -> Self {
AutoIncrementPrimaryKey(value)
}
pub fn get(&self) -> Option<&T> {
self.0.as_ref()
}
pub fn set(&mut self, value: T) {
self.0 = Some(value);
}
}
impl<T: Debug> Default for AutoIncrementPrimaryKey<T> {
fn default() -> Self {
AutoIncrementPrimaryKey(None)
}
}
impl<T: Debug + Display> Display for AutoIncrementPrimaryKey<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Some(value) => write!(f, "{}", value),
None => write!(f, "NULL"),
}
}
}
impl<T: Debug + FromStr> FromStr for AutoIncrementPrimaryKey<T> {
type Err = T::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<T>() {
Ok(value) => Ok(AutoIncrementPrimaryKey(Some(value))),
Err(_) => Ok(AutoIncrementPrimaryKey(None)),
}
}
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for AutoIncrementPrimaryKey<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value: Option<T> = Option::deserialize(deserializer)?;
Ok(AutoIncrementPrimaryKey(value))
}
}
impl<T: PartialEq> PartialEq for AutoIncrementPrimaryKey<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}