use std::collections::BTreeMap;
use crate::Document;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Limits {
pub max_path_bytes: usize,
pub max_files: usize,
pub max_file_bytes: usize,
pub max_total_bytes: usize,
pub max_fonts: usize,
pub max_font_bytes: usize,
pub max_total_font_bytes: usize,
pub max_inputs: usize,
pub max_input_bytes: usize,
pub max_pages: usize,
pub max_pdf_bytes: usize,
pub max_raster_pixels: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_path_bytes: 4 * 1024,
max_files: 256,
max_file_bytes: 16 * 1024 * 1024,
max_total_bytes: 64 * 1024 * 1024,
max_fonts: 64,
max_font_bytes: 16 * 1024 * 1024,
max_total_font_bytes: 64 * 1024 * 1024,
max_inputs: 256,
max_input_bytes: 2 * 1024 * 1024,
max_pages: 32,
max_pdf_bytes: 64 * 1024 * 1024,
max_raster_pixels: 20_000_000,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum DiagnosticsPolicy {
#[default]
DenyWarnings,
AllowWarnings,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum PageConstraint {
#[default]
Any,
Exactly(usize),
Between {
minimum: usize,
maximum: usize,
},
}
impl PageConstraint {
pub(crate) fn accepts(self, actual: usize, hard_maximum: usize) -> bool {
if actual == 0 || actual > hard_maximum {
return false;
}
match self {
Self::Any => true,
Self::Exactly(expected) => actual == expected,
Self::Between { minimum, maximum } => (minimum..=maximum).contains(&actual),
}
}
pub(crate) fn describe(self, hard_maximum: usize) -> String {
match self {
Self::Any => format!("1-{hard_maximum}"),
Self::Exactly(expected) => expected.to_string(),
Self::Between { minimum, maximum } => format!("{minimum}-{maximum}"),
}
}
}
#[derive(Clone, Debug)]
pub struct CompileRequest {
pub(crate) source: String,
pub(crate) inputs: BTreeMap<String, String>,
pub(crate) sources: BTreeMap<String, String>,
pub(crate) binaries: BTreeMap<String, Vec<u8>>,
pub(crate) pages: PageConstraint,
pub(crate) diagnostics: DiagnosticsPolicy,
}
impl CompileRequest {
#[must_use]
pub fn new(source: impl Into<String>) -> Self {
Self {
source: source.into(),
inputs: BTreeMap::new(),
sources: BTreeMap::new(),
binaries: BTreeMap::new(),
pages: PageConstraint::Any,
diagnostics: DiagnosticsPolicy::DenyWarnings,
}
}
#[must_use]
pub fn input(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.inputs.insert(key.into(), value.into());
self
}
#[must_use]
pub fn inputs(mut self, inputs: BTreeMap<String, String>) -> Self {
self.inputs = inputs;
self
}
#[must_use]
pub fn source_file(mut self, path: impl Into<String>, source: impl Into<String>) -> Self {
self.sources.insert(path.into(), source.into());
self
}
#[must_use]
pub fn binary_file(mut self, path: impl Into<String>, binary: impl Into<Vec<u8>>) -> Self {
self.binaries.insert(path.into(), binary.into());
self
}
#[must_use]
pub const fn pages(mut self, pages: PageConstraint) -> Self {
self.pages = pages;
self
}
#[must_use]
pub const fn diagnostics(mut self, diagnostics: DiagnosticsPolicy) -> Self {
self.diagnostics = diagnostics;
self
}
}
pub struct CompileOutput {
pub document: Document,
pub warnings: Vec<String>,
}
pub struct CompileReport {
pub result: crate::Result<CompileOutput>,
pub dependencies: Vec<std::path::PathBuf>,
}