Add Printer trait + PrinterHandle enum domain model with polymorphism example

This commit is contained in:
2026-08-28 17:03:21 +00:00
parent 2807b2b067
commit 03bd07fbd7
8 changed files with 302 additions and 1 deletions
+54
View File
@@ -0,0 +1,54 @@
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 {
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(())
}
}
impl Printer for BambuPrinter {
fn base(&self) -> &PrinterBase {
&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.
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.
Ok(())
}
}