use serde::Serialize;
use crate::{DecodeOutcome, Error, KNOWN_COMPATIBLE_NAMESPACES};
#[derive(Clone, Debug, Serialize)]
pub struct Limits {
pub max_input_bytes: u64,
pub max_documents: usize,
pub max_base64_chars: usize,
pub max_decoded_document_bytes: u64,
pub max_total_decoded_bytes: u64,
pub max_zip_members: usize,
pub max_zip_expanded_bytes: u64,
pub max_zip_compression_ratio: u64,
pub max_xml_depth: usize,
pub max_xml_nodes: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_input_bytes: 64 * 1024 * 1024,
max_documents: 256,
max_base64_chars: 96 * 1024 * 1024,
max_decoded_document_bytes: 64 * 1024 * 1024,
max_total_decoded_bytes: 256 * 1024 * 1024,
max_zip_members: 16,
max_zip_expanded_bytes: 64 * 1024 * 1024,
max_zip_compression_ratio: 100,
max_xml_depth: 128,
max_xml_nodes: 1_000_000,
}
}
}
#[derive(Clone, Debug)]
pub struct ParseOptions {
pub limits: Limits,
pub allowed_namespaces: Vec<String>,
}
impl Default for ParseOptions {
fn default() -> Self {
Self {
limits: Limits::default(),
allowed_namespaces: KNOWN_COMPATIBLE_NAMESPACES
.iter()
.map(|namespace| (*namespace).to_owned())
.collect(),
}
}
}
impl ParseOptions {
pub fn with_limits(limits: Limits) -> Self {
Self {
limits,
..Self::default()
}
}
pub(crate) fn allows(&self, namespace: &str) -> bool {
self.allowed_namespaces
.iter()
.any(|allowed| allowed == namespace)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum StructuralWarningCode {
DanglingObjref,
DocumentWithoutProfile,
SourceSizeMissing,
CreationDateMissing,
}
impl StructuralWarningCode {
pub const fn as_str(self) -> &'static str {
match self {
Self::DanglingObjref => "dangling_objref",
Self::DocumentWithoutProfile => "document_without_profile",
Self::SourceSizeMissing => "source_size_missing",
Self::CreationDateMissing => "creation_date_missing",
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct StructuralWarning {
pub code: StructuralWarningCode,
pub message: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct MimeType {
pub media_type: String,
pub subtype: String,
pub extension: Option<String>,
pub charset: Option<String>,
}
impl MimeType {
pub fn essence(&self) -> String {
format!("{}/{}", self.media_type, self.subtype)
}
}
#[derive(Clone, Debug, Serialize)]
pub struct Document {
pub index: usize,
pub title: String,
pub creation_date: String,
pub mime_type: MimeType,
pub source_size: Option<u64>,
pub object_ref: String,
pub transforms: Vec<String>,
pub nested_dossier: bool,
#[serde(skip)]
pub(crate) payload: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Dossier {
pub title: String,
pub category: Option<String>,
pub creation_date: Option<String>,
pub namespace: String,
pub xml_encoding: String,
pub documents: Vec<Document>,
pub signatures_present: usize,
pub timestamps_present: usize,
pub warnings: Vec<StructuralWarning>,
}
impl Dossier {
pub fn decode_document(&self, index: usize, limits: &Limits) -> Result<DecodeOutcome, Error> {
crate::decode::decode_document(
self.documents.get(index).ok_or_else(|| {
Error::new(
crate::ErrorCode::InvalidAttribute,
"document index is out of range",
)
})?,
limits,
)
}
}