pub mod file;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod cups;
#[cfg(windows)]
pub mod windows;
use crate::print::paper::Margins;
use crate::print::{ComposeOptions, PrintError, PrintOptions, Sheet};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PrinterId(pub String);
impl PrinterId {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for PrinterId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrinterInfo {
pub id: PrinterId,
pub description: Option<String>,
pub location: Option<String>,
pub is_default: bool,
pub accepting_jobs: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeviceCapabilities {
pub hardware_margins: Margins,
pub supports_duplex: bool,
pub supports_color: bool,
pub resolution_dpi: Option<f64>,
pub accepts_pdf: bool,
}
impl Default for DeviceCapabilities {
fn default() -> Self {
Self {
hardware_margins: Margins::uniform(crate::print::paper::DEFAULT_HARDWARE_MARGIN_PT),
supports_duplex: false,
supports_color: true,
resolution_dpi: None,
accepts_pdf: false,
}
}
}
pub enum SpoolPayload<'a> {
PassThroughPdf(&'a [u8]),
Sheets {
session: &'a crate::reader::RenderSession,
sheets: &'a [Sheet],
compose: ComposeOptions,
},
}
pub struct SpoolJob<'a> {
pub printer: PrinterId,
pub title: String,
pub options: &'a PrintOptions,
pub payload: SpoolPayload<'a>,
}
impl std::fmt::Debug for SpoolPayload<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PassThroughPdf(bytes) => f
.debug_struct("PassThroughPdf")
.field("bytes", &bytes.len())
.finish(),
Self::Sheets {
sheets, compose, ..
} => f
.debug_struct("Sheets")
.field("sheets", &sheets.len())
.field("compose", compose)
.finish_non_exhaustive(),
}
}
}
impl std::fmt::Debug for SpoolJob<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SpoolJob")
.field("printer", &self.printer)
.field("title", &self.title)
.field("payload", &self.payload)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobId(pub String);
impl std::fmt::Display for JobId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobStatus {
Pending,
Processing,
Completed,
Cancelled,
Aborted,
Unknown,
}
pub trait Spooler: Send + Sync {
fn printers(&self) -> Result<Vec<PrinterInfo>, PrintError>;
fn default_printer(&self) -> Result<Option<PrinterId>, PrintError> {
Ok(self
.printers()?
.into_iter()
.find(|p| p.is_default)
.map(|p| p.id))
}
fn capabilities(&self, printer: &PrinterId) -> Result<DeviceCapabilities, PrintError>;
fn plan_sheets(
&self,
_printer: &PrinterId,
job: &crate::print::PrintJob,
capabilities: &DeviceCapabilities,
) -> Result<Vec<Sheet>, PrintError> {
crate::print::plan_sheets(job, capabilities)
}
fn submit(&self, job: SpoolJob<'_>) -> Result<JobId, PrintError>;
fn status(&self, job: &JobId) -> Result<JobStatus, PrintError>;
fn cancel(&self, job: &JobId) -> Result<(), PrintError>;
}
pub fn platform_spooler() -> Box<dyn Spooler> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
Box::new(cups::CupsSpooler::new())
}
#[cfg(windows)]
{
Box::new(windows::WindowsSpooler::new())
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
{
Box::new(file::FileSpooler::new(std::env::temp_dir()))
}
}