printobserver_printer_api/
adjustable.rs1use core::fmt;
13use core::str::FromStr;
14use std::borrow::Cow;
15
16use printobserver_types::contract::Sample;
17use printobserver_types::schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
18use printobserver_types::serde::{
19 Deserialize, Deserializer, Serialize, Serializer, de::Error as _,
20};
21
22const TOOL_TARGET: &str = "tool_target";
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
32pub enum Adjustable {
33 Feedrate,
35 Flowrate,
37 ToolTarget {
39 tool: i64,
41 },
42 BedTarget,
44 Fan,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct AdjustableError {
51 detail: String,
53}
54
55impl AdjustableError {
56 #[must_use]
58 pub fn detail(&self) -> &str {
59 &self.detail
60 }
61}
62
63impl fmt::Display for AdjustableError {
64 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(formatter, "not an adjustable: {}", self.detail)
66 }
67}
68
69impl core::error::Error for AdjustableError {}
70
71impl fmt::Display for Adjustable {
72 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 Self::Feedrate => formatter.write_str("feedrate"),
75 Self::Flowrate => formatter.write_str("flowrate"),
76 Self::ToolTarget { tool } => write!(formatter, "{TOOL_TARGET}:{tool}"),
77 Self::BedTarget => formatter.write_str("bed_target"),
78 Self::Fan => formatter.write_str("fan"),
79 }
80 }
81}
82
83impl FromStr for Adjustable {
84 type Err = AdjustableError;
85
86 fn from_str(text: &str) -> Result<Self, Self::Err> {
87 match text {
88 "feedrate" => return Ok(Self::Feedrate),
89 "flowrate" => return Ok(Self::Flowrate),
90 "bed_target" => return Ok(Self::BedTarget),
91 "fan" => return Ok(Self::Fan),
92 _ => {}
93 }
94 let Some(number) = text.strip_prefix(concat!("tool_target", ":")) else {
95 return Err(AdjustableError {
96 detail: format!("{text:?} names no adjustable"),
97 });
98 };
99 number
100 .parse()
101 .map(|tool| Self::ToolTarget { tool })
102 .map_err(|error| AdjustableError {
103 detail: format!("{text:?} carries no tool number: {error}"),
104 })
105 }
106}
107
108impl Serialize for Adjustable {
109 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
110 serializer.collect_str(self)
111 }
112}
113
114impl<'de> Deserialize<'de> for Adjustable {
115 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
116 let text = String::deserialize(deserializer)?;
117 text.parse().map_err(D::Error::custom)
118 }
119}
120
121impl JsonSchema for Adjustable {
122 fn schema_name() -> Cow<'static, str> {
123 Cow::Borrowed("Adjustable")
124 }
125
126 fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
127 json_schema!({
128 "type": "string",
129 "title": "Adjustable",
130 "description": "One thing an adjustment may change.",
131 "pattern": "^(feedrate|flowrate|bed_target|fan|tool_target:-?[0-9]+)$"
132 })
133 }
134}
135
136impl Sample for Adjustable {
137 fn sample_full() -> Self {
138 Self::ToolTarget { tool: 0 }
139 }
140}