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
+11 -28
View File
@@ -1,38 +1,24 @@
use std::path::Path;
use tracing::info;
use super::{Printer, PrinterBase, PrinterError};
/// Bambu-specific fields — this is the "extends GenericBambuPrinter" part of
/// your original design, expressed as extra struct fields instead of a
/// second inheritance layer.
pub struct BambuPrinter {
base: PrinterBase,
pub access_code: String,
}
impl BambuPrinter {
pub fn new(id: impl Into<String>, name: impl Into<String>, host: impl Into<String>, access_code: impl Into<String>) -> Self {
pub fn new(id: &str, name: &str, host: &str, access_code: &str) -> Self {
Self {
base: PrinterBase { id: id.into(), name: name.into(), host: host.into() },
access_code: access_code.into(),
}
}
/// A method that ONLY exists on `BambuPrinter` — not part of the
/// `Printer` trait at all. This is what "adding a printer-specific
/// method" looks like: just define it in this struct's own `impl`
/// block. `PrusaPrinter` and `KlipperPrinter` have no equivalent, and
/// calling this requires a `PrinterHandle::Bambu(..)` match first (see
/// the example), which is the price for not doing runtime downcasting.
///
/// Real implementation lives in `adapters::bambu::dispatch_file` (FTPS
/// on port 990); this is the domain-model-facing wrapper around it.
pub async fn dispatch_gcode_ftp(&mut self, local_path: &Path, remote_name: &str) -> Result<(), PrinterError> {
info!(printer = %self.base.id, remote_name = %remote_name, path = ?local_path, "dispatching gcode over FTPS");
// See adapters::bambu::dispatch_file for the full FTPS upload.
Ok(())
/// Only `BambuPrinter` has this — it's not on the `Printer` trait at
/// all, so `PrusaPrinter`/`KlipperPrinter` simply don't have it. This
/// is what "adding a printer-specific method" looks like: just define
/// it in this struct's own `impl` block.
pub fn dispatch_gcode(&self, file_name: &str) {
println!("[{}] uploading {file_name} over FTPS", self.base.name);
}
}
@@ -41,14 +27,11 @@ impl Printer for BambuPrinter {
&self.base
}
/// OVERRIDE: Bambu speaks MQTTS on port 8883, authenticated with the
/// printer's LAN access code. Completely different transport from the
/// other two vendors below.
/// OVERRIDE: Bambu's real connect logic opens an MQTTS session (see
/// `adapters::bambu::run_telemetry` for that). Here we just print what
/// it would do, so the focus stays on how dispatch works.
async fn connect(&mut self) -> Result<(), PrinterError> {
info!(printer = %self.base.id, host = %self.base.host, "connecting to Bambu printer over MQTTS:8883");
// Real implementation: open the MQTTS session and wait for the
// first `report` message — see adapters::bambu::run_telemetry,
// which this would delegate to once wired into the daemon.
println!("[{}] connecting over MQTT with access code {}", self.base.name, self.access_code);
Ok(())
}
}