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