use core::ops::ControlFlow;
use printobserver_types::Timestamp;
use printobserver_types::contract::Sample;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct ToolFilament {
pub tool: i64,
pub length_mm: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub volume_cm3: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct FilePrint {
pub at: Timestamp,
pub success: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct FileFacts {
pub file_name: String,
pub origin: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size_bytes: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub filament: Option<Vec<ToolFilament>>,
pub successes: i64,
pub failures: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_print: Option<FilePrint>,
}
impl Sample for ToolFilament {
fn sample_full() -> Self {
Self {
tool: 0,
length_mm: 1_000.0,
volume_cm3: Some(2.41),
}
}
fn sample_minimal() -> Self {
Self {
volume_cm3: None,
..Self::sample_full()
}
}
}
impl Sample for FilePrint {
fn sample_full() -> Self {
Self {
at: Timestamp::sample_full(),
success: true,
}
}
}
impl Sample for FileFacts {
fn sample_full() -> Self {
Self {
file_name: "benchy.gcode".to_owned(),
origin: "local".to_owned(),
size_bytes: Some(4_194_304),
filament: Some(vec![ToolFilament::sample_full()]),
successes: 3,
failures: 1,
last_print: Some(FilePrint::sample_full()),
}
}
fn sample_minimal() -> Self {
Self {
file_name: "benchy.gcode".to_owned(),
origin: "sdcard".to_owned(),
size_bytes: None,
filament: None,
successes: 0,
failures: 0,
last_print: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct Footprint {
pub min_x: f64,
pub min_y: f64,
pub max_x: f64,
pub max_y: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PrintedExtent {
pub tallest_mm: f64,
pub footprint: Footprint,
}
impl Sample for Footprint {
fn sample_full() -> Self {
Self {
min_x: 90.0,
min_y: 90.0,
max_x: 110.0,
max_y: 110.0,
}
}
fn sample_minimal() -> Self {
Self {
min_x: 0.0,
min_y: 0.0,
max_x: 0.0,
max_y: 0.0,
}
}
}
impl Sample for PrintedExtent {
fn sample_full() -> Self {
Self {
tallest_mm: 6.0,
footprint: Footprint::sample_full(),
}
}
fn sample_minimal() -> Self {
Self {
tallest_mm: 0.2,
footprint: Footprint::sample_minimal(),
}
}
}
pub trait GcodeSink: Send {
fn take(&mut self, piece: &[u8]) -> ControlFlow<()>;
}