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()),
}
}
}