cat_dev/mion/proto/cgis/
control.rs

1use crate::mion::cgis::MIONCGIApiError;
2use std::fmt::{Display, Formatter, Result as FmtResult};
3
4/// The type of operations you can do on the `control.cgi` page.
5#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
6pub enum ControlOperation {
7	PowerOn,
8	PowerOnV2,
9	GetInfo,
10	SetParam,
11}
12
13impl Display for ControlOperation {
14	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
15		write!(fmt, "{}", Into::<&str>::into(self))
16	}
17}
18
19impl From<&ControlOperation> for &str {
20	fn from(value: &ControlOperation) -> Self {
21		match *value {
22			ControlOperation::PowerOn => "power_on",
23			ControlOperation::PowerOnV2 => "power_on_v2",
24			ControlOperation::GetInfo => "get_info",
25			ControlOperation::SetParam => "set_param",
26		}
27	}
28}
29impl From<ControlOperation> for &str {
30	fn from(value: ControlOperation) -> Self {
31		Self::from(&value)
32	}
33}
34impl TryFrom<&str> for ControlOperation {
35	// This type is an API Error, because we don't ever deserialize it from the
36	// network.
37	type Error = MIONCGIApiError;
38
39	fn try_from(value: &str) -> Result<Self, Self::Error> {
40		match value {
41			"power_on" => Ok(Self::PowerOn),
42			"power_on_v2" => Ok(Self::PowerOnV2),
43			"get_info" => Ok(Self::GetInfo),
44			"set_param" => Ok(Self::SetParam),
45			val => Err(MIONCGIApiError::UnknownControlOperation(val.to_owned())),
46		}
47	}
48}
49
50/// The type of parameters you can set on the control page.
51#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
52pub enum SetParameter {
53	/// Set the ATAPI Port to use.
54	AtapiPort(u16),
55}
56
57impl SetParameter {
58	/// Get the value regardless of what it is as a string.
59	#[must_use]
60	pub fn get_value_as_string(&self) -> String {
61		match self {
62			Self::AtapiPort(ref port) => format!("{port}"),
63		}
64	}
65}
66
67impl Display for SetParameter {
68	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
69		write!(fmt, "{}", Into::<&str>::into(self))
70	}
71}
72
73impl From<&SetParameter> for &str {
74	fn from(value: &SetParameter) -> Self {
75		match *value {
76			SetParameter::AtapiPort(_) => "atapi_port",
77		}
78	}
79}
80impl From<SetParameter> for &str {
81	fn from(value: SetParameter) -> Self {
82		Self::from(&value)
83	}
84}
85
86#[cfg(test)]
87mod unit_tests {
88	use super::*;
89
90	#[test]
91	pub fn round_trip_control_operation() {
92		for operation in vec![
93			ControlOperation::PowerOn,
94			ControlOperation::PowerOnV2,
95			ControlOperation::GetInfo,
96			ControlOperation::SetParam,
97		] {
98			assert_eq!(
99				ControlOperation::try_from(Into::<&str>::into(&operation)),
100				Ok(operation),
101				"Round-trip conversion of: [{operation}] was not successful!",
102			);
103		}
104	}
105}