use core::future::Future;
use core::pin::Pin;
use printobserver_types::contract::Sample;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{EventBody, EventKind, EventPayload, EventSource, RawBytes, Timestamp};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct ProviderPrint {
pub id: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde")]
#[schemars(crate = "printobserver_types::schemars")]
pub struct NormalizedAlert {
pub source: EventSource,
pub received_at: Timestamp,
#[serde(flatten)]
pub body: EventBody,
pub raw: RawBytes,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub image_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub print: Option<ProviderPrint>,
}
impl NormalizedAlert {
#[must_use]
pub const fn kind(&self) -> &EventKind {
&self.body.kind
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct MalformedExternalEventPayload {
pub detail: String,
}
impl EventPayload for MalformedExternalEventPayload {
const KIND: &'static str = "malformed_external_event";
}
impl Sample for MalformedExternalEventPayload {
fn sample_full() -> Self {
Self {
detail: "the body is not JSON: expected value at line 1 column 1".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct FetchedImage {
pub bytes: RawBytes,
pub content_type: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VisionError {
Malformed {
detail: String,
raw: RawBytes,
},
TimedOut,
TooLarge {
limit: i64,
},
UnacceptableContentType {
content_type: String,
},
Unreachable {
detail: String,
},
}
impl core::fmt::Display for VisionError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Malformed { detail, raw } => {
write!(formatter, "the body could not be read ({detail}); {raw}")
}
Self::TimedOut => formatter.write_str("the source took too long"),
Self::TooLarge { limit } => {
write!(
formatter,
"what arrived is larger than the {limit} byte limit"
)
}
Self::UnacceptableContentType { content_type } => {
write!(
formatter,
"{content_type} is not a content type this port accepts"
)
}
Self::Unreachable { detail } => {
write!(formatter, "the source is unreachable: {detail}")
}
}
}
}
impl core::error::Error for VisionError {}
pub trait VisionPort: Send + Sync {
fn normalize(
&self,
body: RawBytes,
content_type: Option<String>,
) -> BoxFuture<'_, Result<NormalizedAlert, VisionError>>;
fn fetch_image(&self, source_url: String) -> BoxFuture<'_, Result<FetchedImage, VisionError>>;
}