Diesel Enum
まれに書くときに書き方忘れる
ToSqlとFromSqlの実装忘れたらコンパイルとおらない
code: enum.rs
Debug,
Eq,
PartialEq,
Clone,
FromSqlRow,
AsExpression,
EnumString,
Display,
)]
pub enum EnumAB {
A,
B,
}
impl ToSql<EnumTypeInSchema, Mysql> for EnumAB {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
out.write_all(self.to_string().as_bytes())?;
Ok(IsNull::No)
}
}
impl FromSql<EnumTypeInSchema, Mysql> for EnumAB {
fn from_sql(bytes: MysqlValue<'_>) -> deserialize::Result<Self> {
match bytes.as_bytes() {
b"a" => Ok(EnumAB::A),
b"b" => Ok(EnumAB::B),
_ => Err("Unrecognized enum variant".into()),
}
}
}