interestinglib/enums/
fact_type.rs
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 FactType {
Any,
Quote,
Riddle,
InterestingFact,
}
#[cfg(feature = "diesel")]
impl ToSql<Varchar, Pg> for FactType {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match *self {
FactType::Any => out.write_all(b"Any")?,
FactType::Quote => out.write_all(b"Quote")?,
FactType::Riddle => out.write_all(b"Riddle")?,
FactType::InterestingFact => out.write_all(b"InterestingFact")?,
}
Ok(IsNull::No)
}
}
#[cfg(feature = "diesel")]
impl FromSql<Varchar, Pg> for FactType {
fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
match not_none!(bytes) {
b"Any" => Ok(FactType::Any),
b"Quote" => Ok(FactType::Quote),
b"Riddle" => Ok(FactType::Riddle),
b"InterestingFact" => Ok(FactType::InterestingFact),
_ => Err("Unrecognized enum variant".into()),
}
}
}