journallib/enums/
lesson_type.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
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 LessonType {
    Test,
    FinalTest,
    CommonLesson,
    TestOutsideTheSystem,
    VideoConference,
}

impl ToSql<Varchar, Pg> for LessonType {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        match *self {
            LessonType::Test => out.write_all(b"Test")?,
            LessonType::FinalTest => out.write_all(b"FinalTest")?,
            LessonType::CommonLesson => out.write_all(b"CommonLesson")?,
            LessonType::TestOutsideTheSystem => out.write_all(b"TestOutsideTheSystem")?,
            LessonType::VideoConference => out.write_all(b"VideoConference")?,
        }
        Ok(IsNull::No)
    }
}

impl FromSql<Varchar, Pg> for LessonType {
    fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
        match not_none!(bytes) {
            b"Test" => Ok(LessonType::Test),
            b"FinalTest" => Ok(LessonType::FinalTest),
            b"CommonLesson" => Ok(LessonType::CommonLesson),
            b"TestOutsideTheSystem" => Ok(LessonType::TestOutsideTheSystem),
            b"VideoConference" => Ok(LessonType::VideoConference),

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