timesheetlib/enums/
payment_type.rsuse std::fmt;
use std::fmt::Formatter;
use std::str;
use serde::Deserialize;
use serde::Serialize;
#[cfg(feature = "diesel")]
use {
diesel::{deserialize, not_none, serialize},
diesel::{AsExpression, FromSqlRow},
diesel::backend::Backend,
diesel::deserialize::FromSql,
diesel::pg::Pg,
diesel::serialize::{IsNull, Output, ToSql},
diesel::types::Varchar,
std::io::Write,
};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[cfg_attr(
feature = "diesel",
derive(AsExpression, FromSqlRow),
sql_type = "Varchar"
)]
pub enum PaymentType {
Salary,
AnualLeave,
Bonus,
Support,
FinalPay,
Undefined,
}
impl fmt::Display for PaymentType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl Default for PaymentType {
fn default() -> Self {
PaymentType::Undefined
}
}
#[cfg(feature = "diesel")]
impl ToSql<Varchar, Pg> for PaymentType {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match *self {
PaymentType::Salary => out.write_all(b"Salary")?,
PaymentType::AnualLeave => out.write_all(b"AnualLeave")?,
PaymentType::Bonus => out.write_all(b"Bonus")?,
PaymentType::Support => out.write_all(b"Support")?,
PaymentType::FinalPay => out.write_all(b"FinalPay")?,
PaymentType::Undefined => out.write_all(b"Undefined")?,
}
Ok(IsNull::No)
}
}
#[cfg(feature = "diesel")]
impl FromSql<Varchar, Pg> for PaymentType {
fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
match not_none!(bytes) {
b"Salary" => Ok(PaymentType::Salary),
b"AnualLeave" => Ok(PaymentType::AnualLeave),
b"Bonus" => Ok(PaymentType::Bonus),
b"Support" => Ok(PaymentType::Support),
b"FinalPay" => Ok(PaymentType::FinalPay),
b"Undefined" => Ok(PaymentType::Undefined),
_ => Err("Unrecognized enum variant".into()),
}
}
}