use std::path::Path;
use crate::core::command::{Command, ProjectFile};
use crate::core::project::PlateInspection;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrintStartParams {
pub file: String,
pub plate: u32,
pub use_ams: bool,
pub ams_map: Vec<i32>,
pub bed_type: String,
pub timelapse: bool,
}
impl PrintStartParams {
pub fn is_3mf(&self) -> bool {
self.file.to_ascii_lowercase().ends_with(".3mf")
}
}
pub fn build_command(params: &PrintStartParams, inspection: Option<&PlateInspection>) -> Command {
if !params.is_3mf() {
return Command::GcodeFile(params.file.clone());
}
let name = Path::new(¶ms.file)
.file_name()
.and_then(|s| s.to_str())
.unwrap_or(¶ms.file)
.to_string();
let mut pf = ProjectFile::new(format!("ftp://{}", params.file), params.plate, name);
pf.bed_type = params.bed_type.clone();
pf.timelapse = params.timelapse;
if params.use_ams {
pf.use_ams = true;
pf.ams_mapping = params.ams_map.clone();
}
if let Some(insp) = inspection {
pf.md5 = insp.gcode_md5.clone();
}
Command::ProjectFile(pf)
}
#[cfg(test)]
mod tests {
use super::*;
fn params(file: &str) -> PrintStartParams {
PrintStartParams {
file: file.to_string(),
plate: 1,
use_ams: false,
ams_map: vec![],
bed_type: "auto".to_string(),
timelapse: false,
}
}
fn inspection(md5: &str) -> PlateInspection {
PlateInspection {
plate: 1,
gcode_md5: md5.to_string(),
sidecar_md5: None,
sidecar_matches: true,
bed_type: None,
filament_colors: vec![],
has_timelapse_blocks: false,
}
}
#[test]
fn three_mf_builds_a_project_file_with_ftp_url() {
let mut p = params("/cache/coin.gcode.3mf");
p.plate = 2;
p.use_ams = true;
p.ams_map = vec![0, 3];
p.timelapse = true;
match build_command(&p, None) {
Command::ProjectFile(pf) => {
assert_eq!(pf.url, "ftp:///cache/coin.gcode.3mf");
assert_eq!(pf.plate, 2);
assert_eq!(pf.subtask_name, "coin.gcode.3mf");
assert!(pf.use_ams);
assert_eq!(pf.ams_mapping, vec![0, 3]);
assert!(pf.timelapse);
assert!(pf.md5.is_empty(), "no inspection ⇒ no md5 check");
}
other => panic!("expected ProjectFile, got {other:?}"),
}
}
#[test]
fn an_inspection_stamps_the_plate_gcode_md5() {
match build_command(¶ms("/x.gcode.3mf"), Some(&inspection("abc123"))) {
Command::ProjectFile(pf) => assert_eq!(pf.md5, "abc123"),
other => panic!("expected ProjectFile, got {other:?}"),
}
}
#[test]
fn raw_gcode_builds_a_gcode_file_and_ignores_inspection() {
assert!(matches!(
build_command(¶ms("/test.gcode"), Some(&inspection("abc"))),
Command::GcodeFile(f) if f == "/test.gcode"
));
}
}