Line data Source code
1 : use diesel::pg::{Pg, PgValue};
2 : use diesel::{
3 : deserialize::FromSql, deserialize::FromSqlRow, expression::AsExpression, serialize::ToSql,
4 : sql_types::Int2,
5 : };
6 : use serde::{Deserialize, Serialize};
7 :
8 0 : #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, FromSqlRow, AsExpression)]
9 : #[diesel(sql_type = SplitStateSQLRepr)]
10 0 : #[derive(Deserialize, Serialize)]
11 : pub enum SplitState {
12 : Idle = 0,
13 : Splitting = 1,
14 : }
15 :
16 : impl Default for SplitState {
17 0 : fn default() -> Self {
18 0 : Self::Idle
19 0 : }
20 : }
21 :
22 : type SplitStateSQLRepr = Int2;
23 :
24 : impl ToSql<SplitStateSQLRepr, Pg> for SplitState {
25 0 : fn to_sql<'a>(
26 0 : &'a self,
27 0 : out: &'a mut diesel::serialize::Output<Pg>,
28 0 : ) -> diesel::serialize::Result {
29 0 : let raw_value: i16 = *self as i16;
30 0 : let mut new_out = out.reborrow();
31 0 : ToSql::<SplitStateSQLRepr, Pg>::to_sql(&raw_value, &mut new_out)
32 0 : }
33 : }
34 :
35 : impl FromSql<SplitStateSQLRepr, Pg> for SplitState {
36 0 : fn from_sql(pg_value: PgValue) -> diesel::deserialize::Result<Self> {
37 0 : match FromSql::<SplitStateSQLRepr, Pg>::from_sql(pg_value).map(|v| match v {
38 0 : 0 => Some(Self::Idle),
39 0 : 1 => Some(Self::Splitting),
40 0 : _ => None,
41 0 : })? {
42 0 : Some(v) => Ok(v),
43 0 : None => Err(format!("Invalid SplitState value, was: {:?}", pg_value.as_bytes()).into()),
44 : }
45 0 : }
46 : }
|