Simplify printer/ module for someone learning Rust

Drop reqwest, RPITIT+Send signature, and multi-variant error enum from the
teaching example so the only new idea per file is the trait override itself.
Real networking stays in adapters/, unaffected.
This commit is contained in:
2026-08-28 18:04:13 +00:00
parent 9b3815fd25
commit 5705f36c01
5 changed files with 79 additions and 129 deletions
+9 -20
View File
@@ -1,7 +1,3 @@
use std::time::Duration;
use tracing::info;
use super::{Printer, PrinterBase, PrinterError};
pub struct KlipperPrinter {
@@ -9,7 +5,7 @@ pub struct KlipperPrinter {
}
impl KlipperPrinter {
pub fn new(id: impl Into<String>, name: impl Into<String>, host: impl Into<String>) -> Self {
pub fn new(id: &str, name: &str, host: &str) -> Self {
Self {
base: PrinterBase { id: id.into(), name: name.into(), host: host.into() },
}
@@ -21,22 +17,15 @@ impl Printer for KlipperPrinter {
&self.base
}
/// OVERRIDE: Moonraker's real-time feed is a WebSocket JSON-RPC channel
/// (see adapters::moonraker::run for that), but it also exposes a plain
/// REST endpoint. `connect()` here is a lightweight reachability probe
/// against that REST endpoint before the daemon opens the WS stream —
/// a third, again-completely-different notion of "connect".
/// OVERRIDE: a third, again-different body — Moonraker's real feed is a
/// WebSocket (see `adapters::moonraker`). This also shows the `Err`
/// path: an empty `host` fails immediately, same as any other vendor
/// would if it couldn't reach its printer.
async fn connect(&mut self) -> Result<(), PrinterError> {
info!(printer = %self.base.id, host = %self.base.host, "probing Moonraker over HTTP");
reqwest::Client::new()
.get(format!("http://{}/printer/info", self.base.host))
.timeout(Duration::from_secs(5))
.send()
.await
.and_then(|resp| resp.error_for_status())
.map_err(|err| PrinterError::Connection(self.base.id.clone(), err.to_string()))?;
if self.base.host.is_empty() {
return Err(PrinterError(format!("{} has no host configured", self.base.name)));
}
println!("[{}] connecting over WebSocket (Moonraker)", self.base.name);
Ok(())
}
}