use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{PrintId, Timestamp};
use super::ids::SpoolId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
#[schemars(crate = "printobserver_types::schemars")]
pub enum PrintOutcome {
Completed,
Cancelled,
Failed,
Interrupted,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
#[schemars(crate = "printobserver_types::schemars")]
pub enum ChargeMethod {
SlicerEstimate,
GcodeExtrusion,
FileFraction,
UnobservedEstimate,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "key",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum ChargeKey {
Print {
print_id: PrintId,
tool: i64,
segment: u32,
},
Unobserved {
origin: String,
file_name: String,
generation: u32,
print_number: i64,
tool: i64,
},
}
impl ChargeKey {
#[must_use]
pub fn canonical(&self) -> String {
match self {
Self::Print {
print_id,
tool,
segment,
} => format!("print:{print_id}:{tool}:{segment}"),
Self::Unobserved {
origin,
file_name,
generation,
print_number,
tool,
} => format!("unobserved:{origin}:{generation}:{print_number}:{tool}:{file_name}"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PrintSegmentKey {
pub print_id: PrintId,
pub tool: i64,
pub segment: u32,
}
impl PrintSegmentKey {
#[must_use]
pub const fn charge_key(&self) -> ChargeKey {
ChargeKey::Print {
print_id: self.print_id,
tool: self.tool,
segment: self.segment,
}
}
}
impl ChargeKey {
#[must_use]
pub const fn tool(&self) -> i64 {
match self {
Self::Print { tool, .. } | Self::Unobserved { tool, .. } => *tool,
}
}
#[must_use]
pub const fn watched_print(&self) -> Option<PrintId> {
match self {
Self::Print { print_id, .. } => Some(*print_id),
Self::Unobserved { .. } => None,
}
}
#[must_use]
pub const fn print_segment(&self) -> Option<PrintSegmentKey> {
match self {
Self::Print {
print_id,
tool,
segment,
} => Some(PrintSegmentKey {
print_id: *print_id,
tool: *tool,
segment: *segment,
}),
Self::Unobserved { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct ChargedFilament {
pub spool_id: SpoolId,
pub grams: f64,
pub diameter_mm: f64,
pub density_g_cm3: f64,
pub remaining_before_g: f64,
pub remaining_after_g: f64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct FilePosition {
pub bytes: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size_bytes: Option<i64>,
pub observed_at: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PendingCharge {
pub charge_key: PrintSegmentKey,
pub length_mm: f64,
pub method: ChargeMethod,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub analysed_length_mm: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub position: Option<FilePosition>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
pub pended_at: Timestamp,
}
impl PendingCharge {
#[must_use]
pub const fn print_id(&self) -> PrintId {
self.charge_key.print_id
}
#[must_use]
pub const fn tool(&self) -> i64 {
self.charge_key.tool
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PendingTaken {
pub charge_key: ChargeKey,
pub print_id: PrintId,
}