Files
Pictor-public/crates/worker/src/lib.rs
T

94 lines
3.3 KiB
Rust

//! `pictor-worker`: the only Pictor process that parses outside files.
//!
//! The app (the tray process) starts it, sends one JSON request per line on stdin and reads one JSON response per
//! line on stdout (the protocol is in `docs/FORMATS.md`). [`client::Worker`] does that from the app's side: it puts
//! the worker in a Windows Job Object with a memory cap, gives every request a time cap, and kills and restarts the
//! worker when it hangs or crashes — so a hostile PDF can take down a worker, never the tray.
pub mod client;
pub mod ops;
use serde::Serialize;
/// The protocol version in the worker's hello line; the client refuses another.
pub const PROTOCOL: u32 = 1;
/// A typed error the UI can act on (`kind`) with a plain sentence for the user (`message`).
#[derive(Debug, Clone, Serialize, serde::Deserialize, PartialEq)]
pub struct OpError {
pub kind: String,
pub message: String,
}
impl OpError {
pub fn new(kind: &str, message: impl Into<String>) -> OpError {
OpError { kind: kind.into(), message: message.into() }
}
}
impl std::fmt::Display for OpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl From<pictor_pdf::PdfError> for OpError {
fn from(e: pictor_pdf::PdfError) -> Self {
use pictor_pdf::PdfError as E;
let kind = match &e {
E::EngineMissing(_) => "engine_missing",
E::NeedsPassword => "needs_password",
E::WrongPassword => "wrong_password",
E::Damaged(_) => "damaged",
E::Unsupported(_) => "unsupported",
E::NoSuchPage(_) | E::Invalid(_) => "invalid",
E::Write(_) => "io",
E::TooLarge(_) => "too_large",
};
OpError::new(kind, e.to_string())
}
}
impl From<pictor_raster::RasterError> for OpError {
fn from(e: pictor_raster::RasterError) -> Self {
let text = format!("{e:?}");
let kind = match text.split([' ', '{', '(']).next().unwrap_or("") {
"HeifCodecMissing" | "AvifCodecMissing" => "codec_missing",
"InputTooLarge" | "TooManyPixels" | "TooMuchMemory" => "too_large",
"UnknownFormat" | "UnsupportedFeature" | "CannotWrite" => "unsupported",
"CannotReachTarget" => "cannot_reach_target",
"Empty" | "InvalidCrop" | "InvalidSize" | "InvalidTarget" => "invalid",
_ => "damaged",
};
OpError::new(kind, e.to_string())
}
}
impl From<pictor_meta::MetaError> for OpError {
fn from(e: pictor_meta::MetaError) -> Self {
use pictor_meta::MetaError as E;
let kind = match &e {
E::Encrypted => "needs_password",
E::Unsupported(_) => "unsupported",
E::TooLarge { .. } | E::TooBig(_) => "too_large",
E::Write(_) => "io",
_ => "damaged",
};
OpError::new(kind, e.to_string())
}
}
impl From<pictor_qr::QrError> for OpError {
fn from(e: pictor_qr::QrError) -> Self {
OpError::new("invalid", e.to_string())
}
}
impl From<pictor_save::SaveError> for OpError {
fn from(e: pictor_save::SaveError) -> Self {
match e {
pictor_save::SaveError::Exists(_) => OpError::new("exists", e.to_string()),
_ => OpError::new("io", e.to_string()),
}
}
}