use serde::Deserialize; #[derive(Debug)] pub struct PrinterState { pub name: String, pub bed_temp: f32, } #[derive(Debug, Deserialize)] pub struct Config { // This allows you to have a list of different printer types pub printers: Vec, } impl Config { pub fn load(toml: &str) -> Config { toml::from_str(toml).expect("Couldn't parse config.toml") } } #[derive(Debug, Deserialize)] #[serde(tag = "type", rename_all = "lowercase")] pub enum Printer { Prusa { name: String, host: String, api_key: String, }, Bambu { name: String, host: String, access_code: String, serial_number: String, }, } pub fn prusa_fetch(client: &reqwest::blocking::Client, printer: &Printer) { let Printer::Prusa { name, host, api_key, } = printer else { panic!("Expected a Prusa printer, but received a different variant!"); }; let url = format!("http://{}/api/v1/status", host); let mut req = client.get(&url); req = req.header("X-Api-Key", api_key); match req.send() { Err(e) => { eprintln!("Could not reach Prusa printer {} at {}: {}", name, host, e); return; } Ok(resp) => { if !resp.status().is_success() { eprintln!("HTTP {}: {}", resp.status(), url); if resp.status().as_u16() == 403 { eprintln!("Invalid PrusaLink key for {}.", name); } return; } match resp.text() { Err(e) => eprintln!("Failed to parse response for Prusa printer {}: {}", host, e), Ok(text) => println!("{}", text), } } } }