use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{FileName, Timestamp};
use super::ids::SpoolId;
use super::spool::{UnverifiedMark, UnverifiedMarks};
#[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 PreflightVerdict {
Passed,
Unchecked,
Insufficient,
UnverifiedSpool,
NoAnalysis,
Overridden,
Warned,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PreflightTool {
pub tool: i64,
pub result: PreflightToolResult,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PreflightSpool {
pub id: SpoolId,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct PreflightNeed {
pub required_g: f64,
pub buffer_g: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct ShortSpool {
pub spool: PreflightSpool,
pub remaining_g: f64,
pub need: PreflightNeed,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub marks: Vec<UnverifiedMark>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct DoubtedSpool {
pub spool: PreflightSpool,
pub remaining_g: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub need: Option<PreflightNeed>,
pub marks: UnverifiedMarks,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct UnanalysedSpool {
pub spool: PreflightSpool,
pub remaining_g: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "verdict",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum PreflightObjection {
Insufficient(ShortSpool),
UnverifiedSpool(DoubtedSpool),
NoAnalysis(UnanalysedSpool),
}
impl PreflightObjection {
#[must_use]
pub const fn verdict(&self) -> PreflightVerdict {
match self {
Self::Insufficient(_) => PreflightVerdict::Insufficient,
Self::UnverifiedSpool(_) => PreflightVerdict::UnverifiedSpool,
Self::NoAnalysis(_) => PreflightVerdict::NoAnalysis,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "verdict",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum PreflightToolResult {
Passed {
spool: PreflightSpool,
remaining_g: f64,
need: PreflightNeed,
},
Unchecked,
Insufficient(ShortSpool),
UnverifiedSpool(DoubtedSpool),
NoAnalysis(UnanalysedSpool),
Overridden {
objection: PreflightObjection,
},
Warned {
objection: PreflightObjection,
},
}
impl PreflightToolResult {
#[must_use]
pub const fn verdict(&self) -> PreflightVerdict {
match self {
Self::Passed { .. } => PreflightVerdict::Passed,
Self::Unchecked => PreflightVerdict::Unchecked,
Self::Insufficient(_) => PreflightVerdict::Insufficient,
Self::UnverifiedSpool(_) => PreflightVerdict::UnverifiedSpool,
Self::NoAnalysis(_) => PreflightVerdict::NoAnalysis,
Self::Overridden { .. } => PreflightVerdict::Overridden,
Self::Warned { .. } => PreflightVerdict::Warned,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct Preflight {
pub file_name: String,
pub tools: Vec<PreflightTool>,
pub verdict: PreflightVerdict,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub material: Option<MaterialResult>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sheet: Option<SheetResult>,
pub checked_at: Timestamp,
}
fn named_text(text: &str, what: &str) -> Result<String, String> {
let trimmed = text.trim();
if trimmed.is_empty() {
return Err(format!("{what} is named by something other than space"));
}
Ok(trimmed.to_owned())
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(
crate = "printobserver_types::serde",
try_from = "String",
into = "String"
)]
pub struct MaterialName(String);
impl JsonSchema for MaterialName {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("MaterialName")
}
fn json_schema(
_generator: &mut printobserver_types::schemars::SchemaGenerator,
) -> printobserver_types::schemars::Schema {
printobserver_types::schemars::json_schema!({
"type": "string",
"title": "MaterialName",
"description": "A filament material as a spool's registration or a slicer's settings name it, such as `PLA`: never blank, and trimmed of surrounding space as it is read.",
"minLength": 1,
"pattern": "\\S"
})
}
}
impl MaterialName {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn same(&self, other: &Self) -> bool {
self.0.to_lowercase() == other.0.to_lowercase()
}
}
impl TryFrom<String> for MaterialName {
type Error = String;
fn try_from(text: String) -> Result<Self, Self::Error> {
named_text(&text, "a material").map(Self)
}
}
impl From<MaterialName> for String {
fn from(name: MaterialName) -> Self {
name.0
}
}
impl core::fmt::Display for MaterialName {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(
crate = "printobserver_types::serde",
try_from = "String",
into = "String"
)]
pub struct SheetId(String);
impl JsonSchema for SheetId {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("SheetId")
}
fn json_schema(
_generator: &mut printobserver_types::schemars::SchemaGenerator,
) -> printobserver_types::schemars::Schema {
printobserver_types::schemars::json_schema!({
"type": "string",
"title": "SheetId",
"description": "A build sheet's identifier as the sheet table names it, such as `smooth-pei`: never blank, and trimmed of surrounding space as it is read.",
"minLength": 1,
"pattern": "\\S"
})
}
}
impl SheetId {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn same(&self, other: &Self) -> bool {
self.0.to_lowercase() == other.0.to_lowercase()
}
}
impl TryFrom<String> for SheetId {
type Error = String;
fn try_from(text: String) -> Result<Self, Self::Error> {
named_text(&text, "a sheet").map(Self)
}
}
impl From<SheetId> for String {
fn from(sheet: SheetId) -> Self {
sheet.0
}
}
impl core::fmt::Display for SheetId {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct MaterialComparison {
pub tool: i64,
pub spool: PreflightSpool,
pub loaded: MaterialName,
pub sliced_for: MaterialName,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct UncomparedMaterial {
pub tool: i64,
pub spool: PreflightSpool,
pub loaded: MaterialName,
pub file_name: FileName,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "cause",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum MaterialObjection {
Mismatch(MaterialComparison),
Undeclared(UncomparedMaterial),
Unread {
tool: i64,
spool: PreflightSpool,
loaded: MaterialName,
file_name: FileName,
detail: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "verdict",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum MaterialResult {
Passed(MaterialComparison),
Unchecked,
Refused {
objection: MaterialObjection,
},
Overridden {
objection: MaterialObjection,
},
Warned {
objection: MaterialObjection,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct SheetComparison {
pub sheet: SheetId,
pub material: MaterialName,
pub allowed: Vec<SheetId>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "cause",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum SheetObjection {
NotAllowed(SheetComparison),
UnlistedMaterial {
sheet: SheetId,
material: MaterialName,
},
UnknownSheet {
sheet: SheetId,
material: MaterialName,
known: Vec<SheetId>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
crate = "printobserver_types::serde",
tag = "verdict",
rename_all = "snake_case",
deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum SheetResult {
Passed(SheetComparison),
Unchecked,
Undeclared,
Refused {
objection: SheetObjection,
},
Overridden {
objection: SheetObjection,
},
Warned {
objection: SheetObjection,
},
}