assignmentlib/enums/
question_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use serde::Deserialize;
use serde::Serialize;
use std::str;
use strum_macros::EnumString;
#[cfg(feature = "diesel")]
use {
    diesel::backend::Backend,
    diesel::deserialize::FromSql,
    diesel::pg::Pg,
    diesel::serialize::{IsNull, Output, ToSql},
    diesel::types::Varchar,
    diesel::{deserialize, not_none, serialize},
    diesel::{AsExpression, FromSqlRow},
    std::io::Write,
};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, EnumString)]
#[cfg_attr(feature = "diesel", derive(AsExpression, FromSqlRow))]
#[cfg_attr(feature = "diesel", sql_type = "Varchar")]
pub enum QuestionType {
    Drawing,
    CheckMatch,
    GapMatch,
    GraphicGapMatch,
    Hotspot,
    Hottext,
    InlineChoice,
    Single,
    Multiple,
    Order,
    Match,
    InsertMissing,
    Number,
    Word,
    Text,
    TextEntry,
    File,
    Missing,
    Compilance,
    Undefined,
}

#[cfg(feature = "diesel")]
impl ToSql<Varchar, Pg> for QuestionType {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        match *self {
            QuestionType::Single => out.write_all(b"Single")?,
            QuestionType::Drawing => out.write_all(b"Drawing")?,
            QuestionType::CheckMatch => out.write_all(b"CheckMatch")?,
            QuestionType::GraphicGapMatch => out.write_all(b"GraphicGapMatch")?,
            QuestionType::GapMatch => out.write_all(b"CheckMatch")?,
            QuestionType::Hotspot => out.write_all(b"Hotspot")?,
            QuestionType::Hottext => out.write_all(b"Hottext")?,
            QuestionType::InlineChoice => out.write_all(b"InlineChoice")?,
            QuestionType::TextEntry => out.write_all(b"TextEntry")?,
            QuestionType::Multiple => out.write_all(b"Multiple")?,
            QuestionType::Order => out.write_all(b"Order")?,
            QuestionType::Match => out.write_all(b"Match")?,
            QuestionType::InsertMissing => out.write_all(b"InsertMissing")?,
            QuestionType::Number => out.write_all(b"Number")?,
            QuestionType::Word => out.write_all(b"Word")?,
            QuestionType::Text => out.write_all(b"Text")?,
            QuestionType::File => out.write_all(b"File")?,
            QuestionType::Missing => out.write_all(b"Missing")?,
            QuestionType::Compilance => out.write_all(b"Compilance")?,
            QuestionType::Undefined => out.write_all(b"Undefined")?,
        }
        Ok(IsNull::No)
    }
}

#[cfg(feature = "diesel")]
impl FromSql<Varchar, Pg> for QuestionType {
    fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
        match not_none!(bytes) {
            b"Single" => Ok(QuestionType::Single),
            b"Drawing" => Ok(QuestionType::Drawing),
            b"CheckMatch" => Ok(QuestionType::CheckMatch),
            b"GraphicGapMatch" => Ok(QuestionType::GraphicGapMatch),
            b"GapMatch" => Ok(QuestionType::GapMatch),
            b"Hotspot" => Ok(QuestionType::Hotspot),
            b"Hottext" => Ok(QuestionType::Hottext),
            b"InlineChoice" => Ok(QuestionType::InlineChoice),
            b"TextEntry" => Ok(QuestionType::TextEntry),
            b"Multiple" => Ok(QuestionType::Multiple),
            b"Order" => Ok(QuestionType::Order),
            b"Match" => Ok(QuestionType::Match),
            b"InsertMissing" => Ok(QuestionType::InsertMissing),
            b"Number" => Ok(QuestionType::Number),
            b"Word" => Ok(QuestionType::Word),
            b"Text" => Ok(QuestionType::Text),
            b"File" => Ok(QuestionType::File),
            b"Missing" => Ok(QuestionType::Missing),
            b"Compilance" => Ok(QuestionType::Compilance),
            b"Undefined" => Ok(QuestionType::Undefined),
            _ => Err("Unrecognized enum variant".into()),
        }
    }
}