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