journallib/enums/
absent_mark.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use serde::Deserialize;
use serde::Serialize;
use {
    diesel::backend::Backend,
    diesel::deserialize::FromSql,
    diesel::pg::Pg,
    diesel::serialize::{IsNull, Output, ToSql},
    diesel::sql_types::Varchar,
    diesel::{deserialize, not_none, serialize},
    std::io::Write,
    diesel::{AsExpression, FromSqlRow},
};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, AsExpression, FromSqlRow)]
#[sql_type = "Varchar"]
pub enum AbsentMark {
    A,
    S,
    L,
    E,
    M,
    F,
    B,
    H,
}

impl ToSql<Varchar, Pg> for AbsentMark {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        match *self {
            AbsentMark::A => out.write_all(b"A")?,
            AbsentMark::S => out.write_all(b"S")?,
            AbsentMark::L => out.write_all(b"L")?,
            AbsentMark::E => out.write_all(b"E")?,
            AbsentMark::M => out.write_all(b"M")?,
            AbsentMark::F => out.write_all(b"F")?,
            AbsentMark::B => out.write_all(b"B")?,
            AbsentMark::H => out.write_all(b"H")?,
        }
        Ok(IsNull::No)
    }
}

impl FromSql<Varchar, Pg> for AbsentMark {
    fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
        match not_none!(bytes) {
            b"A" => Ok(AbsentMark::A),
            b"S" => Ok(AbsentMark::S),
            b"L" => Ok(AbsentMark::L),
            b"E" => Ok(AbsentMark::E),
            b"M" => Ok(AbsentMark::M),
            b"F" => Ok(AbsentMark::F),
            b"B" => Ok(AbsentMark::B),
            b"H" => Ok(AbsentMark::H),

            _ => Err("Unrecognized enum variant".into()),
        }
    }
}