mod references;
mod resources;
mod support;
pub(super) use resources::Resource;
pub(super) use support::paths;
use super::{ParagraphBlockBudget, RepeatParagraphBlockRequest};
use crate::{
document_core::DocumentCore,
error::HwpError,
model::{control::Control, paragraph::Paragraph, shape::ShapeObject},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(
tag = "kind",
content = "index",
rename_all = "camelCase",
deny_unknown_fields
)]
pub enum ParagraphBlockPathStep {
Paragraph(usize),
Control(usize),
Shape,
Cell(usize),
TextBox,
Caption,
GroupChild(usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ParagraphBlockValidationError {
pub code: String,
pub path: Vec<ParagraphBlockPathStep>,
pub detail: String,
}
impl std::fmt::Display for ParagraphBlockValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"paragraph block {} at {:?}: {}",
self.code, self.path, self.detail
)
}
}
impl std::error::Error for ParagraphBlockValidationError {}
impl From<HwpError> for ParagraphBlockValidationError {
fn from(error: HwpError) -> Self {
Self {
code: "budgetOrAddress".into(),
path: vec![],
detail: error.to_string(),
}
}
}
pub(super) fn reject(
path: &[ParagraphBlockPathStep],
code: &str,
detail: impl Into<String>,
) -> ParagraphBlockValidationError {
ParagraphBlockValidationError {
code: code.into(),
path: path.to_vec(),
detail: detail.into(),
}
}
#[derive(Clone, Copy)]
enum SourceNode<'a> {
Paragraph(&'a Paragraph),
Control(&'a Control),
Shape(&'a ShapeObject),
}
struct Located<'a> {
node: SourceNode<'a>,
path: Vec<ParagraphBlockPathStep>,
}
pub(super) fn paragraphs<'a>(
paragraphs: &'a [Paragraph],
request: &RepeatParagraphBlockRequest,
) -> Result<Vec<(Vec<ParagraphBlockPathStep>, &'a Paragraph)>, ParagraphBlockValidationError> {
Ok(support::inspect(paragraphs, request)?
.into_iter()
.filter_map(|located| match located.node {
SourceNode::Paragraph(p) => Some((located.path, p)),
_ => None,
})
.collect())
}
pub(super) fn row_source(
doc: &crate::model::document::Document,
paras: &[Paragraph],
original: &crate::model::table::Table,
cells: &[usize],
request: &RepeatParagraphBlockRequest,
) -> Result<(), ParagraphBlockValidationError> {
let nodes = support::inspect(paras, request)?;
let mut originals = Vec::new();
for (relative, index) in cells.iter().enumerate() {
for mut located in support::inspect(&original.cells[*index].paragraphs, request)? {
let mut path = vec![
ParagraphBlockPathStep::Paragraph(0),
ParagraphBlockPathStep::Control(0),
ParagraphBlockPathStep::Cell(relative),
];
path.append(&mut located.path);
located.path = path;
originals.push(located);
}
}
references::validate(doc, &originals, request.limits.max_document_nodes)?;
resources::validate(doc, &nodes, request)
}
impl DocumentCore {
pub fn validate_paragraph_block_native(
&self,
request: &RepeatParagraphBlockRequest,
) -> Result<ParagraphBlockBudget, ParagraphBlockValidationError> {
validate_document_block(self.document(), request)
}
}
pub(super) fn validate_document_block(
document: &crate::model::document::Document,
request: &RepeatParagraphBlockRequest,
) -> Result<ParagraphBlockBudget, ParagraphBlockValidationError> {
let no_op = RepeatParagraphBlockRequest {
count: 0,
..request.clone()
};
let zero = super::document_block_budget(document, &no_op)?;
if request.count == 0 {
return Ok(zero);
}
if request.count > request.limits.max_copies
|| (request.source_end - request.source_start)
.checked_mul(request.count)
.is_none_or(|n| n > request.limits.max_paragraphs)
{
return Err(reject(
&[],
"budgetOrAddress",
"copy/paragraph budget exceeded",
));
}
let paragraphs = &document.sections[request.section_index].paragraphs
[request.source_start..request.source_end];
let nodes = support::inspect(paragraphs, request)?;
let budget = super::document_block_budget(document, request)?;
references::validate(document, &nodes, request.limits.max_document_nodes)?;
resources::validate(document, &nodes, request)?;
Ok(budget)
}
pub(super) fn reachable_resources(
document: &crate::model::document::Document,
request: &RepeatParagraphBlockRequest,
) -> Result<std::collections::BTreeSet<Resource>, ParagraphBlockValidationError> {
let paragraphs = &document.sections[request.section_index].paragraphs
[request.source_start..request.source_end];
let nodes = support::inspect(paragraphs, request)?;
resources::collect(document, &nodes, request)
}