use serde::Deserialize;
use serde::Serialize;
use std::io::Write;
#[cfg(feature = "diesel")]
use {
diesel::backend::Backend,
diesel::deserialize::FromSql,
diesel::pg::Pg,
diesel::serialize::{IsNull, Output, ToSql},
diesel::sql_types::Varchar,
diesel::{deserialize, not_none, serialize},
};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "diesel", derive(AsExpression, FromSqlRow))]
#[cfg_attr(feature = "diesel", sql_type = "Varchar")]
pub enum MicroscopeCommand {
LaunchX,
LaunchY,
LaunchZ,
StopX,
StopY,
StopZ,
ResetX,
ResetY,
ResetZ,
ResetAll,
GetStatusX,
GetStatusY,
GetStatusZ,
TakePicture,
}
#[cfg(feature = "diesel")]
impl MicroscopeCommand {
pub fn to_string(&self) -> String {
let res = match *self {
MicroscopeCommand::LaunchX => "launchX",
MicroscopeCommand::LaunchY => "launchY",
MicroscopeCommand::LaunchZ => "launchZ",
MicroscopeCommand::StopX => "stopX",
MicroscopeCommand::StopY => "stopY",
MicroscopeCommand::StopZ => "stopZ",
MicroscopeCommand::ResetX => "resetX",
MicroscopeCommand::ResetY => "resetY",
MicroscopeCommand::ResetZ => "resetZ",
MicroscopeCommand::ResetAll => "resetAll",
MicroscopeCommand::GetStatusX => "getStatusX",
MicroscopeCommand::GetStatusY => "getStatusY",
MicroscopeCommand::GetStatusZ => "getStatusZ",
MicroscopeCommand::TakePicture => "takePicture",
};
res.to_string()
}
pub fn get_string(command: MicroscopeCommand) -> String {
let res = match command {
MicroscopeCommand::LaunchX => "launchX",
MicroscopeCommand::LaunchY => "launchY",
MicroscopeCommand::LaunchZ => "launchZ",
MicroscopeCommand::StopX => "stopX",
MicroscopeCommand::StopY => "stopY",
MicroscopeCommand::StopZ => "stopZ",
MicroscopeCommand::ResetX => "resetX",
MicroscopeCommand::ResetY => "resetY",
MicroscopeCommand::ResetZ => "resetZ",
MicroscopeCommand::ResetAll => "resetAll",
MicroscopeCommand::GetStatusX => "getStatusX",
MicroscopeCommand::GetStatusY => "getStatusY",
MicroscopeCommand::GetStatusZ => "getStatusZ",
MicroscopeCommand::TakePicture => "takePicture",
};
res.to_string()
}
pub fn from_string(str: String) -> Option<MicroscopeCommand> {
let str = str.as_str();
return match str {
"launchX" => Option::from(MicroscopeCommand::LaunchX),
"launchY" => Option::from(MicroscopeCommand::LaunchY),
"launchZ" => Option::from(MicroscopeCommand::LaunchZ),
"stopX" => Option::from(MicroscopeCommand::StopX),
"stopY" => Option::from(MicroscopeCommand::StopY),
"stopZ" => Option::from(MicroscopeCommand::StopZ),
"resetX" => Option::from(MicroscopeCommand::ResetX),
"resetY" => Option::from(MicroscopeCommand::ResetY),
"resetZ" => Option::from(MicroscopeCommand::ResetZ),
"resetAll" => Option::from(MicroscopeCommand::ResetAll),
"getStatusX" => Option::from(MicroscopeCommand::GetStatusX),
"getStatusY" => Option::from(MicroscopeCommand::GetStatusY),
"getStatusZ" => Option::from(MicroscopeCommand::GetStatusZ),
"takePicture" => Option::from(MicroscopeCommand::TakePicture),
_ => None,
};
}
}
#[cfg(feature = "diesel")]
impl ToSql<Varchar, Pg> for MicroscopeCommand {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
match *self {
MicroscopeCommand::LaunchX => out.write_all(b"launchX")?,
MicroscopeCommand::LaunchY => out.write_all(b"launchY")?,
MicroscopeCommand::LaunchZ => out.write_all(b"launchZ")?,
MicroscopeCommand::StopX => out.write_all(b"stopX")?,
MicroscopeCommand::StopY => out.write_all(b"stopY")?,
MicroscopeCommand::StopZ => out.write_all(b"stopZ")?,
MicroscopeCommand::ResetX => out.write_all(b"resetX")?,
MicroscopeCommand::ResetY => out.write_all(b"resetY")?,
MicroscopeCommand::ResetZ => out.write_all(b"resetZ")?,
MicroscopeCommand::ResetAll => out.write_all(b"resetAll")?,
MicroscopeCommand::GetStatusX => out.write_all(b"getStatusX")?,
MicroscopeCommand::GetStatusY => out.write_all(b"getStatusY")?,
MicroscopeCommand::GetStatusZ => out.write_all(b"getStatusZ")?,
MicroscopeCommand::TakePicture => out.write_all(b"takePicture")?,
}
Ok(IsNull::No)
}
}
#[cfg(feature = "diesel")]
impl FromSql<Varchar, Pg> for MicroscopeCommand {
fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
match not_none!(bytes) {
b"launchX" => Ok(MicroscopeCommand::LaunchX),
b"launchY" => Ok(MicroscopeCommand::LaunchY),
b"launchZ" => Ok(MicroscopeCommand::LaunchZ),
b"stopX" => Ok(MicroscopeCommand::StopX),
b"stopY" => Ok(MicroscopeCommand::StopY),
b"stopZ" => Ok(MicroscopeCommand::StopZ),
b"resetX" => Ok(MicroscopeCommand::ResetX),
b"resetY" => Ok(MicroscopeCommand::ResetY),
b"resetZ" => Ok(MicroscopeCommand::ResetZ),
b"resetAll" => Ok(MicroscopeCommand::ResetAll),
b"getStatusX" => Ok(MicroscopeCommand::GetStatusX),
b"getStatusY" => Ok(MicroscopeCommand::GetStatusY),
b"getStatusZ" => Ok(MicroscopeCommand::GetStatusZ),
b"takePicture" => Ok(MicroscopeCommand::TakePicture),
_ => Err("Unrecognized enum variant".into()),
}
}
}