bioscopelib/enums/
microscope_command.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//!
//! Commands sent to the microscope
//
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 {
    /// Starts the stepper motor responsible for the X axis (needs additional parameters - speed, steps)
    LaunchX,
    /// Starts the stepper motor responsible for the Y axis (needs additional parameters - speed, steps)
    LaunchY,
    /// Starts the stepper motor responsible for the Z axis (needs additional parameters - speed, steps)
    LaunchZ,
    /// Stops the stepper motor responsible for the X axis
    StopX,
    /// Stops the stepper motor responsible for the Y axis
    StopY,
    /// Stops the stepper motor responsible for the Z axis
    StopZ,
    /// Resets the coordinates of the stepper motor responsible for the X axis to zero
    ResetX,
    /// Resets the coordinates of the stepper motor responsible for the Y axis to zero\
    ResetY,
    /// Resets the coordinates of the stepper motor responsible for the Z axis to zero
    ResetZ,
    /// Resets the coordinates of all stepper motors to zero (in develop...)
    ResetAll,
    /// Gets the coordinates of the stepper motor responsible for the X axis and its state
    GetStatusX,
    /// Gets the coordinates of the stepper motor responsible for the Y axis and its state
    GetStatusY,
    /// Gets the coordinates of the stepper motor responsible for the Z axis and its state
    GetStatusZ,
    /// Gets a photo from a microscope
    TakePicture,
}

#[cfg(feature = "diesel")]
impl MicroscopeCommand {
    /// Gets the string value corresponding to the enum value
    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()
    }

    /// Gets the string value corresponding to the enum value, but without creating an object
    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()
    }

    /// Get enum value
    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()),
        }
    }
}