use crate::docgen;
use crate::tools::path::PathAccess;
use crate::{Tool, Workspace};
use anyhow::{Context as _, Result};
use async_trait::async_trait;
use chrono::{Datelike as _, Timelike as _};
use serde_json::{Value, json};
use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use super::GENERATED_DIR;
pub(crate) const FILE_MARKER: &str = "[FILE:";
const MAX_BASE_CHARS: usize = 80;
const MAX_SPLIT_PARTS: usize = 50;
const MAX_MERGE_INPUTS: usize = 50;
const MAX_MERGE_BYTES: u64 = 200 * 1024 * 1024;
const UNSUPPORTED_NOTE_MAX: usize = 10;
const ACTIONS: [&str; 13] = [
"create",
"fill_template",
"docx_edit",
"xlsx_edit",
"pptx_edit",
"pdf_merge",
"pdf_split",
"pdf_rotate",
"pdf_crop",
"pdf_resize",
"pdf_text",
"pdf_image",
"pdf_form_fill",
];
#[expect(clippy::too_many_lines)] fn edit_op_fields(
family: crate::ooxml::Family,
) -> &'static [(&'static str, &'static [&'static str])] {
match family {
crate::ooxml::Family::Docx => &[
("replace_text", &["find", "replace"]),
("insert_text", &["find", "insert", "position"]),
("remove_text", &["find"]),
("remove_paragraph", &["find"]),
(
"format_text",
&[
"find",
"bold",
"italic",
"underline",
"strike",
"size",
"color",
],
),
(
"format_paragraph",
&[
"find",
"align",
"style",
"indent_left",
"indent_right",
"indent_first_line",
"spacing_before",
"spacing_after",
"line_spacing",
"list",
"level",
],
),
("add_paragraph", &["text", "after"]),
("add_image", &["after", "image", "width", "height"]),
("add_table", &["after", "headers", "rows"]),
],
crate::ooxml::Family::Xlsx => &[
("set_cell", &["sheet", "cell", "value", "number_format"]),
("clear_cell", &["sheet", "cell"]),
(
"format_cells",
&[
"sheet",
"range",
"column",
"row",
"font",
"size",
"bold",
"italic",
"underline",
"strike",
"color",
"fill",
"border",
"align",
"vertical",
"wrap",
"number_format",
"width_chars",
"height_points",
],
),
("insert_row", &["sheet", "row"]),
("delete_row", &["sheet", "row"]),
("insert_column", &["sheet", "column"]),
("delete_column", &["sheet", "column"]),
("merge_cells", &["sheet", "range"]),
("unmerge_cells", &["sheet", "range"]),
("add_sheet", &["name"]),
("rename_sheet", &["sheet", "name"]),
("delete_sheet", &["sheet"]),
("move_sheet", &["sheet", "to"]),
("add_image", &["sheet", "cell", "image", "width", "height"]),
],
crate::ooxml::Family::Pptx => &[
("replace_text", &["slide", "find", "replace"]),
("remove_text", &["slide", "find"]),
(
"format_text",
&[
"slide",
"find",
"bold",
"italic",
"underline",
"strike",
"size",
"color",
"align",
],
),
("add_paragraph", &["slide", "text", "after", "level"]),
("remove_paragraph", &["slide", "find"]),
(
"add_image",
&[
"slide",
"image",
"x_fraction",
"y_fraction",
"width_fraction",
"height_fraction",
],
),
(
"add_table",
&[
"slide",
"headers",
"rows",
"x_fraction",
"y_fraction",
"width_fraction",
"height_fraction",
],
),
("add_slide", &["after", "title", "bullets"]),
("delete_slide", &["slide"]),
("move_slide", &["slide", "to"]),
("duplicate_slide", &["slide"]),
("replace_notes", &["slide", "find", "replace"]),
("remove_notes", &["slide", "find"]),
("add_notes", &["slide", "text"]),
],
}
}
fn op_fields(family: crate::ooxml::Family, op: &str) -> &'static [&'static str] {
edit_op_fields(family)
.iter()
.find(|(name, _)| *name == op)
.map(|(_, fields)| *fields)
.expect("the op was checked against the family's vocabulary")
}
const PPTX_SLIDE_TEXT_OPS: [&str; 5] = [
"replace_text",
"remove_text",
"format_text",
"add_paragraph",
"remove_paragraph",
];
const SUPPORTED_FORMATS: [&str; 4] = [
crate::ooxml::Family::Docx.name(),
crate::ooxml::Family::Xlsx.name(),
crate::ooxml::Family::Pptx.name(),
"pdf",
];
#[derive(serde::Deserialize)]
struct Span {
min: f64,
max: f64,
#[serde(default)]
min_exclusive: bool,
}
impl Span {
fn contains(&self, value: f64) -> bool {
let above_min = if self.min_exclusive {
value > self.min
} else {
value >= self.min
};
above_min && value <= self.max
}
fn bounds(&self) -> String {
if self.min_exclusive {
format!("greater than {} and at most {}", self.min, self.max)
} else {
format!("between {} and {}", self.min, self.max)
}
}
}
#[derive(serde::Deserialize)]
struct Rules {
heading_levels: Vec<f64>,
scalar_kinds: Vec<String>,
image_extensions: Vec<String>,
table_columns_max: usize,
dimension_points: Span,
slide_position_fraction: Span,
slide_size_fraction: Span,
text_size_points: Span,
paragraph_alignments: Vec<(String, String)>,
paragraph_lists: Vec<(String, String)>,
paragraph_indent_points: Span,
paragraph_spacing_points: Span,
paragraph_line_points: Span,
paragraph_style_max: usize,
slide_text_size_points: Span,
cell_font_points: Span,
column_width_chars: Span,
row_height_points: Span,
font_name_max: usize,
format_cells_max: usize,
max_pages_per_request: usize,
pdf_point_abs_max: f64,
pdf_page_fraction: Span,
pdf_fraction_slack: f64,
pdf_opacity: Span,
color_digits: usize,
slide_alignments: Vec<(String, String)>,
cell_alignments: Vec<String>,
cell_verticals: Vec<String>,
cell_border_sides: Vec<String>,
degrees_step: i64,
sheet_name_max: usize,
sheet_name_forbidden: String,
edit_text_max: usize,
edits_max: usize,
bullets_max: usize,
paragraph_level_max: u32,
sheet_row_max: u32,
sheet_column_max: u32,
number_format_max: usize,
pptx_sizes: Vec<PptxSize>,
pptx_default_size: DeckSize,
}
#[derive(serde::Deserialize)]
struct DeckSize {
width_emu: u64,
height_emu: u64,
}
impl DeckSize {
fn inches(&self) -> String {
const EMU_PER_INCH: u64 = 914_400;
let side = |emu: u64| {
let fraction = format!("{:03}", emu % EMU_PER_INCH * 1000 / EMU_PER_INCH);
let fraction = fraction.trim_end_matches('0');
let dot = if fraction.is_empty() { "" } else { "." };
format!("{}{dot}{fraction}", emu / EMU_PER_INCH)
};
format!("{} × {} in", side(self.width_emu), side(self.height_emu))
}
}
#[derive(serde::Deserialize)]
struct PptxSize {
name: String,
#[serde(flatten)]
deck: DeckSize,
}
static RULES: LazyLock<Rules> = LazyLock::new(|| {
serde::Deserialize::deserialize(crate::docgen::rules())
.expect("assets/docgen/rules.json must state the rules the document tool enforces")
});
pub(crate) struct DocumentTool {
access: PathAccess,
}
impl DocumentTool {
#[must_use]
pub(crate) const fn new(access: PathAccess) -> Self {
Self { access }
}
const fn input_location(&self) -> &'static str {
match self.access {
PathAccess::Unrestricted => {
"readable from anywhere on this machine (the product's own live databases excepted)"
}
PathAccess::Workspace | PathAccess::Allowlisted => "confined to your workspace",
}
}
}
#[async_trait]
impl Tool for DocumentTool {
fn name(&self) -> &'static str {
"document"
}
fn is_advertised(&self) -> bool {
crate::tools::bun::bun_binary_path().is_some()
}
fn media_marker(&self) -> Option<&'static str> {
Some(FILE_MARKER)
}
fn preserve_full_output(&self) -> bool {
true
}
fn description(&self) -> String {
let ppt = crate::reader_output::ppt_description_substitutions();
let input_mb = megabytes(crate::util::FILE_MAX_BYTES).to_string();
let edit_text_max = RULES.edit_text_max.to_string();
let docx_inline_marks = spelled_docx_marks(true);
let docx_label_marks = spelled_docx_marks(false);
let mut pairs: Vec<(&str, &str)> = vec![
("{{input_location}}", self.input_location()),
("{{input_mb}}", &input_mb),
("{{edit_text_max}}", &edit_text_max),
("{{docx_inline_marks}}", &docx_inline_marks),
("{{docx_label_marks}}", &docx_label_marks),
];
pairs.extend(ppt.iter().map(|(key, value)| (*key, value.as_str())));
crate::prompt::substitute(&crate::prompt::load_prompt("tool/document.md"), &pairs)
}
fn parameters_schema(&self) -> Value {
super::tool_params_schema(
&json!({
"action": {
"oneOf": ACTIONS.iter().map(|action| action_schema(action)).collect::<Vec<_>>()
},
"file_name": file_name_arg(),
"metadata": metadata_arg(),
}),
&["action"],
)
}
async fn execute(&self, ws: &Workspace, args: Value) -> Result<String> {
Ok(self.dispatch(ws, args).await?.text)
}
async fn execute_answer(&self, ws: &Workspace, args: Value) -> Result<super::ToolAnswer> {
self.dispatch(ws, args).await
}
}
fn normalize_call(args: &Value) -> Result<Value> {
let nested_shape = || {
anyhow::anyhow!(
"usage: \"action\" must name one of {} as a string with the action's own arguments \
beside it, or as an object naming one action — hint: like {{\"action\": \"pdf_rotate\", \
\"path\": \"…\", \"degrees\": 90}} or {{\"action\": {{\"pdf_rotate\": {{…}}}}}}",
ACTIONS.join(", ")
)
};
let flattened = |name: &str, fields: &serde_json::Map<String, Value>| {
let object = args
.as_object()
.expect("a call whose action matched is an object");
let mut flat = object
.iter()
.filter(|(key, _)| key.as_str() != "action" && key.as_str() != name)
.map(|(key, value)| (key.clone(), value.clone()))
.collect::<serde_json::Map<String, Value>>();
flat.extend(fields.clone());
flat.insert("action".to_owned(), json!(name));
if let Some(file_name) = args.get("file_name") {
flat.insert("file_name".to_owned(), file_name.clone());
}
Value::Object(flat)
};
match args.get("action") {
Some(Value::String(name)) => match args.get(name.as_str()) {
Some(Value::Object(fields)) => Ok(flattened(name, fields)),
_ => Ok(args.clone()),
},
Some(Value::Object(action)) if action.len() == 1 => {
let (name, fields) = action.iter().next().expect("one key");
match fields {
Value::Object(fields) => Ok(flattened(name, fields)),
_ => Err(nested_shape()),
}
}
_ => Err(nested_shape()),
}
}
fn require_declared_arguments(action: &str, args: &Value) -> Result<()> {
let entry = action_schema(action);
let declared = entry["properties"][action]["properties"]
.as_object()
.expect("every action's entry declares its arguments");
let accepted = std::iter::once("action")
.chain(std::iter::once("file_name"))
.chain(declared.keys().map(String::as_str))
.collect::<Vec<_>>();
let Some(object) = args.as_object() else {
return Ok(());
};
let unknown = object
.keys()
.map(String::as_str)
.filter(|key| !accepted.contains(key) && stated(object, key))
.collect::<Vec<_>>();
if !unknown.is_empty() {
let named = listed(
&unknown
.iter()
.map(|key| format!("\"{key}\""))
.collect::<Vec<_>>(),
);
anyhow::bail!(
"usage: {action} takes no argument {named} — hint: it takes {}",
listed(&accepted)
);
}
Ok(())
}
fn text_arg(description: &str) -> Value {
json!({ "type": "string", "description": description })
}
fn number_arg(description: &str) -> Value {
json!({ "type": "number", "description": description })
}
fn integer_arg(description: &str) -> Value {
json!({ "type": "integer", "description": description })
}
fn toggle_arg() -> Value {
json!({ "type": "boolean" })
}
fn bool_arg(description: &str) -> Value {
json!({ "type": "boolean", "description": description })
}
fn choice_arg<S: AsRef<str>>(values: &[S], description: &str) -> Value {
let values: Vec<&str> = values.iter().map(AsRef::as_ref).collect();
json!({ "type": "string", "enum": values, "description": description })
}
fn array_arg(items: &Value, description: &str) -> Value {
json!({ "type": "array", "items": items, "description": description })
}
fn scalar_arg() -> Value {
json!({ "type": RULES.scalar_kinds })
}
fn file_name_arg() -> Value {
text_arg(&format!(
"Base name for the produced file (max {MAX_BASE_CHARS} chars); the tool appends the \
extension, puts the file in the workspace's generated/ directory and makes the name \
unique."
))
}
fn metadata_arg() -> Value {
let properties = crate::docgen::pdf_metadata_fields()
.iter()
.map(|field| (field.key.clone(), text_arg(&field.description)))
.collect::<serde_json::Map<_, _>>();
json!({
"type": "object",
"minProperties": 1,
"additionalProperties": false,
"description": format!(
"The PDF's own description. A field left out stays as the file has it — except an \
existing modified date, which advances to a write; an empty string clears one; \
keywords is the single text the file carries. A date is {}.",
crate::docgen::pdf_date_format()
),
"properties": properties,
})
}
fn metadata_entry() -> Value {
json!({ "type": "object" })
}
fn path_arg() -> Value {
text_arg("The document to work on.")
}
fn pdf_image_arg() -> Value {
picture_arg(pdf_pictures().names)
}
fn image_arg() -> Value {
picture_arg(embedded_pictures().names)
}
fn content_image_arg() -> Value {
picture_arg(&format!(
"{}; a PDF takes {}",
embedded_pictures().names,
pdf_pictures().names
))
}
#[cfg(test)]
pub(crate) fn image_args() -> Vec<Value> {
vec![image_arg(), content_image_arg(), pdf_image_arg()]
}
fn picture_arg(names: &str) -> Value {
text_arg(&format!(
"The picture to place ({names}); naming one side's size alone keeps its own proportions."
))
}
fn paths_arg() -> Value {
json!({
"type": "array",
"items": text_arg("Path of one PDF."),
"minItems": 2,
"maxItems": MAX_MERGE_INPUTS,
"description": format!(
"The PDFs to merge, in order: at most {} MB in total.",
megabytes(MAX_MERGE_BYTES)
),
})
}
fn values_arg(kind: &str) -> Value {
json!({
"type": "object",
"minProperties": 1,
"description": format!(
"One entry per {kind}, keyed by its name; a value is text, a number or a boolean."
),
})
}
fn pages_arg(split: bool) -> Value {
let element = text_arg(
"An entry: \"all\", a page, a range, or a comma list like \"1-3,5\"; \"all\" stands alone.",
);
if split {
json!({
"type": "array",
"items": element,
"minItems": 1,
"maxItems": MAX_SPLIT_PARTS,
"description": format!(
"One output file per entry, in order: [\"1-3\", \"5\"] makes two files, and \
[\"1-3,5\"] one file holding those four pages. At most {MAX_SPLIT_PARTS} files \
and {} pages.",
RULES.max_pages_per_request
),
})
} else {
array_arg(
&element,
&format!(
"The pages to act on, as [\"1-3\", \"5\"]; absent means all pages. At most \
{} pages.",
RULES.max_pages_per_request
),
)
}
}
fn row_arg() -> Value {
json!({ "type": "array", "items": scalar_arg() })
}
fn picture_size_rule() -> String {
format!(
"in points, {}; a side derived from the other one is held to the same bound",
RULES.dimension_points.bounds()
)
}
fn placed_size_arg(what: &str) -> Value {
number_arg(&format!("{what} {}.", picture_size_rule()))
}
const BLOCK_IMAGE_FIT: &str = "with neither side named the picture keeps its own size, fitted \
whole into the page's or slide's own text column.";
fn picture_width_arg(what: &str, fallback: &str) -> Value {
number_arg(&format!("{what} {}; {fallback}", picture_size_rule()))
}
fn border_arg() -> Value {
let sides = RULES
.cell_border_sides
.iter()
.map(|side| (side.clone(), bool_arg("Set this side.")))
.collect::<serde_json::Map<String, Value>>();
json!({
"type": "object",
"additionalProperties": false,
"properties": sides,
"description": "The sides to set — true draws a thin border on that side, false takes its \
own away — at least one of them.",
})
}
fn color_arg(what: &str) -> Value {
text_arg(&format!("{what}: {} hex digits.", RULES.color_digits))
}
fn list_arg() -> Value {
let mut values = pair_words(&RULES.paragraph_lists)
.into_iter()
.map(|word| json!(word))
.collect::<Vec<_>>();
values.push(json!(false));
json!({
"type": ["string", "boolean"],
"enum": values,
"description": "The list kind, or false to take the paragraph's list away; absent keeps \
its numbering.",
})
}
fn fill_arg() -> Value {
json!({
"anyOf": [
{ "type": "string" },
{ "enum": [false] },
],
"description": format!(
"Background: {} hex digits, or false to take the cell's fill away.",
RULES.color_digits
),
})
}
const CONTENT_BLOCKS: &[(&str, &[&str], &[&str])] = &[
("heading", &["text", "level"], &["text"]),
("paragraph", &["text", "footnote"], &["text"]),
("notes", &["text"], &["text"]),
("list", &["items", "ordered"], &["items"]),
("table", &["headers", "rows"], &[]),
("image", &["image", "width", "height"], &["image"]),
("link", &["text", "url"], &["text", "url"]),
("page_break", &[], &[]),
("contents", &[], &[]),
];
fn block_kind_note(kind: &str) -> Option<String> {
match kind {
"page_break" => Some("A page break: the next block starts a new page.".to_owned()),
"link" => Some("A paragraph holding a link: its text and the address it opens.".to_owned()),
"contents" => Some(format!(
"A table of contents over the document's headings (levels {}), with the page numbers \
already written in; one per document.",
listed(&RULES.heading_levels)
)),
_ => None,
}
}
fn block_field(name: &str) -> Value {
match name {
"text" => text_arg("The block's text."),
"level" => integer_arg(&format!("Level {}.", listed(&RULES.heading_levels))),
"items" => array_arg(&scalar_arg(), "The bullets."),
"ordered" => bool_arg("Number them."),
"headers" => array_arg(
&scalar_arg(),
&format!(
"The header cells; a .docx table holds at most {} columns.",
RULES.table_columns_max
),
),
"rows" => array_arg(
&row_arg(),
&format!(
"The rows, each an array of cells; a .docx table holds at most {} columns.",
RULES.table_columns_max
),
),
"image" => content_image_arg(),
"width" => picture_width_arg("Placed width", BLOCK_IMAGE_FIT),
"height" => placed_size_arg("Placed height"),
"footnote" => text_arg(
"A footnote attached to this paragraph: its text is printed at the foot of the \
page the paragraph lands on.",
),
"url" => text_arg(
"The address the link opens: an http, https, mailto or tel address, e.g. \
https://example.com; another scheme is refused.",
),
other => unreachable!("every block field has a declaration: {other}"),
}
}
fn content_arg() -> Value {
let variants = CONTENT_BLOCKS
.iter()
.map(|(kind, fields, required)| {
let mut properties = serde_json::Map::new();
properties.insert(
"type".to_owned(),
match block_kind_note(kind) {
Some(note) => json!({ "enum": [kind], "description": note }),
None => json!({ "enum": [kind] }),
},
);
properties.extend(
fields
.iter()
.map(|name| ((*name).to_owned(), block_field(name))),
);
json!({
"type": "object",
"additionalProperties": false,
"required": std::iter::once("type").chain(required.iter().copied()).collect::<Vec<_>>(),
"properties": properties,
})
})
.collect::<Vec<_>>();
json!({
"type": "array",
"minItems": 1,
"items": { "oneOf": variants },
"description": "The blocks, in order, each with its own \"type\"; \"notes\" is pptx speaker \
notes, a table needs a row or a header, and a link, a page break, a \
footnote and a table of contents are a .docx's own.",
})
}
const SHEET_FIELDS: &[(&str, bool)] = &[
("name", false),
("rows", true),
("merges", false),
];
fn sheet_field_names() -> Vec<&'static str> {
SHEET_FIELDS.iter().map(|(name, _)| *name).collect()
}
fn sheets_arg() -> Value {
let properties = SHEET_FIELDS
.iter()
.map(|(name, _)| ((*name).to_owned(), sheet_field(name)))
.collect::<serde_json::Map<String, Value>>();
let required = SHEET_FIELDS
.iter()
.filter(|(_, required)| *required)
.map(|(name, _)| *name)
.collect::<Vec<_>>();
json!({
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"additionalProperties": false,
"required": required,
"properties": properties,
},
"description": "The workbook's sheets, in order.",
})
}
fn sheet_field(name: &str) -> Value {
match name {
"name" => text_arg(&format!(
"Sheet name: at most {} characters, none of {:?}, unique ignoring case; without one it \
becomes Sheet<n>.",
RULES.sheet_name_max, RULES.sheet_name_forbidden,
)),
"rows" => array_arg(&sheet_row_arg(), "The rows, top to bottom."),
"merges" => array_arg(
&text_arg("A range like A1:C1, corners in either order."),
"Ranges to merge, each covering at least two cells; a merge never clears what its cells \
hold.",
),
other => unreachable!("every sheet field has a declaration: {other}"),
}
}
fn cell_arg() -> Value {
json!({
"anyOf": [
scalar_arg(),
{
"type": "object",
"additionalProperties": false,
"properties": { "formula": text_arg("The formula, without a leading =.") },
"required": ["formula"],
},
],
"description": "Text (a string starting with `=` stays text), a number, a boolean, or \
{\"formula\": \"SUM(A1:A2)\"}; a number is written as the cell stores it, \
not as a reading displayed it.",
})
}
fn sheet_row_arg() -> Value {
array_arg(
&cell_arg(),
"One row, one cell after another, left to right.",
)
}
const TOGGLE_RULE: &str = "bold, italic, underline and strike are toggles: true sets the \
property, false switches it off.";
fn edits_arg(family: crate::ooxml::Family, fields: &Value, rules: &str) -> Value {
let op_fields = edit_op_fields(family);
let ops: Vec<&str> = op_fields.iter().map(|(op, _)| *op).collect();
let mut properties = fields.clone();
let fields_by_op = op_fields
.iter()
.map(|(op, taken)| format!("{op}: {}", taken.join(", ")))
.collect::<Vec<_>>()
.join("; ");
let statements = [
rules,
TOGGLE_RULE,
&format!("Fields by op: {fields_by_op}."),
];
let description = statements
.iter()
.filter(|part| !part.is_empty())
.copied()
.collect::<Vec<_>>()
.join(" ");
properties["op"] = choice_arg(&ops, &description);
json!({
"type": "array",
"minItems": 1,
"maxItems": RULES.edits_max,
"items": {
"type": "object",
"additionalProperties": false,
"properties": properties,
"required": ["op"],
},
"description": format!(
"The edits, in order, at most {}. Applied whole or not at all: an edit whose fields \
do not fit its op is refused by name rather than partly applied.",
RULES.edits_max
),
})
}
#[expect(clippy::too_many_lines)] fn action_schema(action: &str) -> Value {
match action {
"create" => super::action_entry_schema(
"create",
"Write a new file: blocks for docx/pptx/pdf, sheets for xlsx. A PDF is laid out as a \
simple stream: blocks follow one another down the page, and columns, floating objects \
and exact page placement are not attempted. A .docx may carry a running header and \
footer, page numbers, a table of contents over its headings, links, footnotes and \
explicit page breaks.",
&["format"],
&json!({
"format": choice_arg(&SUPPORTED_FORMATS, "The format to write."),
"content": content_arg(),
"sheets": sheets_arg(),
"metadata": metadata_entry(),
"size": choice_arg(
&pptx_size_words(),
&format!(
"A pptx slide size: {}. Unnamed is the {} deck this tool has always \
written.",
pptx_size_list(),
RULES.pptx_default_size.inches()
),
),
"header": text_arg(
"A running header, one paragraph per line: {page} is the page number and \
{pages} the document's page count.",
),
"footer": text_arg(
"A running footer, one paragraph per line: {page} is the page number and \
{pages} the document's page count.",
),
}),
),
"fill_template" => super::action_entry_schema(
"fill_template",
"Fill a copy of the user's own .docx/.pptx/.xlsx sample's placeholders.",
&["path", "values"],
&json!({ "path": path_arg(), "values": values_arg("placeholder") }),
),
"docx_edit" => super::action_entry_schema(
"docx_edit",
"Edit a .docx/.docm; a .docx package is required. Rewrite and format its text, and \
add paragraphs, pictures and tables.",
&["path", "edits"],
&json!({
"path": path_arg(),
"edits": edits_arg(
crate::ooxml::Family::Docx,
&json!({
"find": text_arg("Text to find."),
"replace": text_arg("Text to write."),
"insert": text_arg("Text to insert."),
"position": choice_arg(&["before", "after"], "Where it lands."),
"text": text_arg("The paragraph's text."),
"after": text_arg("The paragraph to land after."),
"bold": toggle_arg(),
"italic": toggle_arg(),
"underline": toggle_arg(),
"strike": toggle_arg(),
"size": number_arg(&format!(
"Font size, {} points.",
RULES.text_size_points.bounds()
)),
"color": color_arg("Text colour"),
"align": choice_arg(
&pair_words(&RULES.paragraph_alignments),
"Paragraph alignment."
),
"style": text_arg(&format!(
"A paragraph style the document defines, at most {} characters; \
Normal, Heading 1, Heading 2 and Heading 3 always work.",
RULES.paragraph_style_max
)),
"indent_left": number_arg(&format!(
"Left indent, {} points.",
RULES.paragraph_indent_points.bounds()
)),
"indent_right": number_arg(&format!(
"Right indent, {} points.",
RULES.paragraph_indent_points.bounds()
)),
"indent_first_line": number_arg(&format!(
"First-line indent, {} points; a negative one hangs.",
RULES.paragraph_indent_points.bounds()
)),
"spacing_before": number_arg(&format!(
"Space above, {} points.",
RULES.paragraph_spacing_points.bounds()
)),
"spacing_after": number_arg(&format!(
"Space below, {} points.",
RULES.paragraph_spacing_points.bounds()
)),
"line_spacing": number_arg(&format!(
"Line height, {} points.",
RULES.paragraph_line_points.bounds()
)),
"list": list_arg(),
"level": integer_arg(&format!("List level, 0 to {}.", RULES.paragraph_level_max)),
"image": image_arg(),
"width": picture_width_arg("Picture width", BLOCK_IMAGE_FIT),
"height": placed_size_arg("Picture height"),
"headers": array_arg(
&scalar_arg(),
&format!(
"add_table: the headings row; the table holds at most {} columns, \
headings included.",
RULES.table_columns_max
),
),
"rows": array_arg(
&array_arg(&scalar_arg(), "A row of cells."),
&format!(
"add_table: the table's rows, each an array of cells — a table \
holds at most {} columns; a headings row alone makes a table.",
RULES.table_columns_max
),
),
}),
"format_text and format_paragraph need one property beyond find. add_image and \
add_table land after the first paragraph that holds their after text — a \
table cell's anchor means after the whole table — and are refused, never \
appended, when no paragraph holds it.",
),
}),
),
"xlsx_edit" => super::action_entry_schema(
"xlsx_edit",
"Edit an .xlsx/.xlsm; an .xlsx package is required. Rewrite cells and formatting, shift \
rows and columns, merge and unmerge a range, add, rename, move or delete a sheet, and \
place a picture in a sheet.",
&["path", "edits"],
&json!({
"path": path_arg(),
"edits": edits_arg(
crate::ooxml::Family::Xlsx,
&json!({
"sheet": text_arg("Sheet name, as a reading prints it."),
"name": text_arg(&format!(
"A sheet name: at most {} characters, none of {:?}, unique ignoring \
case.",
RULES.sheet_name_max, RULES.sheet_name_forbidden
)),
"to": integer_arg(
"The 1-based position the sheet takes, counting the sheets as a reading \
prints them."
),
"cell": text_arg("A1 address, like B7."),
"value": cell_arg(),
"number_format": text_arg(&format!(
"An Excel format code, at most {} characters.",
RULES.number_format_max
)),
"range": text_arg(&format!(
"Cells, like B2:D2, corners in either order; a format_cells range \
holds at most {} cells.",
RULES.format_cells_max
)),
"column": text_arg(&format!(
"Column letters, up to the {}th column.",
RULES.sheet_column_max
)),
"row": integer_arg(&format!(
"The 1-based row, up to {}.",
RULES.sheet_row_max
)),
"width_chars": number_arg(&format!(
"Column width, {} characters (as Excel shows one).",
RULES.column_width_chars.bounds()
)),
"height_points": number_arg(&format!(
"Row height, {} points.",
RULES.row_height_points.bounds()
)),
"font": text_arg(&format!(
"Font name, at most {} characters.",
RULES.font_name_max
)),
"size": number_arg(&format!(
"Font size, {} points.",
RULES.cell_font_points.bounds()
)),
"bold": toggle_arg(),
"italic": toggle_arg(),
"underline": toggle_arg(),
"strike": toggle_arg(),
"color": color_arg("Text colour"),
"fill": fill_arg(),
"border": border_arg(),
"align": choice_arg(&RULES.cell_alignments, "Horizontal alignment."),
"vertical": choice_arg(&RULES.cell_verticals, "Vertical alignment."),
"wrap": bool_arg("Wrap the text."),
"image": image_arg(),
"width": picture_width_arg(
"Picture width",
"with neither side named the picture keeps its own size.",
),
"height": placed_size_arg("Picture height"),
}),
"An edit names one sheet unless it adds one. One target only: range, column or \
row, with at least one of its own properties. An inserted row or column may be \
one past the sheet's used range; a deleted one must be inside it.",
),
}),
),
"pptx_edit" => super::action_entry_schema(
"pptx_edit",
"Edit a .pptx/.pptm; a slide is its 1-based number.",
&["path", "edits"],
&json!({
"path": path_arg(),
"edits": edits_arg(
crate::ooxml::Family::Pptx,
&json!({
"slide": integer_arg("Slide number, as a reading prints it, 1-based."),
"find": text_arg("Text to find on the slide."),
"replace": text_arg("Text to write."),
"text": text_arg("The text to write."),
"after": {
"type": ["string", "integer"],
"description": "add_paragraph: the text it follows; add_slide: the slide number.",
},
"level": integer_arg(&format!("Outline level, 0 to {}.", RULES.paragraph_level_max)),
"bold": toggle_arg(),
"italic": toggle_arg(),
"underline": toggle_arg(),
"strike": toggle_arg(),
"size": number_arg(&format!(
"Font size, {} points.",
RULES.slide_text_size_points.bounds()
)),
"color": color_arg("Text colour"),
"align": choice_arg(&pair_words(&RULES.slide_alignments), "Alignment."),
"image": image_arg(),
"headers": array_arg(
&scalar_arg(),
"add_table: the headings row; a presentation's table has no column bound.",
),
"rows": array_arg(
&array_arg(&scalar_arg(), "A row of cells."),
"add_table: the table's rows, each an array of cells — a headings row alone makes a table.",
),
"x_fraction": number_arg(&format!(
"Across the slide, {} of its width; an unnamed place is the slide's \
middle, an unnamed size a picture's largest fit or a table's own, and \
a place named without a size can push either past the slide's edge.",
RULES.slide_position_fraction.bounds()
)),
"y_fraction": number_arg(&format!(
"Down the slide, {} of its height.",
RULES.slide_position_fraction.bounds()
)),
"width_fraction": number_arg(&format!(
"Width, {} of the slide's width.",
RULES.slide_size_fraction.bounds()
)),
"height_fraction": number_arg(&format!(
"Height, {} of the slide's height.",
RULES.slide_size_fraction.bounds()
)),
"title": text_arg("The slide's title."),
"bullets": array_arg(
&scalar_arg(),
&format!("The slide's bullets, at most {}.", RULES.bullets_max),
),
"to": integer_arg("The slide it moves to."),
}),
"format_text needs one property beyond find.",
),
}),
),
"pdf_merge" => super::action_entry_schema(
"pdf_merge",
"Merge PDFs into one new file.",
&["paths"],
&json!({ "paths": paths_arg(), "metadata": metadata_entry() }),
),
"pdf_split" => super::action_entry_schema(
"pdf_split",
"Split a PDF, one output file per page entry.",
&["path", "pages"],
&json!({ "path": path_arg(), "pages": pages_arg(true), "metadata": metadata_entry() }),
),
"pdf_rotate" => super::action_entry_schema(
"pdf_rotate",
"Add a rotation to a PDF's pages.",
&["path", "degrees"],
&json!({ "path": path_arg(), "degrees": degrees_arg(), "pages": pages_arg(false),
"metadata": metadata_entry() }),
),
"pdf_crop" => super::action_entry_schema(
"pdf_crop",
"Crop a PDF page's visible area.",
&["path", "area"],
&json!({ "path": path_arg(), "area": page_area_arg(), "pages": pages_arg(false),
"metadata": metadata_entry() }),
),
"pdf_resize" => super::action_entry_schema(
"pdf_resize",
"Resize a PDF page, scaling its content to fit.",
&["path", "width", "height"],
&json!({
"path": path_arg(),
"width": number_arg(&format!(
"Page width, {} points.",
RULES.dimension_points.bounds()
)),
"height": number_arg(&format!(
"Page height, {} points.",
RULES.dimension_points.bounds()
)),
"pages": pages_arg(false),
"metadata": metadata_entry(),
}),
),
"pdf_text" => super::action_entry_schema(
"pdf_text",
"Draw text on a PDF's pages, as page content rather than an annotation.",
&["path", "text"],
&json!({
"path": path_arg(),
"text": text_arg("The text to draw."),
"pages": pages_arg(false),
"x": point_arg("X position on the page as it reads, from its left edge"),
"y": point_arg("Y position on the page as it reads, from its bottom edge"),
"size": number_arg(&format!(
"Font size, {} points.",
RULES.dimension_points.bounds()
)),
"color": color_arg("Text colour"),
"stamp": bool_arg("Draw a box around the text."),
"opacity": number_arg(&format!(
"Opacity of the drawn text and its stamp box, {}; unstated draws them solid.",
RULES.pdf_opacity.bounds()
)),
"rotate": number_arg(&format!(
"Rotation in degrees, within ±{}.",
RULES.pdf_point_abs_max
)),
"metadata": metadata_entry(),
}),
),
"pdf_image" => super::action_entry_schema(
"pdf_image",
"Place a PNG or JPEG on a PDF's pages, as page content rather than an annotation.",
&["path", "image"],
&json!({
"path": path_arg(),
"image": pdf_image_arg(),
"pages": pages_arg(false),
"x": point_arg("X position on the page as it reads, from its left edge"),
"y": point_arg("Y position on the page as it reads, from its bottom edge"),
"width": placed_size_arg("Placed width"),
"height": placed_size_arg("Placed height"),
"metadata": metadata_entry(),
}),
),
"pdf_form_fill" => super::action_entry_schema(
"pdf_form_fill",
"Fill a PDF's form fields into a copy.",
&["path", "values"],
&json!({
"path": path_arg(),
"values": values_arg("form field"),
"flatten": bool_arg("Bake the fields into the page content and remove them."),
"metadata": metadata_entry(),
}),
),
other => unreachable!("the schema lists exactly the dispatched actions: {other}"),
}
}
fn point_arg(what: &str) -> Value {
number_arg(&format!(
"{what} in points, within ±{}.",
RULES.pdf_point_abs_max
))
}
fn degrees_arg() -> Value {
integer_arg(&format!(
"A multiple of {}, not a whole turn.",
RULES.degrees_step
))
}
fn page_area_arg() -> Value {
let side = |what: &str| {
number_arg(&format!(
"{what}, as a fraction of the page ({}).",
RULES.pdf_page_fraction.bounds()
))
};
json!({
"type": "object",
"additionalProperties": false,
"required": ["x", "y", "width", "height"],
"properties": {
"x": side("The left edge"),
"y": side("The top edge"),
"width": side("The width"),
"height": side("The height"),
},
"description": "The area to keep, in fractions of the page's current visible area, \
measured from the displayed top-left and reaching down and right.",
})
}
impl DocumentTool {
async fn dispatch(&self, ws: &Workspace, args: Value) -> Result<super::ToolAnswer> {
let args = normalize_call(&args)?;
let action = super::get_str(&args, "action")?;
if ACTIONS.contains(&action) {
require_declared_arguments(action, &args)?;
}
let output = super::output_directory(ws, GENERATED_DIR);
match action {
"create" => self.create(ws, &args, &output).await,
"fill_template" => self.fill_template(ws, &args, &output).await,
"docx_edit" => {
self.edit(ws, &args, &output, crate::ooxml::Family::Docx)
.await
}
"xlsx_edit" => {
self.edit(ws, &args, &output, crate::ooxml::Family::Xlsx)
.await
}
"pptx_edit" => {
self.edit(ws, &args, &output, crate::ooxml::Family::Pptx)
.await
}
"pdf_merge" => self.pdf_merge(ws, &args, &output).await,
"pdf_split" => self.pdf_split(ws, &args, &output).await,
"pdf_rotate" => self.pdf_rotate(ws, &args, &output).await,
"pdf_crop" => self.pdf_crop(ws, &args, &output).await,
"pdf_resize" => self.pdf_resize(ws, &args, &output).await,
"pdf_text" => self.pdf_text(ws, &args, &output).await,
"pdf_image" => self.pdf_image(ws, &args, &output).await,
"pdf_form_fill" => self.pdf_form_fill(ws, &args, &output).await,
other => anyhow::bail!(
"usage: unknown action \"{other}\" — hint: use {}",
ACTIONS.join(", ")
),
}
}
async fn create(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let format = super::get_str(args, "format")?;
if !SUPPORTED_FORMATS.contains(&format) {
anyhow::bail!(
"usage: unknown format \"{format}\" — hint: use {}",
SUPPORTED_FORMATS.join(", ")
);
}
let metadata = opt_metadata(args)?;
if metadata.is_some() && format != "pdf" {
anyhow::bail!(
"usage: a {format} file carries no PDF description — hint: pass \"metadata\" only \
for format \"pdf\""
);
}
let mut request = json!({ "op": "create", "format": format });
set_metadata(&mut request, metadata);
for (key, what) in [
("header", "a running header"),
("footer", "a running footer"),
] {
let Some(text) = super::get_opt_str_strict(args, key)? else {
continue;
};
if format != "docx" {
anyhow::bail!(
"usage: \"{key}\" is {what}, and only a .docx has one — hint: leave it out \
for format \"{format}\""
);
}
require_visible(
&format!("\"{key}\""),
text,
&format!("give its text, or leave \"{key}\" out"),
)?;
request[key] = json!(text);
}
let size = super::get_opt_str_strict(args, "size")?;
if let Some(size) = size {
if format != "pptx" {
anyhow::bail!(
"usage: \"size\" is a slide size, and only format \"pptx\" has slides — \
hint: pass \"size\" only for format \"pptx\""
);
}
let sizes = pptx_size_words();
if !sizes.contains(&size) {
anyhow::bail!(
"usage: unknown slide size \"{size}\" — hint: use {}",
listed(&sizes)
);
}
request["size"] = json!(size);
}
if format == "xlsx" {
if args.get("content").is_some_and(|value| !value.is_null()) {
anyhow::bail!(
"usage: \"content\" is not used for format \"xlsx\" — hint: pass \"sheets\" to \
write a spreadsheet"
);
}
request["sheets"] = resolve_sheets(args)?;
} else {
if args.get("sheets").is_some_and(|value| !value.is_null()) {
anyhow::bail!(
"usage: \"sheets\" is not used for format \"{format}\" — hint: pass \"content\" \
to write a {format} document"
);
}
request["content"] = resolve_content(ws, args, format, self.access).await?;
}
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&format!("{}_{}", default_base(format), crate::util::unix_millis()),
);
let outputs = reserve(output, std::slice::from_ref(&base), format).await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "created").await
}
async fn fill_template(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (sample, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let values = require_values(args, "placeholder")?;
require_scalar_values(&values)?;
let family = require_sample_family(&sample).await?;
let extension = copy_extension(&sample, family);
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&sample, "filled"),
);
let outputs = reserve(output, std::slice::from_ref(&base), &extension).await?;
let request = json!({
"op": "fill_template",
"template": sample.to_string_lossy(),
"format": family.name(),
"values": values,
"output": outputs[0].to_string_lossy(),
});
self.deliver(request, &outputs, "filled the sample into")
.await
}
async fn edit(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
family: crate::ooxml::Family,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
require_edit_family(&input, family).await?;
let mut edits = validate_edits(family, args)?;
let mut diagram_text = HashMap::new();
resolve_edit_images(ws, &mut edits, self.access).await?;
if family == crate::ooxml::Family::Pptx && edits_search_slide_text(&edits) {
diagram_text = crate::document::read_pptx_diagram_text(&input).await;
}
let extension = copy_extension(&input, family);
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "edited"),
);
let outputs = reserve(output, std::slice::from_ref(&base), &extension).await?;
let mut request = json!({
"op": format!("{}_edit", family.name()),
"input": input.to_string_lossy(),
"edits": edits,
"output": outputs[0].to_string_lossy(),
});
if !diagram_text.is_empty() {
request["diagram_text"] = json!(diagram_text);
}
self.deliver(request, &outputs, "edited").await
}
async fn pdf_merge(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let paths = super::get_str_array(args, "paths")?;
let metadata = opt_metadata(args)?;
if paths.len() < 2 {
anyhow::bail!(
"usage: `paths` needs at least 2 PDFs to merge, got {} — hint: pass two or \
more PDF paths",
paths.len()
);
}
if paths.len() > MAX_MERGE_INPUTS {
anyhow::bail!(
"usage: `paths` holds {} files, more than one merge takes (limit \
{MAX_MERGE_INPUTS}) — hint: merge them in batches",
paths.len()
);
}
let mut inputs = Vec::with_capacity(paths.len());
let mut total = 0u64;
for path in &paths {
let (resolved, size) = resolve_input(ws, path, self.access).await?;
total += size;
inputs.push(resolved);
}
if total > MAX_MERGE_BYTES {
anyhow::bail!(
"usage: `paths` totals {} MB, more than one merge takes (limit {} MB) — hint: \
merge them in batches",
megabytes(total),
megabytes(MAX_MERGE_BYTES)
);
}
for input in &inputs {
require_pdf(input).await?;
}
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&inputs[0], "merged"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
let mut request = json!({
"op": "pdf_merge",
"inputs": path_strings(&inputs),
"output": outputs[0].to_string_lossy(),
});
set_metadata(&mut request, metadata);
self.deliver(request, &outputs, "merged the files into")
.await
}
async fn pdf_split(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let groups = split_groups(args)?;
let metadata = opt_metadata(args)?;
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
crate::util::name_stem(&file_name_of(&input)),
);
let bases: Vec<String> = (0..groups.len())
.map(|index| format!("{base}_part{}", index + 1))
.collect();
let outputs = reserve(output, &bases, "pdf").await?;
let mut request = json!({
"op": "pdf_split",
"input": input.to_string_lossy(),
"groups": groups,
"outputs": path_strings(&outputs),
});
set_metadata(&mut request, metadata);
self.deliver(request, &outputs, "split the file into").await
}
async fn pdf_rotate(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let degrees = match super::get_opt_i64(args, "degrees")? {
Some(degrees) if degrees % RULES.degrees_step == 0 && degrees.rem_euclid(360) != 0 => {
degrees.rem_euclid(360)
}
Some(degrees) if degrees % RULES.degrees_step == 0 => anyhow::bail!(
"usage: {degrees} degrees would leave the pages as they are — hint: use a \
multiple of {} below a full turn",
RULES.degrees_step
),
Some(_) => anyhow::bail!(
"usage: \"degrees\" must be a multiple of {}",
RULES.degrees_step
),
None => anyhow::bail!(
"usage: missing required argument \"degrees\" — hint: pass a multiple of {} below a \
full turn",
RULES.degrees_step
),
};
let mut request = json!({
"op": "pdf_rotate",
"input": input.to_string_lossy(),
"degrees": degrees,
});
request["pages"] = opt_pages(args)?.kit_value();
set_metadata(&mut request, opt_metadata(args)?);
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "rotated"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "rotated").await
}
async fn pdf_crop(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let area = require_page_area(args)?;
let mut request = json!({
"op": "pdf_crop",
"input": input.to_string_lossy(),
"area": area,
});
request["pages"] = opt_pages(args)?.kit_value();
set_metadata(&mut request, opt_metadata(args)?);
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "cropped"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "cropped").await
}
async fn pdf_resize(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let mut request = json!({
"op": "pdf_resize",
"input": input.to_string_lossy(),
});
set_positive_number(&mut request, args, "width", &RULES.dimension_points)?;
set_positive_number(&mut request, args, "height", &RULES.dimension_points)?;
if let Some(missing) = ["width", "height"]
.into_iter()
.find(|side| request.get(*side).is_none())
{
anyhow::bail!(
"usage: missing required argument \"{missing}\" — a resize names both sides — \
hint: give both \"width\" and \"height\" in points"
);
}
request["pages"] = opt_pages(args)?.kit_value();
set_metadata(&mut request, opt_metadata(args)?);
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "resized"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "resized").await
}
async fn pdf_text(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let text = super::get_str(args, "text")?;
let mut request = json!({
"op": "pdf_text",
"input": input.to_string_lossy(),
"text": text,
});
request["pages"] = opt_pages(args)?.kit_value();
set_number(&mut request, args, "x")?;
set_number(&mut request, args, "y")?;
set_positive_number(&mut request, args, "size", &RULES.dimension_points)?;
set_bounded_number(
&mut request,
args,
"opacity",
&RULES.pdf_opacity,
"give a fraction just above zero",
)?;
set_number(&mut request, args, "rotate")?;
if let Some(color) = super::get_opt_str_strict(args, "color")? {
require_hex_color("\"color\"", color)?;
request["color"] = json!(color);
}
if let Some(stamp) = super::get_opt_bool(args, "stamp")? {
request["stamp"] = json!(stamp);
}
set_metadata(&mut request, opt_metadata(args)?);
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "text"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "stamped text into").await
}
async fn pdf_image(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let (image, _) = resolve_input(ws, super::get_str(args, "image")?, self.access).await?;
ensure_picture(&image, &pdf_pictures())?;
let mut request = json!({
"op": "pdf_image",
"input": input.to_string_lossy(),
"image": image.to_string_lossy(),
});
request["pages"] = opt_pages(args)?.kit_value();
set_number(&mut request, args, "x")?;
set_number(&mut request, args, "y")?;
set_positive_number(&mut request, args, "width", &RULES.dimension_points)?;
set_positive_number(&mut request, args, "height", &RULES.dimension_points)?;
set_metadata(&mut request, opt_metadata(args)?);
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "image"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
request["output"] = json!(outputs[0].to_string_lossy());
self.deliver(request, &outputs, "inserted the image into")
.await
}
async fn pdf_form_fill(
&self,
ws: &Workspace,
args: &Value,
output: &Path,
) -> Result<super::ToolAnswer> {
let (input, _) = resolve_input(ws, super::get_str(args, "path")?, self.access).await?;
let values = require_values(args, "form field")?;
require_scalar_values(&values)?;
let flatten = super::get_bool(args, "flatten", false)?;
let metadata = opt_metadata(args)?;
require_pdf(&input).await?;
let base = sanitize_base(
super::get_opt_str_strict(args, "file_name")?,
&input_base(&input, "filled"),
);
let outputs = reserve(output, std::slice::from_ref(&base), "pdf").await?;
let mut request = json!({
"op": "pdf_form_fill",
"input": input.to_string_lossy(),
"values": values,
"flatten": flatten,
"output": outputs[0].to_string_lossy(),
});
set_metadata(&mut request, metadata);
self.deliver(request, &outputs, "filled the form into")
.await
}
async fn deliver(
&self,
request: Value,
outputs: &[PathBuf],
prefix: &str,
) -> Result<super::ToolAnswer> {
if let Err(e) = docgen::probe().await {
remove_files(outputs).await;
return Err(e);
}
let outcome = match docgen::run(request).await {
Ok(outcome) => outcome,
Err(e) => {
remove_files(outputs).await;
return Err(e);
}
};
let mut sizes = Vec::with_capacity(outputs.len());
for path in outputs {
if !outcome
.outputs
.iter()
.any(|reported| reported.as_path() == path.as_path())
{
remove_files(outputs).await;
return Err(super::internal_fault(&format!(
"the document kit did not report writing {}",
path.display()
)));
}
match tokio::fs::metadata(path).await {
Ok(meta) if meta.len() > 0 => sizes.push(meta.len()),
_ => {
remove_files(outputs).await;
return Err(super::internal_fault(&format!(
"the document kit reported success but wrote nothing to {}",
path.display()
)));
}
}
}
let names: Vec<String> = outputs.iter().map(|path| file_name_of(path)).collect();
let mut text = describe(prefix, &names, &sizes);
if !outcome.missing.is_empty() {
text = super::with_note(
&text,
&format!(
"[no values for these placeholders, left unchanged: {}]",
outcome.missing.join(", ")
),
);
}
if !outcome.unsupported.is_empty() {
let listed = outcome
.unsupported
.iter()
.take(UNSUPPORTED_NOTE_MAX)
.map(|character| format!("'{}'", character.escape_debug()))
.collect::<Vec<_>>()
.join(" ");
let more = outcome
.unsupported
.len()
.saturating_sub(UNSUPPORTED_NOTE_MAX);
let characters = if more == 0 {
listed
} else {
format!("{listed} and {more} more")
};
text = super::with_note(
&text,
&format!("[missing from the file, the embedded font cannot draw: {characters}]"),
);
}
if outcome.placeholders == Some(0) {
text = super::with_note(
&text,
"[no {name} placeholders in the parts this operation rewrites — nothing was \
substituted; the copy is unchanged]",
);
}
for note in &outcome.notes {
text = super::with_note(&text, note);
}
for path in outputs {
text.push('\n');
text.push_str(&self.format_media_result(path));
}
let partial = outcome.lossy
|| !outcome.missing.is_empty()
|| !outcome.unsupported.is_empty()
|| outcome.placeholders == Some(0);
Ok(if partial {
super::ToolAnswer::partial(text)
} else {
super::ToolAnswer::done(text)
})
}
}
async fn resolve_input(ws: &Workspace, path: &str, access: PathAccess) -> Result<(PathBuf, u64)> {
let resolved = super::path::resolve_read_target(ws.as_path(), path, access).await?;
let meta = tokio::fs::metadata(&resolved)
.await
.with_context(|| format!("cannot read {}", resolved.display()))?;
if !meta.is_file() {
anyhow::bail!(
"usage: {} is not a file — hint: pass the path of a file",
resolved.display()
);
}
if meta.len() > crate::util::FILE_MAX_BYTES {
anyhow::bail!(
"usage: {} is over the {} MB limit for a document operation, got {} — hint: shrink \
the file and retry",
resolved.display(),
megabytes(crate::util::FILE_MAX_BYTES),
super::listing::human_readable_size(meta.len())
);
}
Ok((resolved, meta.len()))
}
async fn head_of(path: &Path) -> Result<[u8; 8]> {
use tokio::io::AsyncReadExt as _;
let mut magic = [0u8; 8];
let mut file = tokio::fs::File::open(path)
.await
.with_context(|| format!("cannot read {}", path.display()))?;
let _ = file.read(&mut magic).await;
Ok(magic)
}
async fn real_family_of(path: &Path) -> Result<crate::document::RealFamily> {
let head = head_of(path).await?;
if !crate::document::is_zip_container(&head) && !head.starts_with(crate::document::CFB_MAGIC) {
return Ok(crate::document::real_family(&head));
}
let owned = path.to_owned();
match crate::document::run_document_parse(move || container_family(&owned)).await {
Some(checked) => checked,
None => anyhow::bail!(
"usage: {} could not be read — hint: retry it, or check the file is intact",
file_name_of(path)
),
}
}
fn container_family(path: &Path) -> Result<crate::document::RealFamily> {
use crate::document::RealFamily;
let bytes = std::fs::read(path).with_context(|| format!("cannot read {}", path.display()))?;
let real = crate::document::real_family(&bytes);
let real = match real {
RealFamily::Other if bytes.starts_with(crate::document::CFB_MAGIC) => {
crate::legacy::named_family(path).map_or(real, crate::legacy::Family::real)
}
other => other,
};
if let Some(RealFormat::Legacy(family)) = RealFormat::of(real)
&& crate::legacy::is_encrypted(&bytes, family)
{
return Ok(RealFamily::Encrypted);
}
Ok(real)
}
fn password_refusal(path: &Path, wanted: Wanted) -> anyhow::Error {
anyhow::anyhow!(
"usage: {} is password-protected — hint: remove the protection, save an unprotected copy \
and {}",
file_name_of(path),
wanted.unprotected()
)
}
fn real_clause(real: Option<RealFormat>, path: &Path) -> String {
if let Some(format) = real {
return format!("which is really {}", format.phrase());
}
crate::ooxml::family_of(path).map_or_else(
|| "which is not a Word, Excel, PowerPoint or PDF document".to_owned(),
|family| format!("which is {}", crate::ooxml::corrupt_reason(family)),
)
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum RealFormat {
Pdf,
Package(crate::ooxml::Family),
Legacy(crate::legacy::Family),
}
impl RealFormat {
fn of(real: crate::document::RealFamily) -> Option<Self> {
use crate::document::RealFamily;
match real {
RealFamily::Pdf => Some(Self::Pdf),
RealFamily::Docx => Some(Self::Package(crate::ooxml::Family::Docx)),
RealFamily::Xlsx => Some(Self::Package(crate::ooxml::Family::Xlsx)),
RealFamily::Pptx => Some(Self::Package(crate::ooxml::Family::Pptx)),
RealFamily::LegacyDoc => Some(Self::Legacy(crate::legacy::Family::Doc)),
RealFamily::LegacyXls => Some(Self::Legacy(crate::legacy::Family::Xls)),
RealFamily::LegacyPpt => Some(Self::Legacy(crate::legacy::Family::Ppt)),
RealFamily::Encrypted | RealFamily::Other => None,
}
}
fn phrase(self) -> &'static str {
match self {
Self::Pdf => "a PDF",
Self::Package(crate::ooxml::Family::Docx) => "a .docx package",
Self::Package(crate::ooxml::Family::Xlsx) => "an .xlsx package",
Self::Package(crate::ooxml::Family::Pptx) => "a .pptx package",
Self::Legacy(crate::legacy::Family::Doc) => "an old .doc file",
Self::Legacy(crate::legacy::Family::Xls) => "an old .xls file",
Self::Legacy(crate::legacy::Family::Ppt) => "an old .ppt file",
}
}
}
#[derive(Clone, Copy)]
enum Wanted {
Package,
Sample,
Pdf,
}
impl Wanted {
const fn verb(self) -> &'static str {
match self {
Self::Package => "edited",
Self::Sample => "filled",
Self::Pdf => "written",
}
}
const fn convert(self) -> &'static str {
match self {
Self::Pdf => "save it as a PDF first",
Self::Package | Self::Sample => "save it as one of the OOXML families first",
}
}
const fn pdf(self) -> &'static str {
match self {
Self::Sample => "a PDF's form fields are filled with pdf_form_fill",
Self::Package | Self::Pdf => {
"a PDF's own actions are pdf_merge, pdf_split, pdf_rotate, pdf_crop, pdf_resize, \
pdf_text, pdf_image and pdf_form_fill"
}
}
}
const fn unprotected(self) -> &'static str {
match self {
Self::Package => "edit that",
Self::Sample => "fill that",
Self::Pdf => "convert it to a PDF",
}
}
}
fn wrong_family_advice(real: Option<RealFormat>, takes: &str, wanted: Wanted) -> String {
match real {
Some(RealFormat::Package(family)) => match wanted {
Wanted::Package => format!("use {}_edit to edit it", family.name()),
Wanted::Sample => "rename it to match its content".to_owned(),
Wanted::Pdf => "convert it to a PDF first".to_owned(),
},
Some(RealFormat::Legacy(_)) => old_format_advice(wanted),
Some(RealFormat::Pdf) => wanted.pdf().to_owned(),
None => format!("pass {takes}"),
}
}
fn old_format_advice(wanted: Wanted) -> String {
format!(
"an old .doc/.xls/.ppt file is read but never {}; {}",
wanted.verb(),
wanted.convert()
)
}
fn wrong_family(
what: &str,
takes: &str,
path: &Path,
real: crate::document::RealFamily,
wanted: Wanted,
) -> anyhow::Error {
use crate::document::RealFamily;
if real == RealFamily::Encrypted {
return password_refusal(path, wanted);
}
let format = RealFormat::of(real);
anyhow::anyhow!(
"usage: {what}, got {}, {} — hint: {}",
file_name_of(path),
real_clause(format, path),
wrong_family_advice(format, takes, wanted)
)
}
async fn require_edit_family(path: &Path, family: crate::ooxml::Family) -> Result<()> {
let real = real_family_of(path).await?;
if RealFormat::of(real) == Some(RealFormat::Package(family)) {
return Ok(());
}
let accepted = match family {
crate::ooxml::Family::Docx => "a .docx/.docm file",
crate::ooxml::Family::Xlsx => "a .xlsx/.xlsm file",
crate::ooxml::Family::Pptx => "a .pptx/.pptm file",
};
Err(wrong_family(
&format!("{}_edit edits only {accepted}", family.name()),
accepted,
path,
real,
Wanted::Package,
))
}
async fn require_sample_family(sample: &Path) -> Result<crate::ooxml::Family> {
let real = real_family_of(sample).await?;
let format = RealFormat::of(real);
if let Some(RealFormat::Package(family)) = format
&& crate::ooxml::family_of(sample) == Some(family)
{
return Ok(family);
}
let accepted = "a .docx/.docm, .pptx/.pptm or .xlsx/.xlsm sample";
let what = if matches!(format, Some(RealFormat::Package(_))) {
"the sample's name must be the family its bytes are".to_owned()
} else {
format!("a sample must be {accepted}")
};
Err(wrong_family(&what, accepted, sample, real, Wanted::Sample))
}
async fn require_pdf(path: &Path) -> Result<()> {
let real = real_family_of(path).await?;
if real == crate::document::RealFamily::Pdf {
return Ok(());
}
Err(wrong_family(
"the input must be a PDF",
"a PDF",
path,
real,
Wanted::Pdf,
))
}
fn validate_edits(family: crate::ooxml::Family, args: &Value) -> Result<Value> {
let names = edit_op_fields(family)
.iter()
.map(|(op, _)| *op)
.collect::<Vec<_>>();
let edits = match args.get("edits") {
Some(Value::Array(edits)) if !edits.is_empty() => edits,
Some(Value::Array(_)) => {
anyhow::bail!("usage: \"edits\" is empty — hint: name at least one edit")
}
Some(v) => return Err(super::wrong_type("edits", "an array", v)),
None => anyhow::bail!(
"usage: missing required argument \"edits\" — hint: pass a JSON array of edit objects, \
e.g. [{{\"op\": \"replace_text\", \"find\": \"…\", \"replace\": \"…\"}}]"
),
};
if edits.len() > RULES.edits_max {
anyhow::bail!(
"usage: {} edits is more than one call takes (limit {}) — hint: split them across \
calls",
edits.len(),
RULES.edits_max
);
}
let mut out = Vec::with_capacity(edits.len());
for (index, edit) in edits.iter().enumerate() {
let at = format!("edits[{index}]");
let Some(object) = edit.as_object() else {
anyhow::bail!("usage: {at} must be an object — hint: give every edit an \"op\"");
};
let Some(op) = object.get("op").and_then(Value::as_str) else {
anyhow::bail!("usage: {at} has no \"op\" — hint: use {}", names.join(", "));
};
if !names.contains(&op) {
anyhow::bail!(
"usage: unknown {} edit \"{op}\" — hint: use {}",
family.name(),
names.join(", ")
);
}
out.push(validate_edit(family, edit, object, op, &at)?);
}
Ok(Value::Array(out))
}
fn validate_edit(
family: crate::ooxml::Family,
edit: &Value,
object: &serde_json::Map<String, Value>,
op: &str,
at: &str,
) -> Result<Value> {
require_taken_keys(
object,
at,
&format!("{} \"{op}\"", family.name()),
op_fields(family, op),
&["op"],
)?;
let validated = match family {
crate::ooxml::Family::Docx => {
validate_docx_edit(object, op, at)?;
edit.clone()
}
crate::ooxml::Family::Xlsx => validate_xlsx_edit(edit, object, op, at)?,
crate::ooxml::Family::Pptx => {
validate_pptx_edit(object, op, at)?;
edit.clone()
}
};
Ok(without_nulls(validated))
}
fn without_nulls(edit: Value) -> Value {
match edit {
Value::Object(object) => Value::Object(
object
.into_iter()
.filter(|(_, value)| !value.is_null())
.map(|(key, value)| (key, without_nulls(value)))
.collect(),
),
other => other,
}
}
fn edit_string<'a>(
object: &'a serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<Option<&'a str>> {
match optional_string(object.get(key), at, key)? {
Some(text) => {
let length = text.chars().count();
if length > RULES.edit_text_max {
anyhow::bail!(
"usage: {at}.{key} must be at most {} characters, got {length} — hint: shorten \
the text",
RULES.edit_text_max
);
}
Ok(Some(text))
}
None => Ok(None),
}
}
fn edit_string_present<'a>(
object: &'a serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<&'a str> {
match edit_string(object, key, at)? {
Some(text) => Ok(text),
None => {
anyhow::bail!("usage: {at} has no \"{key}\" — hint: give the edit its \"{key}\" text")
}
}
}
fn edit_string_non_empty<'a>(
object: &'a serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<&'a str> {
match edit_string(object, key, at)? {
Some(text) if !text.is_empty() => Ok(text),
_ => anyhow::bail!("usage: {at}.{key} must not be empty"),
}
}
fn require_optional_text(
object: &serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<()> {
match edit_string(object, key, at)? {
Some("") => anyhow::bail!("usage: {at}.{key} must not be empty"),
_ => Ok(()),
}
}
fn edit_integer(
object: &serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<Option<u64>> {
match object.get(key) {
None | Some(Value::Null) => Ok(None),
Some(value) => value.as_u64().map(Some).ok_or_else(|| {
super::wrong_type(&format!("{at}.{key}"), "a non-negative integer", value)
}),
}
}
fn edit_number(
object: &serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<Option<f64>> {
match object.get(key) {
None | Some(Value::Null) => Ok(None),
Some(value) => value
.as_f64()
.filter(|number| number.is_finite())
.map(Some)
.ok_or_else(|| super::wrong_type(&format!("{at}.{key}"), "a number", value)),
}
}
fn validate_docx_edit(object: &serde_json::Map<String, Value>, op: &str, at: &str) -> Result<()> {
match op {
"replace_text" => {
edit_string_non_empty(object, "find", at)?;
edit_string_present(object, "replace", at)?;
}
"insert_text" => {
edit_string_non_empty(object, "find", at)?;
edit_string_non_empty(object, "insert", at)?;
require_position(object, at)?;
}
"remove_text" | "remove_paragraph" => {
edit_string_non_empty(object, "find", at)?;
}
"format_text" => {
edit_string_non_empty(object, "find", at)?;
require_format(object, at)?;
}
"format_paragraph" => {
edit_string_non_empty(object, "find", at)?;
require_paragraph_format(object, at)?;
}
"add_paragraph" => {
edit_string_present(object, "text", at)?;
require_optional_text(object, "after", at)?;
}
"add_image" => {
edit_string_non_empty(object, "after", at)?;
edit_string_non_empty(object, "image", at)?;
require_span_number(
object,
"width",
&RULES.dimension_points,
"a picture's width in points",
at,
)?;
require_span_number(
object,
"height",
&RULES.dimension_points,
"a picture's height in points",
at,
)?;
}
"add_table" => {
edit_string_non_empty(object, "after", at)?;
require_table(object, at, Some(RULES.table_columns_max))?;
}
_ => unreachable!("the op was checked against the family's vocabulary"),
}
Ok(())
}
fn require_position(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
match optional_string(object.get("position"), at, "position")? {
None | Some("after" | "before") => Ok(()),
Some(position) => anyhow::bail!(
"usage: {at}.position must be \"after\" or \"before\", got \"{position}\""
),
}
}
fn require_toggles(
object: &serde_json::Map<String, Value>,
keys: &[&str],
at: &str,
) -> Result<bool> {
let mut any = false;
for key in keys {
match object.get(*key) {
None | Some(Value::Null) => {}
Some(Value::Bool(_)) => any = true,
Some(v) => return Err(super::wrong_type(&format!("{at}.{key}"), "a boolean", v)),
}
}
Ok(any)
}
fn require_span_number(
object: &serde_json::Map<String, Value>,
key: &str,
span: &Span,
what: &str,
at: &str,
) -> Result<()> {
match edit_number(object, key, at)? {
Some(number) if span.contains(number) => Ok(()),
Some(number) => anyhow::bail!(
"usage: {at}.{key} must be {}, got {number} — hint: {what}",
span.bounds()
),
None => Ok(()),
}
}
fn require_stated_number(
object: &serde_json::Map<String, Value>,
key: &str,
span: &Span,
what: &str,
at: &str,
) -> Result<bool> {
require_span_number(object, key, span, what, at)?;
Ok(stated(object, key))
}
fn require_format(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let mut any = require_toggles(object, &["bold", "italic", "underline", "strike"], at)?;
any |= require_stated_number(
object,
"size",
&RULES.text_size_points,
"give a size in points",
at,
)?;
any |= require_color(object, at)?;
if !any {
anyhow::bail!(
"usage: {at} (format_text) needs at least one of bold, italic, underline, strike, \
size or color — hint: a boolean toggles bold, italic, underline or strike, size is \
in points and color is {} hex digits",
RULES.color_digits
);
}
Ok(())
}
fn require_paragraph_format(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let indent = "give an indent in points";
let spacing = "give spacing in points";
let mut any = require_alignment(
object,
"align",
&pair_words(&RULES.paragraph_alignments),
at,
)?;
any |= require_name(object, "style", RULES.paragraph_style_max, at)?;
for (key, span, what) in [
("indent_left", &RULES.paragraph_indent_points, indent),
("indent_right", &RULES.paragraph_indent_points, indent),
("indent_first_line", &RULES.paragraph_indent_points, indent),
("spacing_before", &RULES.paragraph_spacing_points, spacing),
("spacing_after", &RULES.paragraph_spacing_points, spacing),
(
"line_spacing",
&RULES.paragraph_line_points,
"give a line height in points",
),
] {
any |= require_stated_number(object, key, span, what, at)?;
}
any |= require_list(object, at)?;
require_list_level(object, at)?;
if !any {
anyhow::bail!(
"usage: {at} (format_paragraph) needs at least one of align, style, indent_left, \
indent_right, indent_first_line, spacing_before, spacing_after, line_spacing or \
list — hint: name the property the paragraph is to take"
);
}
Ok(())
}
fn require_name(
object: &serde_json::Map<String, Value>,
key: &str,
max: usize,
at: &str,
) -> Result<bool> {
match edit_string(object, key, at)? {
None => Ok(false),
Some("") => anyhow::bail!("usage: {at}.{key} must not be empty"),
Some(name) if name.chars().count() <= max => Ok(true),
Some(name) => anyhow::bail!(
"usage: {at}.{key} must be at most {max} characters, got {}",
name.chars().count()
),
}
}
fn require_list(object: &serde_json::Map<String, Value>, at: &str) -> Result<bool> {
let kinds = pair_words(&RULES.paragraph_lists);
match object.get("list") {
None | Some(Value::Null) => Ok(false),
Some(Value::Bool(false)) => Ok(true),
Some(Value::String(kind)) if kinds.contains(&kind.as_str()) => Ok(true),
Some(Value::String(kind)) => anyhow::bail!(
"usage: {at}.list must be {}, or false to take the paragraph's list away, got \
\"{kind}\"",
listed(&kinds)
),
Some(value) => Err(super::wrong_type(
&format!("{at}.list"),
"a list kind or false",
value,
)),
}
}
fn require_list_level(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let Some(level) = edit_integer(object, "level", at)? else {
return Ok(());
};
if !matches!(object.get("list"), Some(Value::String(_))) {
anyhow::bail!(
"usage: {at}.level needs a list to belong to — hint: name \"list\" as {} beside it",
listed(&pair_words(&RULES.paragraph_lists))
);
}
if level > u64::from(RULES.paragraph_level_max) {
anyhow::bail!(
"usage: {at}.level must be a whole number from 0 to {}, got {level}",
RULES.paragraph_level_max
);
}
Ok(())
}
#[derive(Clone, Copy)]
enum FormatCellsTarget {
Range,
Column,
Row,
}
impl FormatCellsTarget {
fn key(self) -> &'static str {
match self {
Self::Range => "range",
Self::Column => "column",
Self::Row => "row",
}
}
fn attributes(self) -> &'static [&'static str] {
match self {
Self::Range => &[
"font",
"size",
"bold",
"italic",
"underline",
"strike",
"color",
"fill",
"border",
"align",
"vertical",
"wrap",
"number_format",
],
Self::Column => &["width_chars"],
Self::Row => &["height_points"],
}
}
fn keys(self) -> Vec<&'static str> {
let mut keys = vec!["sheet", self.key()];
keys.extend_from_slice(self.attributes());
keys
}
}
fn stated(object: &serde_json::Map<String, Value>, key: &str) -> bool {
!matches!(object.get(key), None | Some(Value::Null))
}
fn require_format_cells_target(
object: &serde_json::Map<String, Value>,
at: &str,
) -> Result<FormatCellsTarget> {
let named = ["range", "column", "row"]
.into_iter()
.filter(|key| stated(object, key))
.collect::<Vec<_>>();
match named.as_slice() {
[] => anyhow::bail!(
"usage: {at} names no target — hint: name \"range\" (a cell or a range of cells), \
\"column\" (a column's letters, for its width) or \"row\" (a 1-based row, for its \
height)"
),
["range"] => Ok(FormatCellsTarget::Range),
["column"] => Ok(FormatCellsTarget::Column),
["row"] => Ok(FormatCellsTarget::Row),
both => {
let listed = both
.iter()
.map(|key| format!("\"{key}\""))
.collect::<Vec<_>>()
.join(" and ");
anyhow::bail!(
"usage: {at} names more than one target ({listed}) — hint: an edit formats one of \
\"range\", \"column\" or \"row\""
)
}
}
}
fn range_corners(range: &str, what: &str) -> Result<((u32, u32), (u32, u32))> {
let mut corners = range.split(':');
let first = corners.next().unwrap_or_default();
let second = corners.next();
if corners.next().is_some() {
anyhow::bail!(
"usage: {what} must be a cell or a rectangular range like B2:D2, got \"{range}\""
);
}
let first = check_cell_address(first, what)?;
let second = match second {
Some(second) => check_cell_address(second, what)?,
None => first,
};
Ok((first, second))
}
fn require_format_cells_range(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let field = format!("{at}.range");
let range = edit_string_non_empty(object, "range", at)?;
let ((first_row, first_column), (last_row, last_column)) = range_corners(range, &field)?;
let rows = u64::from(last_row.abs_diff(first_row)) + 1;
let columns = u64::from(last_column.abs_diff(first_column)) + 1;
let cells = rows * columns;
if cells > RULES.format_cells_max as u64 {
anyhow::bail!(
"usage: {field} \"{range}\" holds {cells} cells, more than one edit formats ({}) — \
hint: name the rows and columns you mean",
RULES.format_cells_max
);
}
Ok(())
}
fn require_merge_text(range: &str, what: &str) -> Result<()> {
let ((first_row, first_column), (last_row, last_column)) = range_corners(range, what)?;
let cells = (u64::from(last_row.abs_diff(first_row)) + 1)
* (u64::from(last_column.abs_diff(first_column)) + 1);
if cells < 2 {
anyhow::bail!(
"usage: {what} \"{range}\" covers one cell, and a merge needs at least two — hint: name \
a range like A1:B2"
);
}
Ok(())
}
fn require_merge_range(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let range = edit_string_non_empty(object, "range", at)?;
require_merge_text(range, &format!("{at}.range"))
}
fn require_color(object: &serde_json::Map<String, Value>, at: &str) -> Result<bool> {
match optional_string(object.get("color"), at, "color")? {
None => Ok(false),
Some(color) => {
require_hex_color(&format!("{at}.color"), color)?;
Ok(true)
}
}
}
fn require_fill(object: &serde_json::Map<String, Value>, at: &str) -> Result<bool> {
match object.get("fill") {
None | Some(Value::Null) => Ok(false),
Some(Value::Bool(false)) => Ok(true),
Some(Value::Bool(true)) => anyhow::bail!(
"usage: {at}.fill must be a colour or false — hint: false takes the cell's fill away, \
and a fill cannot be named without its colour"
),
Some(Value::String(color)) => {
require_hex_color(&format!("{at}.fill"), color).map_err(|color_error| {
anyhow::anyhow!("{color_error} — or pass false to take the cell's fill away")
})?;
Ok(true)
}
Some(value) => Err(super::wrong_type(
&format!("{at}.fill"),
"a colour or false",
value,
)),
}
}
fn require_border(object: &serde_json::Map<String, Value>, at: &str) -> Result<bool> {
let border = match object.get("border") {
None | Some(Value::Null) => return Ok(false),
Some(Value::Object(border)) => border,
Some(value) => {
return Err(super::wrong_type(
&format!("{at}.border"),
"an object of sides",
value,
));
}
};
let stated = border
.iter()
.filter(|(_, value)| !value.is_null())
.collect::<Vec<_>>();
if stated.is_empty() {
anyhow::bail!(
"usage: {at}.border names no side — hint: it takes {}",
listed(&RULES.cell_border_sides)
);
}
for (side, value) in stated {
if !RULES.cell_border_sides.contains(side) {
anyhow::bail!(
"usage: {at}.border.{side} is not a side — hint: it takes {}",
listed(&RULES.cell_border_sides)
);
}
if !matches!(value, Value::Bool(_)) {
return Err(super::wrong_type(
&format!("{at}.border.{side}"),
"a boolean",
value,
));
}
}
Ok(true)
}
fn require_alignment<W: AsRef<str> + std::fmt::Display>(
object: &serde_json::Map<String, Value>,
key: &str,
values: &[W],
at: &str,
) -> Result<bool> {
match optional_string(object.get(key), at, key)? {
None => Ok(false),
Some(value) if values.iter().any(|allowed| allowed.as_ref() == value) => Ok(true),
Some(value) => anyhow::bail!(
"usage: {at}.{key} must be {}, got \"{value}\"",
listed(values)
),
}
}
fn require_taken_keys(
object: &serde_json::Map<String, Value>,
at: &str,
subject: &str,
fields: &[&str],
always: &[&str],
) -> Result<()> {
for key in object.keys() {
if !always.contains(&key.as_str()) && !fields.contains(&key.as_str()) && stated(object, key)
{
let taken = always.iter().chain(fields).copied().collect::<Vec<_>>();
anyhow::bail!(
"usage: {at}.{key} is not a field of {subject} — hint: it takes {}",
listed(&taken)
);
}
}
Ok(())
}
fn require_block_keys(object: &serde_json::Map<String, Value>, at: &str, kind: &str) -> Result<()> {
let fields = CONTENT_BLOCKS
.iter()
.find_map(|(name, fields, _)| (*name == kind).then_some(*fields))
.expect("every block kind the boundary reads is one the declaration names");
require_taken_keys(object, at, &format!("the {kind} block"), fields, &["type"])
}
fn require_docx_block(format: &str, at: &str, kind: &str) -> Result<()> {
if format == "docx" {
return Ok(());
}
anyhow::bail!(
"usage: {at} ({kind}) is content only a .docx holds — hint: leave it out of a {format} \
create"
)
}
fn link_address(raw: &str, index: usize) -> Result<String> {
let Ok(url) = url::Url::parse(raw) else {
anyhow::bail!(
"usage: content[{index}].url must be an absolute URL, e.g. https://example.com — \
got: {raw:?}"
);
};
if !matches!(url.scheme(), "http" | "https" | "mailto" | "tel") {
anyhow::bail!(
"usage: content[{index}].url must be an http, https, mailto or tel address — got: \
{raw:?}"
);
}
Ok(url.to_string())
}
#[expect(clippy::too_many_lines)] fn validate_xlsx_edit(
edit: &Value,
object: &serde_json::Map<String, Value>,
op: &str,
at: &str,
) -> Result<Value> {
if op != "add_sheet" {
require_sheet_field(object, at)?;
}
match op {
"set_cell" => {
require_cell(object, at)?;
require_name(object, "number_format", RULES.number_format_max, at)?;
let mut normalized = edit.clone();
normalized["value"] = require_cell_value(object, at)?;
return Ok(normalized);
}
"clear_cell" => {
require_cell(object, at)?;
}
"format_cells" => {
let target = require_format_cells_target(object, at)?;
require_taken_keys(
object,
at,
&format!("a {} target", target.key()),
&target.keys(),
&["op"],
)?;
let named = match target {
FormatCellsTarget::Range => {
require_format_cells_range(object, at)?;
let mut named = require_toggles(
object,
&["bold", "italic", "underline", "strike", "wrap"],
at,
)?;
named |= require_name(object, "font", RULES.font_name_max, at)?;
named |= require_stated_number(
object,
"size",
&RULES.cell_font_points,
"give a size in points",
at,
)?;
named |= require_color(object, at)?;
named |= require_fill(object, at)?;
named |= require_border(object, at)?;
named |= require_alignment(object, "align", &RULES.cell_alignments, at)?;
named |= require_alignment(object, "vertical", &RULES.cell_verticals, at)?;
if stated(object, "number_format") {
require_name(object, "number_format", RULES.number_format_max, at)?;
named = true;
}
named
}
FormatCellsTarget::Column => {
require_column(object, at)?;
require_stated_number(
object,
"width_chars",
&RULES.column_width_chars,
"give a width in characters, as Excel shows one",
at,
)?
}
FormatCellsTarget::Row => {
require_row(object, at)?;
require_stated_number(
object,
"height_points",
&RULES.row_height_points,
"give a height in points",
at,
)?
}
};
if !named {
anyhow::bail!(
"usage: {at} names no formatting — hint: name at least one of {}, beyond the \
target it applies to",
target.attributes().join(", ")
);
}
}
"insert_row" | "delete_row" => {
require_row(object, at)?;
}
"insert_column" | "delete_column" => {
require_column(object, at)?;
}
"merge_cells" => {
require_merge_range(object, at)?;
}
"unmerge_cells" => {
let field = format!("{at}.range");
range_corners(edit_string_non_empty(object, "range", at)?, &field)?;
}
"add_sheet" | "rename_sheet" => {
require_sheet_name_field(object, at)?;
}
"delete_sheet" => {}
"move_sheet" => {
require_one_based_number(object, "to", at)?;
}
"add_image" => {
require_cell(object, at)?;
edit_string_non_empty(object, "image", at)?;
require_span_number(
object,
"width",
&RULES.dimension_points,
"a picture's width in points",
at,
)?;
require_span_number(
object,
"height",
&RULES.dimension_points,
"a picture's height in points",
at,
)?;
}
_ => unreachable!("the op was checked against the family's vocabulary"),
}
Ok(edit.clone())
}
fn require_sheet_field(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let sheet = edit_string_non_empty(object, "sheet", at)?;
require_sheet_name_length(sheet, &format!("{at}.sheet"))
}
fn require_sheet_name_field(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let name = edit_string_non_empty(object, "name", at)?;
require_sheet_name_text(name, &format!("{at}.name"))
}
fn column_number(letters: &str) -> Option<u32> {
if !((1..=3).contains(&letters.len()) && letters.bytes().all(|byte| byte.is_ascii_uppercase()))
{
return None;
}
crate::reader_output::column_index(letters).map(|index| index + 1)
}
fn cell_address(cell: &str) -> Option<(u32, u32)> {
let split = cell.find(|c: char| c.is_ascii_digit())?;
let (letters, digits) = cell.split_at(split);
let column = column_number(letters)?;
let digits_ok = digits.len() <= 7
&& digits
.as_bytes()
.first()
.is_some_and(|byte| (b'1'..=b'9').contains(byte))
&& digits.bytes().all(|byte| byte.is_ascii_digit());
if !digits_ok {
return None;
}
Some((digits.parse().ok()?, column))
}
fn check_cell_address(cell: &str, what: &str) -> Result<(u32, u32)> {
let Some((row, column)) = cell_address(cell) else {
anyhow::bail!("usage: {what} must be an A1 address like B7, got \"{cell}\"");
};
if row > RULES.sheet_row_max {
anyhow::bail!(
"usage: {what} \"{cell}\": the row must be between 1 and {}",
RULES.sheet_row_max
);
}
if column > RULES.sheet_column_max {
anyhow::bail!(
"usage: {what} \"{cell}\": the column must be between A and {}",
crate::reader_output::column_letters(RULES.sheet_column_max - 1)
);
}
Ok((row, column))
}
fn require_cell(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let cell = edit_string_non_empty(object, "cell", at)?;
check_cell_address(cell, &format!("{at}.cell")).map(|_| ())
}
fn require_cell_value(object: &serde_json::Map<String, Value>, at: &str) -> Result<Value> {
let Some(value) = object.get("value").filter(|value| !value.is_null()) else {
anyhow::bail!(
"usage: {at} has no \"value\" — hint: pass a string, a number, a boolean or \
{{\"formula\": \"SUM(A1:A2)\"}}, or use the \"format_cells\" op to change only the \
cell's formatting"
);
};
if is_scalar(value) {
return Ok(value.clone());
}
match value {
Value::Object(formula) => normalized_formula(&format!("{at}.value"), formula),
other => Err(super::wrong_type(
&format!("{at}.value"),
SCALAR_TYPE,
other,
)),
}
}
fn require_row(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
match edit_integer(object, "row", at)? {
Some(row) if (1..=u64::from(RULES.sheet_row_max)).contains(&row) => Ok(()),
_ => anyhow::bail!(
"usage: {at}.row must be a whole number between 1 and {} — hint: a 1-based sheet row",
RULES.sheet_row_max
),
}
}
fn require_column(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let column = edit_string_non_empty(object, "column", at)?;
match column_number(column) {
Some(number) if number <= RULES.sheet_column_max => Ok(()),
_ => anyhow::bail!(
"usage: {at}.column must be column letters between A and {}, got \"{column}\"",
crate::reader_output::column_letters(RULES.sheet_column_max - 1)
),
}
}
fn validate_pptx_edit(object: &serde_json::Map<String, Value>, op: &str, at: &str) -> Result<()> {
match op {
"replace_text" | "replace_notes" => {
require_slide(object, at)?;
require_pptx_lookup(edit_string_non_empty(object, "find", at)?, "find", at)?;
edit_string_present(object, "replace", at)?;
}
"remove_text" | "remove_paragraph" | "remove_notes" => {
require_slide(object, at)?;
require_pptx_lookup(edit_string_non_empty(object, "find", at)?, "find", at)?;
}
"format_text" => {
require_slide(object, at)?;
require_pptx_lookup(edit_string_non_empty(object, "find", at)?, "find", at)?;
require_pptx_format(object, at)?;
}
"add_paragraph" => {
require_slide(object, at)?;
edit_string_present(object, "text", at)?;
if let Some(after) = edit_string(object, "after", at)? {
require_pptx_lookup(after, "after", at)?;
}
require_optional_level(object, at)?;
}
"add_image" => {
require_slide(object, at)?;
edit_string_non_empty(object, "image", at)?;
require_slide_geometry(object, at)?;
}
"add_table" => {
require_slide(object, at)?;
require_table(object, at, None)?;
require_slide_geometry(object, at)?;
}
"add_slide" => {
if edit_integer(object, "after", at)? == Some(0) {
anyhow::bail!("usage: {at}.after must be a whole number of at least 1");
}
require_optional_text(object, "title", at)?;
require_bullets(object, at)?;
}
"delete_slide" | "duplicate_slide" => {
require_slide(object, at)?;
}
"move_slide" => {
require_slide(object, at)?;
require_one_based_number(object, "to", at)?;
}
"add_notes" => {
require_slide(object, at)?;
edit_string_present(object, "text", at)?;
}
_ => unreachable!("the op was checked against the family's vocabulary"),
}
Ok(())
}
fn require_pptx_lookup(text: &str, key: &str, at: &str) -> Result<()> {
if text.trim().is_empty() {
anyhow::bail!(
"usage: {at}.{key} must name text — whitespace alone names nothing to search for"
);
}
Ok(())
}
fn require_slide(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
require_one_based_number(object, "slide", at)
}
fn require_one_based_number(
object: &serde_json::Map<String, Value>,
key: &str,
at: &str,
) -> Result<()> {
match edit_integer(object, key, at)? {
Some(number) if number >= 1 => Ok(()),
_ => anyhow::bail!("usage: {at}.{key} must be a whole number of at least 1"),
}
}
fn require_optional_level(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
match edit_integer(object, "level", at)? {
None => Ok(()),
Some(level) if level <= u64::from(RULES.paragraph_level_max) => Ok(()),
Some(level) => {
anyhow::bail!(
"usage: {at}.level must be a whole number from 0 to {}, got {level}",
RULES.paragraph_level_max
)
}
}
}
fn require_bullets(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let bullets = match object.get("bullets") {
None | Some(Value::Null) => return Ok(()),
Some(Value::Array(bullets)) => bullets,
Some(v) => return Err(super::wrong_type(&format!("{at}.bullets"), "an array", v)),
};
if bullets.len() > RULES.bullets_max {
anyhow::bail!(
"usage: {at}.bullets must hold at most {} bullets, got {} — hint: split the slide",
RULES.bullets_max,
bullets.len()
);
}
for (index, bullet) in bullets.iter().enumerate() {
let Some(text) = bullet.as_str() else {
return Err(super::wrong_type(
&format!("{at}.bullets[{index}]"),
"a string",
bullet,
));
};
let length = text.chars().count();
if length > RULES.edit_text_max {
anyhow::bail!(
"usage: {at}.bullets[{index}] must be at most {} characters, got {length}",
RULES.edit_text_max
);
}
}
Ok(())
}
fn pair_words(list: &[(String, String)]) -> Vec<&str> {
list.iter().map(|(word, _)| word.as_str()).collect()
}
fn pptx_size_words() -> Vec<&'static str> {
RULES
.pptx_sizes
.iter()
.map(|size| size.name.as_str())
.collect()
}
fn pptx_size_list() -> String {
RULES
.pptx_sizes
.iter()
.map(|size| format!("{} ({})", size.name, size.deck.inches()))
.collect::<Vec<_>>()
.join(", ")
}
fn spelled_docx_marks(bracketed: bool) -> String {
crate::docgen::docx_marks()
.iter()
.filter(|mark| mark.example.starts_with('[') == bracketed)
.map(|mark| format!("`{}`", mark.example))
.collect::<Vec<_>>()
.join(", ")
}
fn require_pptx_format(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let mut any = require_toggles(object, &["bold", "italic", "underline", "strike"], at)?;
any |= require_stated_number(
object,
"size",
&RULES.slide_text_size_points,
"give a size in points",
at,
)?;
any |= require_color(object, at)?;
any |= require_alignment(object, "align", &pair_words(&RULES.slide_alignments), at)?;
if !any {
anyhow::bail!(
"usage: {at} (format_text) needs at least one of bold, italic, underline, strike, \
size, color or align — hint: a boolean sets bold, italic, underline or strike (false \
switches it off), size is in points, color is hex digits and align is {}",
listed(&pair_words(&RULES.slide_alignments))
);
}
Ok(())
}
fn require_slide_geometry(object: &serde_json::Map<String, Value>, at: &str) -> Result<()> {
let place = "give the fraction of the slide the shape is placed at";
let size = "give the fraction of the slide the shape is scaled to";
for (key, span, what) in [
("x_fraction", &RULES.slide_position_fraction, place),
("y_fraction", &RULES.slide_position_fraction, place),
("width_fraction", &RULES.slide_size_fraction, size),
("height_fraction", &RULES.slide_size_fraction, size),
] {
require_span_number(object, key, span, what, at)?;
}
Ok(())
}
async fn resolve_content(
ws: &Workspace,
args: &Value,
format: &str,
access: PathAccess,
) -> Result<Value> {
let blocks = match args.get("content") {
Some(Value::Array(blocks)) if !blocks.is_empty() => blocks,
Some(Value::Array(_)) => {
anyhow::bail!("usage: \"content\" is empty — hint: pass at least one block")
}
Some(v) => return Err(super::wrong_type("content", "an array", v)),
None => anyhow::bail!(
"usage: missing required argument \"content\" — hint: pass a JSON array of blocks, \
e.g. [{{\"type\": \"paragraph\", \"text\": \"…\"}}]"
),
};
let mut out = Vec::with_capacity(blocks.len());
let kinds = CONTENT_BLOCKS
.iter()
.map(|(kind, _, _)| *kind)
.collect::<Vec<_>>();
let mut contents = false;
for (index, block) in blocks.iter().enumerate() {
let Some(object) = block.as_object() else {
anyhow::bail!(
"usage: content[{index}] must be an object — hint: give every block a \"type\""
);
};
let Some(kind) = object.get("type").and_then(Value::as_str) else {
anyhow::bail!(
"usage: content[{index}] has no \"type\" — hint: use {}",
listed(&kinds)
);
};
let at = format!("content[{index}]");
let resolved = match kind {
"heading" => {
require_block_keys(object, &at, "heading")?;
require_string(object, "text", index)?;
require_heading_level(object, index)?;
block.clone()
}
"paragraph" => {
require_block_keys(object, &at, "paragraph")?;
require_string(object, "text", index)?;
if object.get("footnote").is_some_and(|value| !value.is_null()) {
require_docx_block(format, &at, "footnote")?;
require_text(object, "footnote", index)?;
}
block.clone()
}
"notes" => {
if format != "pptx" {
anyhow::bail!(
"usage: content[{index}] (notes) is a presentation's speaker notes — \
only pptx has notes — hint: write a paragraph block instead"
);
}
require_block_keys(object, &at, "notes")?;
require_string(object, "text", index)?;
block.clone()
}
"list" => {
require_block_keys(object, &at, "list")?;
let Some(items) = object.get("items") else {
anyhow::bail!("usage: content[{index}] has no \"items\"");
};
require_scalar_array(items, &format!("content[{index}].items"))?;
require_optional_bool(object, "ordered", index)?;
block.clone()
}
"table" => {
require_block_keys(object, &at, "table")?;
let table = normalize_table(object);
let max = (format == "docx").then_some(RULES.table_columns_max);
require_table(&table, &at, max)?;
Value::Object(table)
}
"image" => resolve_image_block(ws, block, object, index, format, access).await?,
"link" | "page_break" | "contents" => {
docx_only_block(block, object, kind, index, format, &mut contents)?
}
other => anyhow::bail!(
"usage: content[{index}] has unknown type \"{other}\" — hint: use {}",
listed(&kinds)
),
};
out.push(resolved);
}
Ok(Value::Array(out))
}
fn docx_only_block(
block: &Value,
object: &serde_json::Map<String, Value>,
kind: &str,
index: usize,
format: &str,
contents: &mut bool,
) -> Result<Value> {
let at = format!("content[{index}]");
require_block_keys(object, &at, kind)?;
require_docx_block(format, &at, kind)?;
let mut resolved = block.clone();
match kind {
"link" => {
require_text(object, "text", index)?;
resolved["url"] = json!(link_address(require_string(object, "url", index)?, index)?);
}
"contents" if *contents => anyhow::bail!(
"usage: content[{index}] is a second table of contents — a document has one"
),
"contents" => *contents = true,
_ => {}
}
Ok(resolved)
}
async fn resolve_image_block(
ws: &Workspace,
block: &Value,
object: &serde_json::Map<String, Value>,
index: usize,
format: &str,
access: PathAccess,
) -> Result<Value> {
require_block_keys(object, &format!("content[{index}]"), "image")?;
require_optional_size(object, "width", index)?;
require_optional_size(object, "height", index)?;
let Some(raw) = object.get("image").and_then(Value::as_str) else {
anyhow::bail!(
"usage: content[{index}] (image) has no \"image\" — hint: give the image's path"
);
};
let (image, _) = resolve_input(ws, raw, access).await?;
if format == "pdf" {
ensure_picture(&image, &pdf_pictures())?;
} else {
ensure_picture(&image, &embedded_pictures())?;
}
let mut resolved = block.clone();
resolved["image"] = json!(image.to_string_lossy());
Ok(resolved)
}
async fn resolve_edit_images(ws: &Workspace, edits: &mut Value, access: PathAccess) -> Result<()> {
let list = edits
.as_array_mut()
.expect("validate_edits hands an edit list back as an array");
for edit in list {
let object = edit
.as_object_mut()
.expect("validate_edits hands every edit back as an object");
if object.get("op").and_then(Value::as_str) != Some("add_image") {
continue;
}
let raw = object
.get("image")
.and_then(Value::as_str)
.expect("the edit validator requires an add_image image")
.to_owned();
let (image, _) = resolve_input(ws, &raw, access).await?;
ensure_picture(&image, &embedded_pictures())?;
object.insert("image".to_owned(), json!(image.to_string_lossy()));
}
Ok(())
}
fn edits_search_slide_text(edits: &Value) -> bool {
edits.as_array().is_some_and(|edits| {
edits.iter().any(|edit| {
let names_a_lookup = edit.get("find").is_some() || edit.get("after").is_some();
names_a_lookup
&& edit
.get("op")
.and_then(Value::as_str)
.is_some_and(|op| PPTX_SLIDE_TEXT_OPS.contains(&op))
})
})
}
fn resolve_sheets(args: &Value) -> Result<Value> {
let sheets = match args.get("sheets") {
Some(Value::Array(sheets)) if !sheets.is_empty() => sheets,
Some(Value::Array(_)) => {
anyhow::bail!("usage: \"sheets\" is empty — hint: pass at least one sheet")
}
Some(v) => return Err(super::wrong_type("sheets", "an array", v)),
None => anyhow::bail!(
"usage: missing required argument \"sheets\" — hint: pass a JSON array of sheets, \
e.g. [{{\"rows\": [[\"Cell\", 1]]}}]"
),
};
let mut normalized = Vec::with_capacity(sheets.len());
let mut seen_names = HashSet::new();
for (index, sheet) in sheets.iter().enumerate() {
let Some(object) = sheet.as_object() else {
anyhow::bail!(
"usage: sheets[{index}] must be an object — hint: {{\"name\": …, \"rows\": [[…]]}}"
);
};
let at = format!("sheets[{index}]");
require_taken_keys(object, &at, "a sheet", &sheet_field_names(), &[])?;
let name = optional_string(object.get("name"), &at, "name")?;
let name = name.filter(|name| !name.is_empty()).map_or_else(
|| format!("Sheet{}", index + 1),
std::string::ToString::to_string,
);
require_sheet_name(&name, index, &mut seen_names)?;
let rows = match object.get("rows") {
Some(Value::Array(rows)) => normalize_sheet_cells(rows, index)?,
Some(v) => {
return Err(super::wrong_type(
&format!("sheets[{index}].rows"),
"an array",
v,
));
}
None => anyhow::bail!(
"usage: sheets[{index}] has no \"rows\" — hint: {{\"rows\": [[\"Cell\", 1]]}}"
),
};
let mut normalized_sheet = object.clone();
normalized_sheet.insert("name".to_owned(), json!(name));
normalized_sheet.insert("rows".to_owned(), rows);
match object.get("merges") {
None => {}
Some(Value::Null) => {
normalized_sheet.remove("merges");
}
Some(Value::Array(merges)) => require_sheet_merges(merges, index)?,
Some(v) => {
return Err(super::wrong_type(
&format!("sheets[{index}].merges"),
"an array",
v,
));
}
}
normalized.push(Value::Object(normalized_sheet));
}
Ok(Value::Array(normalized))
}
fn require_sheet_merges(merges: &[Value], index: usize) -> Result<()> {
for (position, merge) in merges.iter().enumerate() {
let at = format!("sheets[{index}].merges[{position}]");
match merge {
Value::String(range) => require_merge_text(range, &at)?,
other => return Err(super::wrong_type(&at, "a string", other)),
}
}
Ok(())
}
fn require_sheet_name_length(name: &str, at: &str) -> Result<()> {
if name.chars().count() > RULES.sheet_name_max {
anyhow::bail!(
"usage: {at} must be at most {} characters, got \"{name}\" — hint: shorten the sheet \
name",
RULES.sheet_name_max
);
}
Ok(())
}
fn require_sheet_name(name: &str, index: usize, seen: &mut HashSet<String>) -> Result<()> {
let at = format!("sheets[{index}].name");
require_sheet_name_text(name, &at)?;
if !seen.insert(name.to_lowercase()) {
anyhow::bail!(
"usage: {at} is not unique, ignoring case, got \"{name}\" — hint: give every sheet a \
distinct name"
);
}
Ok(())
}
fn require_sheet_name_text(name: &str, at: &str) -> Result<()> {
require_sheet_name_length(name, at)?;
if let Some(forbidden) = RULES
.sheet_name_forbidden
.chars()
.find(|character| name.contains(*character))
{
anyhow::bail!(
"usage: {at} must not contain {:?} — hint: a sheet name cannot hold any of {:?}",
forbidden.to_string(),
RULES.sheet_name_forbidden
);
}
Ok(())
}
fn normalized_formula(at: &str, object: &serde_json::Map<String, Value>) -> Result<Value> {
let normalized = object
.get("formula")
.and_then(Value::as_str)
.map(str::trim)
.map(|text| text.strip_prefix('=').unwrap_or(text).trim())
.filter(|text| !text.is_empty() && !text.starts_with('='));
match normalized {
Some(text) if object.len() == 1 => Ok(json!({ "formula": text })),
_ => anyhow::bail!(
"usage: {at} must be text, a number, a boolean or {{\"formula\": \"SUM(A1:A2)\"}} — \
hint: the formula's text alone, as in \"SUM(A1:A2)\""
),
}
}
fn normalize_sheet_cells(rows: &[Value], sheet: usize) -> Result<Value> {
let mut normalized_rows = Vec::with_capacity(rows.len());
for (row_index, row) in rows.iter().enumerate() {
let Some(cells) = row.as_array() else {
return Err(super::wrong_type(
&format!("sheets[{sheet}].rows[{row_index}]"),
"an array",
row,
));
};
let mut normalized_cells = Vec::with_capacity(cells.len());
for (cell_index, cell) in cells.iter().enumerate() {
let at = format!("sheets[{sheet}].rows[{row_index}][{cell_index}]");
match cell {
value if is_scalar(value) => normalized_cells.push(value.clone()),
Value::Object(object) => {
let formula = normalized_formula(&at, object)?;
normalized_cells.push(formula);
}
other => return Err(super::wrong_type(&at, SCALAR_TYPE, other)),
}
}
normalized_rows.push(Value::Array(normalized_cells));
}
Ok(Value::Array(normalized_rows))
}
fn require_table(
object: &serde_json::Map<String, Value>,
at: &str,
max_columns: Option<usize>,
) -> Result<()> {
let headers = match object.get("headers").filter(|value| !value.is_null()) {
None => 0,
Some(headers) => {
require_scalar_array(headers, &format!("{at}.headers"))?;
headers.as_array().map_or(0, Vec::len)
}
};
let mut widest = headers;
let mut laid_out = 0;
if let Some(rows) = object.get("rows").filter(|value| !value.is_null()) {
let Some(rows) = rows.as_array() else {
return Err(super::wrong_type(&format!("{at}.rows"), "an array", rows));
};
for (row_index, row) in rows.iter().enumerate() {
let Some(cells) = row.as_array() else {
return Err(super::wrong_type(
&format!("{at}.rows[{row_index}]"),
"an array",
row,
));
};
if let Some((cell_index, cell)) = scalar_array_violation(cells) {
return Err(super::wrong_type(
&format!("{at}.rows[{row_index}][{cell_index}]"),
SCALAR_TYPE,
cell,
));
}
widest = widest.max(cells.len());
laid_out += usize::from(!cells.is_empty());
}
}
if laid_out + headers == 0 {
anyhow::bail!(
"usage: {at} states a table with no rows and no headers — hint: give it at least one \
row or one header"
);
}
if let Some(max) = max_columns.filter(|max| widest > *max) {
anyhow::bail!(
"usage: {at} states {widest} columns — a table holds at most {max} — hint: split the \
table"
);
}
Ok(())
}
fn require_scalar_array(value: &Value, at: &str) -> Result<()> {
let Some(values) = value.as_array() else {
return Err(super::wrong_type(at, "an array", value));
};
match scalar_array_violation(values) {
Some((position, scalar)) => Err(super::wrong_type(
&format!("{at}[{position}]"),
SCALAR_TYPE,
scalar,
)),
None => Ok(()),
}
}
const SCALAR_TYPE: &str = "text, a number or a boolean";
fn scalar_kind(value: &Value) -> Option<&'static str> {
match value {
Value::String(_) => Some("string"),
Value::Number(_) => Some("number"),
Value::Bool(_) => Some("boolean"),
_ => None,
}
}
fn is_scalar(value: &Value) -> bool {
scalar_kind(value).is_some_and(|kind| RULES.scalar_kinds.iter().any(|allowed| allowed == kind))
}
fn scalar_array_violation(values: &[Value]) -> Option<(usize, &Value)> {
values
.iter()
.enumerate()
.find(|(_, value)| !is_scalar(value))
}
fn normalize_table(object: &serde_json::Map<String, Value>) -> serde_json::Map<String, Value> {
let mut table = object.clone();
if table
.get("headers")
.and_then(Value::as_array)
.is_some_and(Vec::is_empty)
{
table.remove("headers");
}
if let Some(Value::Array(rows)) = table.get_mut("rows") {
rows.retain(|row| !row.as_array().is_some_and(Vec::is_empty));
}
table
}
fn listed<T: std::fmt::Display>(values: &[T]) -> String {
crate::util::listed(values, "or")
}
fn megabytes(bytes: u64) -> u64 {
bytes / (1024 * 1024)
}
fn require_heading_level(object: &serde_json::Map<String, Value>, index: usize) -> Result<()> {
match object.get("level") {
None | Some(Value::Null) => Ok(()),
Some(value) => {
let valid = value
.as_f64()
.is_some_and(|level| level.fract() == 0.0 && RULES.heading_levels.contains(&level));
if valid {
Ok(())
} else {
anyhow::bail!(
"usage: content[{index}].level must be {} — hint: a lower number is the more \
important heading",
listed(&RULES.heading_levels)
)
}
}
}
}
fn require_optional_size(
object: &serde_json::Map<String, Value>,
key: &str,
index: usize,
) -> Result<()> {
match object.get(key) {
None | Some(Value::Null) => Ok(()),
Some(value) => {
let number = value.as_f64().ok_or_else(|| {
super::wrong_type(&format!("content[{index}].{key}"), "a number", value)
})?;
if !RULES.dimension_points.contains(number) {
anyhow::bail!(
"usage: content[{index}].{key} must be {} points, got {number} — hint: give \
the size the image should be placed at",
RULES.dimension_points.bounds()
);
}
Ok(())
}
}
}
fn require_optional_bool(
object: &serde_json::Map<String, Value>,
key: &str,
index: usize,
) -> Result<()> {
match object.get(key) {
None | Some(Value::Null | Value::Bool(_)) => Ok(()),
Some(value) => Err(super::wrong_type(
&format!("content[{index}].{key}"),
"a boolean",
value,
)),
}
}
fn optional_string<'a>(value: Option<&'a Value>, at: &str, key: &str) -> Result<Option<&'a str>> {
match value {
None | Some(Value::Null) => Ok(None),
Some(Value::String(text)) => Ok(Some(text)),
Some(other) => Err(super::wrong_type(&format!("{at}.{key}"), "a string", other)),
}
}
fn opt_metadata(args: &Value) -> Result<Option<Value>> {
let Some(value) = args.get("metadata") else {
return Ok(None);
};
if value.is_null() {
return Ok(None);
}
let Some(object) = value.as_object() else {
return Err(super::wrong_type("metadata", "an object", value));
};
let fields = crate::docgen::pdf_metadata_fields();
let keys = fields
.iter()
.map(|field| field.key.as_str())
.collect::<Vec<_>>();
if object.is_empty() {
anyhow::bail!(
"usage: \"metadata\" is empty — hint: name at least one of {}",
listed(&keys)
);
}
let mut metadata = serde_json::Map::with_capacity(object.len());
for (key, value) in object {
let Some(field) = fields.iter().find(|field| field.key == *key) else {
anyhow::bail!(
"usage: unknown metadata field \"{key}\" — hint: use {}",
listed(&keys)
);
};
let Some(text) = value.as_str() else {
return Err(super::wrong_type(
&format!("metadata.{key}"),
"a string",
value,
));
};
metadata.insert(
key.clone(),
Value::String(if field.date {
pdf_date(text)?
} else {
text.to_string()
}),
);
}
Ok(Some(Value::Object(metadata)))
}
fn set_metadata(request: &mut Value, metadata: Option<Value>) {
if let Some(metadata) = metadata {
request["metadata"] = metadata;
}
}
fn pdf_date(text: &str) -> Result<String> {
if text.is_empty() {
return Ok(String::new());
}
let refused = || {
anyhow::anyhow!(
"usage: \"{text}\" is not a date — hint: use {}",
crate::docgen::pdf_date_format()
)
};
let date = text.get(..10).ok_or_else(refused)?;
let date = chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d").map_err(|_| refused())?;
let mut rest = &text[10..];
let offset_minutes = if let Some(stripped) = rest.strip_suffix('Z') {
rest = stripped;
0
} else if let Some(index) = rest.rfind(['+', '-']) {
let (time, offset) = rest.split_at(index);
rest = time;
pdf_offset(offset).ok_or_else(refused)?
} else {
0
};
let time = if rest.is_empty() {
chrono::NaiveTime::from_hms_opt(0, 0, 0).expect("midnight is a valid time")
} else {
let time = rest.strip_prefix('T').ok_or_else(refused)?;
["%H:%M", "%H:%M:%S"]
.iter()
.find_map(|format| chrono::NaiveTime::parse_from_str(time, format).ok())
.ok_or_else(refused)?
};
let mut rendered = format!(
"D:{:04}{:02}{:02}{:02}{:02}{:02}",
date.year(),
date.month(),
date.day(),
time.hour(),
time.minute(),
time.second()
);
if offset_minutes == 0 {
rendered.push('Z');
} else {
let sign = if offset_minutes < 0 { '-' } else { '+' };
let minutes = offset_minutes.abs();
let _ = write!(rendered, "{sign}{:02}'{:02}'", minutes / 60, minutes % 60);
}
Ok(rendered)
}
fn pdf_offset(text: &str) -> Option<i32> {
let (sign, rest) = match text.as_bytes().first()? {
b'+' => (1, &text[1..]),
b'-' => (-1, &text[1..]),
_ => return None,
};
let (hours, minutes) = rest.split_once(':')?;
if hours.len() != 2 || minutes.len() != 2 {
return None;
}
let hours: i32 = hours.parse().ok()?;
let minutes: i32 = minutes.parse().ok()?;
if hours > 23 || minutes > 59 {
return None;
}
Some(sign * (hours * 60 + minutes))
}
fn require_values(args: &Value, kind: &str) -> Result<serde_json::Map<String, Value>> {
let values = super::get_object(args, "values")?;
if values.is_empty() {
anyhow::bail!(
"usage: missing or empty argument \"values\" — hint: name at least one {kind}, e.g. \
{{\"name\": \"Иван\"}}"
);
}
Ok(values)
}
fn require_scalar_values(values: &serde_json::Map<String, Value>) -> Result<()> {
for (name, value) in values {
if !is_scalar(value) {
return Err(super::wrong_type(
&format!("values.{name}"),
SCALAR_TYPE,
value,
));
}
}
Ok(())
}
fn require_hex_color(at: &str, color: &str) -> Result<()> {
let digits = color.strip_prefix('#').unwrap_or(color);
if digits.len() == RULES.color_digits && digits.chars().all(|digit| digit.is_ascii_hexdigit()) {
return Ok(());
}
anyhow::bail!(
"usage: {at} must be {} hex digits, with an optional leading \"#\", got \"{color}\" — \
hint: like \"#{}\"",
RULES.color_digits,
"f".repeat(RULES.color_digits)
)
}
fn require_string<'a>(
object: &'a serde_json::Map<String, Value>,
key: &str,
index: usize,
) -> Result<&'a str> {
match object.get(key) {
Some(Value::String(text)) => Ok(text),
Some(v) => Err(super::wrong_type(
&format!("content[{index}].{key}"),
"a string",
v,
)),
None => anyhow::bail!("usage: content[{index}] has no \"{key}\""),
}
}
fn require_visible(at: &str, text: &str, hint: &str) -> Result<()> {
if text.trim().is_empty() {
anyhow::bail!("usage: {at} is empty — hint: {hint}");
}
Ok(())
}
fn require_text(object: &serde_json::Map<String, Value>, key: &str, index: usize) -> Result<()> {
require_visible(
&format!("content[{index}].{key}"),
require_string(object, key, index)?,
"give the text it shows",
)
}
struct PictureFormats {
extensions: Vec<&'static str>,
names: &'static str,
}
fn embedded_pictures() -> PictureFormats {
PictureFormats {
extensions: RULES.image_extensions.iter().map(String::as_str).collect(),
names: "PNG, JPEG or WebP",
}
}
fn pdf_pictures() -> PictureFormats {
PictureFormats {
extensions: vec!["png", "jpg", "jpeg"],
names: "PNG or JPEG",
}
}
fn ensure_picture(path: &Path, formats: &PictureFormats) -> Result<()> {
if crate::util::has_extension(path, &formats.extensions) {
Ok(())
} else {
anyhow::bail!(
"usage: a picture must be {}, got {} — hint: convert the image and retry",
formats.names,
path.display()
)
}
}
enum Pages {
All,
Explicit(Vec<u32>),
}
impl Pages {
#[must_use]
fn kit_value(&self) -> Value {
match self {
Self::All => json!("all"),
Self::Explicit(pages) => json!(pages),
}
}
}
fn page_selections(args: &Value) -> Result<Vec<String>> {
let selections = super::get_str_array(args, "pages")?;
if selections.len() > 1
&& selections
.iter()
.any(|selection| selection.trim().eq_ignore_ascii_case("all"))
{
anyhow::bail!(
"usage: an \"all\" selection takes every page, so it cannot sit beside another \
selection — hint: name every page explicitly, or pass \"all\" alone"
);
}
Ok(selections)
}
fn opt_pages(args: &Value) -> Result<Pages> {
let mut seen = HashSet::new();
let mut pages = Vec::new();
let mut all = false;
for selection in page_selections(args)? {
let selection = selection.trim();
if selection.eq_ignore_ascii_case("all") {
all = true;
continue;
}
pages.extend(parse_range(selection, &mut seen)?);
}
if all || pages.is_empty() {
Ok(Pages::All)
} else {
Ok(Pages::Explicit(pages))
}
}
fn split_groups(args: &Value) -> Result<Vec<Value>> {
let selections = page_selections(args)?;
if selections.is_empty() {
anyhow::bail!(
"usage: missing required argument \"pages\" — hint: one page selection per output file, \
e.g. [\"1-3\", \"5\"]"
);
}
if selections.len() > MAX_SPLIT_PARTS {
anyhow::bail!(
"usage: {} output files is more than one call can produce (limit {MAX_SPLIT_PARTS}) — \
hint: ask for fewer page selections, or wider ones",
selections.len()
);
}
let mut seen = HashSet::new();
let mut groups = Vec::with_capacity(selections.len());
for selection in &selections {
let selection = selection.trim();
if selection.eq_ignore_ascii_case("all") {
groups.push(json!("all"));
continue;
}
groups.push(json!(parse_range(selection, &mut seen)?));
}
Ok(groups)
}
fn parse_range(spec: &str, seen: &mut HashSet<u32>) -> Result<Vec<u32>> {
let mut pages = Vec::new();
for token in spec.split(',') {
let token = token.trim();
if token.is_empty() {
anyhow::bail!(
"usage: empty page range in \"{spec}\" — hint: write a selection like \
\"1-3,5\""
);
}
let (start, end) = if let Some((start, end)) = token.split_once('-') {
(parse_page(start)?, parse_page(end)?)
} else {
let page = parse_page(token)?;
(page, page)
};
if end < start {
anyhow::bail!(
"usage: reversed page range \"{token}\" — hint: write it low-to-high, \
e.g. \"{end}-{start}\""
);
}
for page in start..=end {
if !seen.insert(page) {
anyhow::bail!(
"usage: page {page} appears more than once — hint: list each page once"
);
}
pages.push(page);
if seen.len() > RULES.max_pages_per_request {
anyhow::bail!(
"usage: too many pages requested (limit {})",
RULES.max_pages_per_request
);
}
}
}
Ok(pages)
}
fn parse_page(text: &str) -> Result<u32> {
let page: u32 = text.trim().parse().map_err(|_| {
anyhow::anyhow!(
"usage: \"{text}\" is not a page number — hint: pages are 1-based, like \"3\" or \"1-3\""
)
})?;
if page == 0 {
anyhow::bail!("usage: page 0 does not exist — hint: the first page is 1");
}
Ok(page)
}
fn set_number(request: &mut Value, args: &Value, key: &str) -> Result<()> {
match args.get(key) {
None | Some(Value::Null) => Ok(()),
Some(v) => {
let number = v
.as_f64()
.filter(|number| number.is_finite())
.ok_or_else(|| super::wrong_type(key, "a number", v))?;
if number.abs() > RULES.pdf_point_abs_max {
anyhow::bail!(
"usage: \"{key}\" must be within ±{} points, got {number} — hint: \
give a position or size on the page",
RULES.pdf_point_abs_max
);
}
request[key] = v.clone();
Ok(())
}
}
}
fn set_positive_number(request: &mut Value, args: &Value, key: &str, span: &Span) -> Result<()> {
set_bounded_number(request, args, key, span, "give a size in points")
}
fn set_bounded_number(
request: &mut Value,
args: &Value,
key: &str,
span: &Span,
hint: &str,
) -> Result<()> {
set_number(request, args, key)?;
if let Some(number) = request.get(key).and_then(Value::as_f64)
&& !span.contains(number)
{
anyhow::bail!(
"usage: \"{key}\" must be {}, got {number} — hint: {hint}",
span.bounds()
);
}
Ok(())
}
fn require_page_area(args: &Value) -> Result<Value> {
let Some(area) = args.get("area").filter(|value| !value.is_null()) else {
anyhow::bail!(
"usage: missing required argument \"area\" — hint: name x, y, width and height as \
fractions of the page's visible area"
);
};
let Some(object) = area.as_object() else {
return Err(super::wrong_type(
"area",
"an object of x, y, width and height",
area,
));
};
let names = ["x", "y", "width", "height"];
if let Some(unknown) = object.keys().find(|key| !names.contains(&key.as_str())) {
anyhow::bail!(
"usage: unknown area key \"{unknown}\" — hint: an area names exactly {}",
listed(&names)
);
}
let mut sides = [0f64; 4];
for (side, slot) in names.iter().zip(&mut sides) {
let Some(value) = object.get(*side) else {
anyhow::bail!(
"usage: \"area\" has no \"{side}\" — hint: name all four of {}",
listed(&names)
);
};
*slot = value
.as_f64()
.filter(|number| number.is_finite())
.ok_or_else(|| super::wrong_type(&format!("area.{side}"), "a number", value))?;
if !RULES.pdf_page_fraction.contains(*slot) {
anyhow::bail!(
"usage: \"area.{side}\" must be {}, got {} — hint: give a fraction of the page",
RULES.pdf_page_fraction.bounds(),
*slot
);
}
}
let [x, y, width, height] = sides;
if width <= 0.0 || height <= 0.0 {
anyhow::bail!(
"usage: \"area\" must have a size, got {width}×{height} — hint: give a width and a \
height above zero"
);
}
let slack = RULES.pdf_fraction_slack;
if x + width > 1.0 + slack || y + height > 1.0 + slack {
anyhow::bail!(
"usage: \"area\" reaches past the page (x {x} + width {width}, y {y} + height \
{height}) — hint: the format would silently clip it; name an area inside the page"
);
}
Ok(area.clone())
}
async fn refuse_symlinked_output(dir: &Path) -> Result<()> {
if let Ok(meta) = tokio::fs::symlink_metadata(dir).await
&& meta.file_type().is_symlink()
{
anyhow::bail!(
"forbidden: {} is a symlink — hint: document output needs a real directory there",
dir.display()
);
}
Ok(())
}
async fn reserve(dir: &Path, bases: &[String], ext: &str) -> Result<Vec<PathBuf>> {
refuse_symlinked_output(dir).await?;
tokio::fs::create_dir_all(dir)
.await
.with_context(|| format!("failed to create the output directory at {}", dir.display()))?;
let mut reserved = Vec::with_capacity(bases.len());
for base in bases {
match reserve_one(dir, base, ext).await {
Ok(path) => reserved.push(path),
Err(e) => {
remove_files(&reserved).await;
return Err(e);
}
}
}
Ok(reserved)
}
async fn reserve_one(dir: &Path, base: &str, ext: &str) -> Result<PathBuf> {
let name = format!("{base}.{ext}");
let (_file, path) = crate::util::create_unique_file(dir, &name)
.await
.with_context(|| format!("failed to reserve an output file in {}", dir.display()))?;
Ok(path)
}
async fn remove_files(paths: &[PathBuf]) {
for path in paths {
let _ = tokio::fs::remove_file(path).await;
}
}
pub(crate) fn file_marker(path: &Path) -> String {
format!("{FILE_MARKER}{}]", path.to_string_lossy())
}
pub(crate) fn describe(prefix: &str, names: &[String], sizes: &[u64]) -> String {
if names.len() == 1 {
return format!(
"{prefix} {} ({})",
names[0],
super::listing::human_readable_size(sizes[0])
);
}
let list = names
.iter()
.zip(sizes)
.map(|(name, size)| format!("{name} ({})", super::listing::human_readable_size(*size)))
.collect::<Vec<_>>()
.join(", ");
format!("{prefix} {} files: {list}", names.len())
}
#[must_use]
fn clean_component(raw: &str) -> String {
let last = raw.rsplit(['/', '\\']).next().unwrap_or(raw);
let cleaned: String = crate::util::neutralized_name(last)
.chars()
.filter(|c| !matches!(c, '<' | '>' | ':' | '"' | '|' | '?' | '*'))
.collect();
let capped: String = cleaned
.trim_matches(['.', ' '])
.chars()
.take(MAX_BASE_CHARS)
.collect();
capped.trim_matches(['.', ' ']).to_string()
}
#[must_use]
fn strip_output_extension(name: &str) -> &str {
let Some((stem, ext)) = name.rsplit_once('.') else {
return name;
};
if stem.is_empty() {
return name;
}
let appended =
crate::ooxml::family_of(Path::new(name)).is_some() || ext.eq_ignore_ascii_case("pdf");
if appended { stem } else { name }
}
#[must_use]
pub(crate) fn sanitize_base(raw: Option<&str>, fallback: &str) -> String {
let candidate = raw.map_or_else(|| clean_component(fallback), clean_component);
let preferred = strip_output_extension(&candidate);
if preferred.is_empty() {
clean_component(fallback)
} else {
preferred.to_string()
}
}
#[must_use]
fn default_base(format: &str) -> &'static str {
match format {
"xlsx" => "spreadsheet",
"pptx" => "presentation",
_ => "document",
}
}
#[must_use]
fn input_base(input: &Path, suffix: &str) -> String {
format!("{}_{suffix}", crate::util::name_stem(&file_name_of(input)))
}
#[must_use]
fn file_name_of(path: &Path) -> String {
crate::util::file_name_or_path(&path.to_string_lossy()).to_string()
}
#[must_use]
fn copy_extension(input: &Path, family: crate::ooxml::Family) -> String {
if crate::ooxml::family_of(input) == Some(family)
&& let Some(ext) = input.extension()
{
return ext.to_string_lossy().to_ascii_lowercase();
}
family.name().to_string()
}
#[must_use]
fn path_strings(paths: &[PathBuf]) -> Vec<String> {
paths
.iter()
.map(|path| path.to_string_lossy().into_owned())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::test::noisy_png;
use crate::workspace::test_ws_named;
use std::io::{Cursor, Write as _};
fn confined_document() -> DocumentTool {
DocumentTool::new(PathAccess::Workspace)
}
fn workspace() -> (tempfile::TempDir, crate::Workspace) {
let _ = crate::config::CONFIG.try_set_storage_root(crate::util::test::test_root().clone());
let dir = tempfile::TempDir::new().expect("tempdir");
let path = std::fs::canonicalize(dir.path()).expect("canonical workspace");
let ws = test_ws_named(&path.to_string_lossy(), "document-test");
(dir, ws)
}
fn add_bytes(zip: &mut zip::ZipWriter<Cursor<Vec<u8>>>, name: &str, body: &[u8]) {
zip.start_file(name, zip::write::SimpleFileOptions::default())
.expect("start part");
zip.write_all(body).expect("write part");
}
fn write_fixture(ws: &Workspace, name: &str, bytes: &[u8]) -> PathBuf {
let path = ws.as_path().join(name);
std::fs::write(&path, bytes).expect("write fixture");
path
}
fn package_with(part: &str) -> Vec<u8> {
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
add_bytes(&mut zip, part, b"<x/>");
zip.finish().expect("finish package").into_inner()
}
fn encrypted_package() -> Vec<u8> {
crate::legacy::test_fixtures::cfb(&[
("EncryptionInfo", b"info".as_slice()),
("EncryptedPackage", b"data".as_slice()),
])
}
fn legacy_doc() -> Vec<u8> {
crate::legacy::test_fixtures::cfb(&[("WordDocument", b"legacy word".as_slice())])
}
fn encrypted_legacy_doc() -> Vec<u8> {
crate::legacy::test_fixtures::doc_fixture("secret", &[], true)
}
fn fillable_pdf() -> Vec<u8> {
crate::document::test_fixtures::assemble_pdf(&[
b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [5 0 R] /DA (/Helv 0 Tf 0 g) >> >>"
.to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] \
/Resources << /Font << /Helv 4 0 R >> >> /Annots [5 0 R] >>"
.to_vec(),
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_vec(),
b"<< /Type /Annot /Subtype /Widget /FT /Tx /T (name) /Rect [72 700 300 730] \
/P 3 0 R /V () /DA (/Helv 12 Tf 0 g) >>"
.to_vec(),
])
}
#[tokio::test]
async fn a_docx_only_block_is_refused_for_another_format() {
let (_dir, ws) = workspace();
for (format, block) in [
("pptx", json!({ "type": "page_break" })),
("pdf", json!({ "type": "contents" })),
(
"pptx",
json!({ "type": "link", "text": "x", "url": "https://example.com" }),
),
(
"pdf",
json!({ "type": "paragraph", "text": "x", "footnote": "note" }),
),
] {
let err = resolve_content(
&ws,
&json!({ "content": [block.clone()] }),
format,
PathAccess::Workspace,
)
.await
.expect_err("a block only a .docx holds must be refused");
assert!(
err.to_string().contains(".docx"),
"the refusal must name the format it belongs to: {err}"
);
}
}
#[tokio::test]
async fn a_second_table_of_contents_is_refused() {
let (_dir, ws) = workspace();
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "contents" }, { "type": "contents" }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a document has one table of contents");
assert!(
err.to_string().contains("second table of contents"),
"the refusal must name the second table: {err}"
);
}
#[tokio::test]
async fn a_running_header_is_refused_for_another_format() {
let (_dir, ws) = workspace();
for (format, key) in [("pptx", "header"), ("pdf", "footer")] {
let err = confined_document()
.execute(
&ws,
json!({
"action": "create", "format": format, "file_name": "made",
key: "Report", "content": [{ "type": "paragraph", "text": "x" }],
}),
)
.await
.expect_err("a format without a running header must refuse one");
assert!(
err.to_string().contains("only a .docx has one"),
"the refusal must say which format carries one: {err}"
);
}
}
#[tokio::test]
async fn blank_content_is_refused() {
let (_dir, ws) = workspace();
for block in [
json!({ "type": "link", "text": "", "url": "https://example.com" }),
json!({ "type": "paragraph", "text": "x", "footnote": " " }),
] {
let err = resolve_content(
&ws,
&json!({ "content": [block] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("empty content must be refused");
assert!(
err.to_string().contains("is empty"),
"the refusal must say the field is empty: {err}"
);
}
for key in ["header", "footer"] {
let err = confined_document()
.execute(
&ws,
json!({
"action": "create", "format": "docx", "file_name": "blank",
key: " ", "content": [{ "type": "paragraph", "text": "x" }],
}),
)
.await
.expect_err("a blank running header or footer must be refused");
assert!(
err.to_string().contains("is empty"),
"the refusal must say the field is empty: {err}"
);
}
}
#[tokio::test]
async fn a_link_address_must_be_followable() {
let (_dir, ws) = workspace();
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "link", "text": "x", "url": "example.com" }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("an address without a scheme cannot be followed");
assert!(
err.to_string().contains("absolute URL"),
"the refusal must say what a link's address is: {err}"
);
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "link", "text": "x", "url": "javascript:alert(1)" }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a scheme outside the allowlist must be refused");
assert!(
err.to_string()
.contains("must be an http, https, mailto or tel address"),
"the refusal must name the schemes a link may use: {err}"
);
let resolved = resolve_content(
&ws,
&json!({ "content": [{ "type": "link", "text": "x", "url": " https://exa\nmple.com " }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect("a padded http address is the same address");
assert_eq!(
resolved[0]["url"],
json!("https://example.com/"),
"the document would state an address other than the one that parses: {resolved}"
);
}
#[tokio::test]
async fn pdf_crop_refuses_an_area_it_cannot_crop() {
let (_dir, ws) = workspace();
let source = ws.as_path().join("stub.pdf");
std::fs::write(&source, b"%PDF-1.4\n").expect("write stub pdf");
for (area, expected) in [
(
json!({ "x": 0.5, "y": 0.0, "width": 0.6, "height": 1.0 }),
"reaches past the page",
),
(
json!({ "x": 0.0, "y": 0.0, "width": 0.0, "height": 1.0 }),
"must have a size",
),
(
json!({ "x": 0.0, "y": 0.0, "width": 1.0, "height": 1.0, "top": 0.0 }),
"unknown area key \"top\"",
),
(
json!({ "x": 0.0, "y": 0.0, "width": 1.0 }),
"\"area\" has no \"height\"",
),
(Value::Null, "missing required argument \"area\""),
] {
let mut args = json!({
"action": "pdf_crop", "path": source.to_string_lossy(),
});
if !area.is_null() {
args["area"] = area;
}
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("an area the tool cannot crop must be refused");
let message = err.to_string();
assert!(message.starts_with("usage:"), "got: {message}");
assert!(
message.contains(expected),
"expected {expected:?}, got: {message}"
);
}
}
#[tokio::test]
async fn pdf_resize_refuses_a_single_side() {
let (_dir, ws) = workspace();
let source = ws.as_path().join("stub.pdf");
std::fs::write(&source, b"%PDF-1.4\n").expect("write stub pdf");
for side in ["width", "height"] {
let mut args = json!({
"action": "pdf_resize", "path": source.to_string_lossy(),
});
args[side] = json!(300);
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("a resize with one side must be refused");
let message = err.to_string();
assert!(message.starts_with("usage:"), "got: {message}");
assert!(
message.contains("a resize names both sides"),
"got: {message}"
);
}
}
#[tokio::test]
async fn inputs_follow_the_callers_access() {
let (dir, ws) = workspace();
let outside = tempfile::TempDir::new().unwrap();
let sample = outside.path().join("sample.docx");
std::fs::write(&sample, b"a sample the kit would take").unwrap();
let path = sample.to_string_lossy().to_string();
let err = resolve_input(&ws, &path, PathAccess::Workspace)
.await
.expect_err("a guest's input must stay inside its workspace");
assert!(
err.to_string().contains("outside the workspace"),
"the refusal must name the boundary: {err}"
);
resolve_input(&ws, &path, PathAccess::Unrestricted)
.await
.expect("the admin's input may be read from anywhere");
drop((dir, outside));
}
#[tokio::test]
async fn an_unopenable_sample_is_refused_as_a_usage_error() {
let (_dir, ws) = workspace();
let sample = write_fixture(&ws, "encrypted.docx", &encrypted_package());
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template", "file_name": "copy",
"path": sample.to_string_lossy(), "values": { "name": "Имя" },
}),
)
.await
.expect_err("a sample that is not a package must be refused");
let message = err.to_string();
assert!(message.starts_with("usage:"), "got: {message}");
assert!(
message.contains("password-protected"),
"the refusal must give the read path's verdict: {message}"
);
assert!(
message.contains("encrypted.docx"),
"the refusal must name the file: {message}"
);
let legacy = write_fixture(&ws, "legacy.docx", &legacy_doc());
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template", "file_name": "copy",
"path": legacy.to_string_lossy(), "values": { "name": "Имя" },
}),
)
.await
.expect_err("a legacy document is not a modern package");
assert!(
!err.to_string().contains("password-protected"),
"an old-format document under a .docx name is not encrypted: {err}"
);
}
#[tokio::test]
async fn a_legacy_encrypted_document_is_the_password_verdict() {
let (_dir, ws) = workspace();
let secret = write_fixture(&ws, "secret.doc", &encrypted_legacy_doc());
let err = confined_document()
.execute(
&ws,
json!({
"action": "docx_edit", "path": secret.to_string_lossy(),
"edits": [{ "op": "replace_text", "find": "x", "replace": "y" }],
}),
)
.await
.expect_err("an encrypted old document cannot be edited");
let message = err.to_string();
assert!(
message.contains("password-protected"),
"the edit refusal must give the read path's verdict: {message}"
);
assert!(
!message.contains("save it as one of the OOXML families"),
"a password is not a reason to convert the file: {message}"
);
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template", "file_name": "copy",
"path": secret.to_string_lossy(), "values": { "name": "Имя" },
}),
)
.await
.expect_err("an encrypted old document cannot be filled");
let message = err.to_string();
assert!(
message.contains("password-protected"),
"the fill refusal must give the read path's verdict: {message}"
);
assert!(
message.contains("secret.doc"),
"the refusal must name the file: {message}"
);
}
#[tokio::test]
async fn a_pdf_action_tells_an_encrypted_input_to_convert_it_to_a_pdf() {
let (_dir, ws) = workspace();
for (name, bytes) in [
("package.docx", encrypted_package()),
("secret.doc", encrypted_legacy_doc()),
] {
let path = write_fixture(&ws, name, &bytes);
let err = confined_document()
.execute(
&ws,
json!({
"action": "pdf_rotate", "path": path.to_string_lossy(), "degrees": 90,
}),
)
.await
.expect_err("an encrypted input cannot be read as a PDF");
let message = err.to_string();
assert!(
message.contains("password-protected"),
"{name}: the refusal must give the read path's verdict: {message}"
);
assert!(
message.contains("and convert it to a PDF"),
"{name}: a PDF action's password refusal must ask for a PDF: {message}"
);
}
}
#[tokio::test]
async fn a_sample_is_answered_by_what_it_really_holds() {
let (_dir, ws) = workspace();
let cases = [
(
"report.doc",
legacy_doc(),
"an old .doc/.xls/.ppt file is read but never filled",
),
(
"sheet.xls",
package_with("xl/workbook.xml"),
"hint: rename it to match its content",
),
(
"chart.docx",
b"not a container".to_vec(),
"which is corrupt or unsupported .docx",
),
];
for (name, bytes, expected) in cases {
std::fs::write(ws.as_path().join(name), &bytes).expect("write sample");
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template", "file_name": "copy",
"path": name, "values": { "name": "Имя" },
}),
)
.await
.expect_err("a sample of another format must be refused");
assert!(
err.to_string().contains(expected),
"{name}: expected {expected:?}, got {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[tokio::test]
async fn a_fill_with_no_values_is_refused() {
let (_dir, ws) = workspace();
let sample = write_fixture(&ws, "sample.docx", &package_with("word/document.xml"));
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template", "file_name": "copy",
"path": sample.to_string_lossy(), "values": {},
}),
)
.await
.expect_err("an empty fill must be refused");
let message = err.to_string();
assert!(
message.contains("missing or empty argument \"values\""),
"got: {message}"
);
assert!(message.contains("placeholder"), "got: {message}");
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[tokio::test]
async fn a_structured_substitute_is_refused() {
let (_dir, ws) = workspace();
let sample = ws.as_path().join("sample.docx");
std::fs::write(&sample, b"not a package").expect("write sample");
for action in ["fill_template", "pdf_form_fill"] {
let err = confined_document()
.execute(
&ws,
json!({
"action": action, "path": sample.to_string_lossy(),
"values": { "name": { "a": 1 } },
}),
)
.await
.expect_err("a structure is not a value");
let message = err.to_string();
assert!(message.starts_with("usage:"), "got: {message}");
assert!(message.contains("values.name"), "got: {message}");
}
}
#[tokio::test]
async fn every_advertised_action_is_dispatched() {
let (_dir, ws) = workspace();
for action in ACTIONS {
let error = confined_document()
.execute(&ws, json!({ "action": action }))
.await
.expect_err("an action with no arguments cannot succeed");
let message = format!("{error:#}");
assert!(!message.contains("unknown action"), "{action}: {message}");
}
}
#[tokio::test]
#[expect(clippy::too_many_lines)] async fn the_content_and_sheet_shapes_are_checked_at_the_boundary() {
let (_dir, ws) = workspace();
let content = [
(
json!({ "content": [{ "type": "table", "rows": [] }] }),
"no rows and no headers",
),
(
json!({ "content": [{ "type": "table", "headers": [], "rows": [] }] }),
"no rows and no headers",
),
(
json!({ "content": [{ "type": "table", "rows": ["not a row"] }] }),
"content[0].rows[0]",
),
(
json!({ "content": [{ "type": "table", "rows": [[{ "a": 1 }]] }] }),
"content[0].rows[0][0]",
),
(
json!({ "content": [{ "type": "table", "headers": [{ "a": 1 }], "rows": [["a"]] }] }),
"content[0].headers[0]",
),
(
json!({ "content": [{ "type": "list", "items": [{ "a": 1 }] }] }),
"content[0].items[0]",
),
(
json!({ "content": [{ "type": "heading", "text": "h", "level": 7 }] }),
"level must be 1, 2 or 3",
),
(
json!({ "content": [{ "type": "heading", "text": "h", "level": "2" }] }),
"level must be 1, 2 or 3",
),
(
json!({ "content": [{ "type": "list", "items": ["a"], "ordered": "yes" }] }),
"content[0].ordered",
),
(
json!({ "content": [{ "type": "image", "image": "x.png", "width": { "a": 1 } }] }),
"content[0].width",
),
(
json!({ "content": [{ "type": "image", "image": "x.png", "height": -1 }] }),
"content[0].height",
),
(
json!({ "content": [{ "type": "image", "image": "x.png", "width": 1e30 }] }),
"content[0].width",
),
];
for (args, expected) in content {
let err = resolve_content(&ws, &args, "docx", PathAccess::Workspace)
.await
.expect_err("a shape the writers cannot render");
assert!(
err.to_string().contains(expected),
"expected the refusal to name {expected}: {err}"
);
}
let long_name = "a".repeat(RULES.sheet_name_max + 1);
let sheets = [
(
json!({ "sheets": [{ "name": 5, "rows": [["a"]] }] }),
"sheets[0].name",
),
(
json!({ "sheets": [{ "name": long_name, "rows": [["a"]] }] }),
"sheets[0].name",
),
(
json!({ "sheets": [{ "name": "bad:name", "rows": [["a"]] }] }),
"sheets[0].name",
),
(
json!({ "sheets": [
{ "name": "Trips", "rows": [["a"]] },
{ "name": "trips", "rows": [["b"]] },
] }),
"sheets[1].name",
),
(
json!({ "sheets": [{ "rows": [[null]] }] }),
"sheets[0].rows[0][0]",
),
(
json!({ "sheets": [{ "rows": [[{ "b": 1 }]] }] }),
"sheets[0].rows[0][0]",
),
(
json!({ "sheets": [{ "rows": [[{ "formula": " " }]] }] }),
"sheets[0].rows[0][0]",
),
(
json!({ "sheets": [{ "rows": [[{ "formula": "=" }]] }] }),
"sheets[0].rows[0][0]",
),
(
json!({ "sheets": [{ "rows": [[{ "formula": "==SUM(A1:A2)" }]] }] }),
"sheets[0].rows[0][0]",
),
(json!({ "sheets": [[["a"]]] }), "sheets[0]"),
];
for (args, expected) in sheets {
let err = resolve_sheets(&args).expect_err("a shape the writer cannot write");
assert!(
err.to_string().contains(expected),
"expected the refusal to name {expected}: {err}"
);
}
let good = json!({ "content": [{ "type": "table", "rows": [["a", "b"]] }] });
assert!(
resolve_content(&ws, &good, "docx", PathAccess::Workspace)
.await
.is_ok()
);
let good =
json!({ "content": [{ "type": "table", "headers": null, "rows": [["a", "b"]] }] });
assert!(
resolve_content(&ws, &good, "docx", PathAccess::Workspace)
.await
.is_ok()
);
let headers_only = json!({ "content": [{ "type": "table", "headers": ["a"] }] });
assert!(
resolve_content(&ws, &headers_only, "docx", PathAccess::Workspace)
.await
.is_ok()
);
let good = json!({ "sheets": [{ "name": "Лист", "rows": [["a", 1, true, { "formula": "=SUM(A1:A2)" }]] }] });
let normalized = resolve_sheets(&good).expect("a well-formed sheet");
assert_eq!(
normalized[0]["rows"][0][3],
json!({ "formula": "SUM(A1:A2)" }),
"the formula must reach the request without its leading `=`"
);
}
#[tokio::test]
async fn a_table_block_takes_the_one_shape_both_producers_write() {
let (_dir, ws) = workspace();
let no_headers = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "headers": [], "rows": [["a"]] }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect("a table with rows is a table");
assert!(
no_headers[0].get("headers").is_none(),
"an empty header set must be dropped: {no_headers}"
);
let no_empty_row = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "rows": [[], ["a"]] }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect("a table with one cell is a table");
assert_eq!(no_empty_row[0]["rows"], json!([["a"]]));
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "headers": [], "rows": [[], []] }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a table of no cells must still be refused");
assert!(
err.to_string().contains("no rows and no headers"),
"got: {err}"
);
let wide = vec![json!("c"); RULES.table_columns_max + 1];
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "rows": [wide.clone()] }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a table past the column bound must be refused");
assert!(
err.to_string().contains("a table holds at most"),
"got: {err}"
);
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "headers": wide.clone(), "rows": [["a"]] }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a headings row past the column bound must be refused");
assert!(
err.to_string().contains("a table holds at most"),
"got: {err}"
);
let deck = resolve_content(
&ws,
&json!({ "content": [{ "type": "table", "rows": [wide] }] }),
"pptx",
PathAccess::Workspace,
)
.await
.expect("a presentation's table has no column bound");
assert_eq!(
deck[0]["rows"],
json!([vec![json!("c"); RULES.table_columns_max + 1]])
);
}
#[tokio::test]
#[expect(clippy::too_many_lines)] async fn the_arguments_are_checked_before_an_output_is_reserved() {
let (_dir, ws) = workspace();
let image = ws.as_path().join("picture.png");
std::fs::write(&image, noisy_png(4, 4)).expect("write png");
let notes = ws.as_path().join("notes.txt");
std::fs::write(¬es, b"not an image").expect("write notes");
let document = ws.as_path().join("document.pdf");
std::fs::write(&document, fillable_pdf()).expect("write pdf");
let merged = ws.as_path().join("merged.pdf");
std::fs::write(&merged, fillable_pdf()).expect("write pdf");
let path = document.to_string_lossy();
let huge = ws.as_path().join("huge.pdf");
std::fs::File::create(&huge)
.and_then(|file| file.set_len(crate::util::FILE_MAX_BYTES + 1))
.expect("a sparse oversized input");
let mut batch = Vec::new();
for index in 0..5 {
let member = ws.as_path().join(format!("batch{index}.pdf"));
std::fs::File::create(&member)
.and_then(|file| file.set_len(crate::util::FILE_MAX_BYTES))
.expect("a sparse input at the per-file limit");
batch.push(member.to_string_lossy().into_owned());
}
let calls = [
(
json!({ "action": "create", "format": "docx", "file_name": 5,
"content": [{ "type": "paragraph", "text": "x" }] }),
"file_name",
),
(
json!({ "action": "create", "format": "xlsx",
"sheets": [{ "rows": [["a"]] }],
"content": [{ "type": "paragraph", "text": "x" }] }),
"\"content\" is not used for format \"xlsx\"",
),
(
json!({ "action": "create", "format": "docx",
"content": [{ "type": "paragraph", "text": "x" }],
"sheets": [{ "rows": [["a"]] }] }),
"\"sheets\" is not used for format \"docx\"",
),
(
json!({ "action": "pdf_text", "path": path, "text": "x", "color": "red" }),
"color",
),
(
json!({ "action": "pdf_text", "path": path, "text": "x", "size": 0 }),
"size",
),
(
json!({ "action": "pdf_text", "path": path, "text": "x", "size": 1e300 }),
"within ±20000",
),
(
json!({ "action": "pdf_image", "path": path, "image": image.to_string_lossy(),
"x": -1e300 }),
"within ±20000",
),
(
json!({ "action": "pdf_form_fill", "path": path, "values": { "name": "x" },
"flatten": "yes" }),
"flatten",
),
(
json!({ "action": "pdf_image", "path": path,
"image": image.to_string_lossy(), "width": -5 }),
"width",
),
(
json!({ "action": "pdf_image", "path": path, "image": notes.to_string_lossy() }),
"PNG or JPEG",
),
(
json!({ "action": "pdf_rotate", "path": path, "degrees": 45 }),
"multiple of 90",
),
(
json!({ "action": "pdf_rotate", "path": path, "degrees": 360 }),
"would leave the pages as they are",
),
(
json!({ "action": "pdf_form_fill", "path": path, "values": {} }),
"empty",
),
(
json!({ "action": "pdf_rotate", "path": huge.to_string_lossy(), "degrees": 90 }),
"MB limit",
),
(
json!({ "action": "pdf_rotate", "path": path, "degrees": 90, "pages": ["0"] }),
"page 0 does not exist",
),
(
json!({ "action": "pdf_merge", "paths": [merged.to_string_lossy()] }),
"at least 2",
),
(
json!({ "action": "pdf_merge",
"paths": (0..=MAX_MERGE_INPUTS).map(|index| format!("part{index}.pdf")).collect::<Vec<_>>() }),
"limit 50",
),
(
json!({ "action": "pdf_merge", "paths": batch }),
"more than one merge takes",
),
(
json!({ "action": "pdf_split", "path": path,
"pages": (1..=MAX_SPLIT_PARTS + 1).map(|n| n.to_string()).collect::<Vec<_>>() }),
"limit 50",
),
(
json!({ "action": "pdf_split", "path": path,
"pages": [format!("1-{}", RULES.max_pages_per_request + 1)] }),
"limit 10000",
),
(
json!({ "action": "pdf_split", "path": path, "pages": ["3-1"] }),
"reversed",
),
];
for (args, expected) in calls {
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("a call the tool cannot carry out");
assert!(
err.to_string().contains(expected),
"expected the refusal to name {expected}: {err}"
);
}
let left = std::fs::read_dir(ws.as_path().join(GENERATED_DIR))
.map_or(0, std::iter::Iterator::count);
assert_eq!(left, 0, "a refused call left an output behind");
}
#[test]
fn pdf_dates_are_written_in_the_pdfs_own_spelling() {
assert_eq!(
pdf_date("2024-05-17").expect("a calendar date"),
"D:20240517000000Z"
);
assert_eq!(
pdf_date("2024-05-17T10:30").expect("a date and minutes"),
"D:20240517103000Z"
);
assert_eq!(
pdf_date("2024-05-17T10:30:15Z").expect("a date, seconds and Z"),
"D:20240517103015Z"
);
assert_eq!(
pdf_date("2024-05-17T10:30:00+03:00").expect("a positive offset"),
"D:20240517103000+03'00'"
);
assert_eq!(pdf_date("").expect("an empty string clears"), "");
for bad in [
"2024-13-01",
"2024-05-17T10:30:00 o'clock",
"2024-05-17 10:30:15Z",
"2024-05-17T10:30:00-0530",
] {
let err = pdf_date(bad).expect_err("a bad date must be refused");
let message = err.to_string();
assert!(message.starts_with("usage:"), "got: {message}");
assert!(
message.contains(bad),
"the refusal must echo {bad:?}: {message}"
);
assert!(
message.contains("YYYY-MM-DD"),
"the refusal must spell the accepted form: {message}"
);
}
}
#[tokio::test]
async fn refuses_a_sample_outside_the_workspace() {
let (_dir, ws) = workspace();
let outside = tempfile::NamedTempFile::new().expect("temp file");
let err = confined_document()
.execute(
&ws,
json!({
"action": "fill_template",
"path": outside.path().to_string_lossy(),
"values": {},
}),
)
.await
.expect_err("a sample outside the workspace must be refused");
assert!(
err.to_string().contains("outside the workspace"),
"got: {err}"
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_symlinked_generated_directory_is_refused() {
let (dir, ws) = workspace();
let elsewhere = dir.path().join("elsewhere");
std::fs::create_dir_all(&elsewhere).expect("target directory");
let generated = ws.as_path().join("generated");
std::os::unix::fs::symlink(&elsewhere, &generated).expect("symlink generated");
let err = reserve(&generated, &["report".to_string()], "docx")
.await
.expect_err("a symlinked generated directory must be refused");
assert!(err.to_string().starts_with("forbidden:"), "got: {err}");
assert!(
std::fs::read_dir(&elsewhere)
.expect("target directory")
.next()
.is_none(),
"nothing may be written through the symlink"
);
}
fn generated_count(ws: &Workspace) -> usize {
std::fs::read_dir(ws.as_path().join(GENERATED_DIR)).map_or(0, std::iter::Iterator::count)
}
#[tokio::test]
async fn editing_refuses_an_input_of_another_family() {
let (_dir, ws) = workspace();
let legacy = "an old .doc/.xls/.ppt file is read but never edited";
let cases = [
("report.doc", legacy_doc(), "docx_edit", legacy),
("sheet.xls", legacy_doc(), "xlsx_edit", legacy),
("deck.ppt", legacy_doc(), "pptx_edit", legacy),
(
"paper.pdf",
fillable_pdf(),
"docx_edit",
"edits only a .docx/.docm file",
),
(
"paper.pdf",
fillable_pdf(),
"xlsx_edit",
"edits only a .xlsx/.xlsm file",
),
(
"paper.pdf",
fillable_pdf(),
"pptx_edit",
"edits only a .pptx/.pptm file",
),
(
"report.docx",
package_with("xl/workbook.xml"),
"docx_edit",
"which is really an .xlsx package",
),
(
"sheet.xlsx",
package_with("ppt/presentation.xml"),
"xlsx_edit",
"which is really a .pptx package",
),
(
"notes.msg",
crate::legacy::test_fixtures::cfb(&[("__properties_version1.0", b"outlook")]),
"docx_edit",
"which is not a Word, Excel, PowerPoint or PDF document",
),
];
for (name, bytes, action, expected) in cases {
std::fs::write(ws.as_path().join(name), &bytes).expect("write input");
let err = confined_document()
.execute(
&ws,
json!({
"action": action, "path": name,
"edits": [{ "op": "replace_text", "find": "x", "replace": "y" }],
}),
)
.await
.expect_err("a wrong-family input must be refused");
assert!(
err.to_string().contains(expected),
"{action} on {name}: expected {expected:?}, got {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
for (name, bytes, expected) in [
("encrypted.docx", encrypted_package(), "password-protected"),
("container.doc", legacy_doc(), legacy),
] {
std::fs::write(ws.as_path().join(name), &bytes).expect("write input");
let err = confined_document()
.execute(
&ws,
json!({
"action": "docx_edit", "path": name,
"edits": [{ "op": "replace_text", "find": "x", "replace": "y" }],
}),
)
.await
.expect_err("a container is refused for what it really holds");
assert!(
err.to_string().contains(expected),
"{name}: expected {expected:?}, got {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[tokio::test]
async fn a_doc_named_zip_is_not_an_old_doc() {
let (_dir, ws) = workspace();
let archive = write_fixture(&ws, "archive.doc", &package_with("mimetype"));
let err = confined_document()
.execute(
&ws,
json!({
"action": "docx_edit", "path": archive.to_string_lossy(),
"edits": [{ "op": "replace_text", "find": "x", "replace": "y" }],
}),
)
.await
.expect_err("a ZIP that holds no package is not a docx");
let message = err.to_string();
assert!(
message.contains("which is not a Word, Excel, PowerPoint or PDF document"),
"got: {message}"
);
assert!(
!message.contains("old .doc/.xls/.ppt"),
"a container that is no old document must not be called one: {message}"
);
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[tokio::test]
async fn a_pdf_is_taken_under_any_name() {
let (_dir, ws) = workspace();
let renamed = write_fixture(&ws, "notes.txt", &fillable_pdf());
require_pdf(&renamed)
.await
.expect("a PDF under any name is a PDF");
let workbook = write_fixture(&ws, "sheet.txt", &package_with("xl/workbook.xml"));
let err = require_pdf(&workbook)
.await
.expect_err("a workbook is not a PDF, whatever it is called");
assert!(
err.to_string().contains("which is really an .xlsx package"),
"got: {err}"
);
if let Err(e) = confined_document()
.execute(
&ws,
json!({ "action": "pdf_text", "path": "notes.txt", "text": "x" }),
)
.await
{
assert!(!e.to_string().contains("must be a PDF"), "got: {e}");
}
}
#[tokio::test]
async fn the_pages_argument_is_an_array_of_selections() {
let (_dir, ws) = workspace();
assert_eq!(
split_groups(&json!({ "pages": ["1-3", "5"] })).expect("two selections"),
vec![json!([1, 2, 3]), json!([5])],
"one output file per element, in order"
);
assert_eq!(
split_groups(&json!({ "pages": ["all"] })).expect("one file, every page"),
vec![json!("all")],
"an element naming every page is passed through unexpanded"
);
assert_eq!(
opt_pages(&json!({ "pages": ["1-3", "5"] }))
.expect("a selection list")
.kit_value(),
json!([1, 2, 3, 5]),
"the union of the elements is what the action acts on"
);
for pages in [
json!({}),
json!({ "pages": null }),
json!({ "pages": [] }),
json!({ "pages": ["all"] }),
] {
assert_eq!(
opt_pages(&pages).expect("every page").kit_value(),
json!("all"),
"{pages} names no page to narrow the call to"
);
}
for pages in [json!({ "pages": [""] }), json!({ "pages": ["1-3", ""] })] {
let err = opt_pages(&pages)
.map(|pages| pages.kit_value())
.expect_err("a blank page element must be refused");
assert!(
err.to_string().contains("empty page range"),
"{pages}: got {err}"
);
}
let duplicate = split_groups(&json!({ "pages": ["1", "2", "1"] }))
.expect_err("a page named twice is refused");
assert!(
duplicate.to_string().contains("appears more than once"),
"got: {duplicate}"
);
for pages in [
json!({ "pages": ["all", "1"] }),
json!({ "pages": ["1-3", "all"] }),
json!({ "pages": ["all", "all"] }),
] {
let err = split_groups(&pages).expect_err("an \"all\" element stands alone");
assert!(
err.to_string()
.contains("cannot sit beside another selection"),
"split {pages}: got {err}"
);
let err = opt_pages(&pages)
.map(|pages| pages.kit_value())
.expect_err("an \"all\" element stands alone");
assert!(
err.to_string()
.contains("cannot sit beside another selection"),
"{pages}: got {err}"
);
}
let pdf = write_fixture(&ws, "base.pdf", &fillable_pdf());
let err = confined_document()
.execute(
&ws,
json!({ "action": "pdf_rotate", "path": pdf.to_string_lossy(),
"degrees": 90, "pages": ["1-2", "2"] }),
)
.await
.expect_err("a page named twice is refused");
assert!(
err.to_string().contains("appears more than once"),
"got: {err}"
);
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[tokio::test]
async fn a_nested_action_call_is_dispatched_like_a_flat_one() {
let (_dir, ws) = workspace();
assert_eq!(
normalize_call(&json!({
"action": { "pdf_rotate": { "path": "a.pdf", "degrees": 90 } },
"file_name": "turned",
}))
.expect("the nested shape"),
json!({
"action": "pdf_rotate", "path": "a.pdf", "degrees": 90, "file_name": "turned"
})
);
assert_eq!(
normalize_call(&json!({ "action": "pdf_rotate", "pdf_rotate": { "degrees": 90 } }))
.expect("the chrome shape"),
json!({ "action": "pdf_rotate", "degrees": 90 })
);
assert_eq!(
normalize_call(&json!({
"action": { "pdf_text": { "path": "a.pdf", "text": "x" } },
"pages": ["1"],
}))
.expect("the nested shape with a selection beside it"),
json!({ "action": "pdf_text", "path": "a.pdf", "text": "x", "pages": ["1"] })
);
for bad in [
json!({ "action": 5 }),
json!({ "action": ["pdf_rotate"] }),
json!({ "action": { "a": 1, "b": 2 } }),
json!({ "action": {} }),
] {
let err = normalize_call(&bad).expect_err("a shape the tool cannot read");
assert!(err.to_string().contains("\"action\""), "{bad}: {err}");
}
let pdf = write_fixture(&ws, "base.pdf", &fillable_pdf());
let err = confined_document()
.execute(
&ws,
json!({
"action": { "pdf_rotate": { "path": pdf.to_string_lossy(), "degrees": 45 } }
}),
)
.await
.expect_err("45 degrees is not a multiple of 90");
assert!(err.to_string().contains("multiple of 90"), "got: {err}");
}
#[tokio::test]
async fn an_action_refuses_an_argument_it_does_not_take() {
let (_dir, ws) = workspace();
for (args, expected) in [
(
json!({ "action": "pdf_form_fill", "path": "form.pdf", "pages": ["1"],
"values": { "name": "Иван" } }),
"takes no argument \"pages\"",
),
(
json!({ "action": "fill_template", "template": "sample.docx",
"values": { "name": "Иван" } }),
"takes no argument \"template\"",
),
] {
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("an argument the action does not take must be refused");
assert!(
err.to_string().contains(expected),
"expected {expected:?}: {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
let err = confined_document()
.execute(&ws, json!({ "action": "no_such_action" }))
.await
.expect_err("an action the tool does not have");
assert!(err.to_string().contains("unknown action"), "got: {err}");
}
#[tokio::test]
#[expect(clippy::too_many_lines)] async fn edit_shapes_are_checked_before_an_output_is_reserved() {
let (_dir, ws) = workspace();
for (name, part) in [
("doc.docx", "word/document.xml"),
("book.xlsx", "xl/workbook.xml"),
("deck.pptx", "ppt/presentation.xml"),
] {
std::fs::write(ws.as_path().join(name), package_with(part)).expect("write input");
}
let too_many: Vec<Value> = (0..=RULES.edits_max)
.map(|_| json!({ "op": "replace_text", "find": "x", "replace": "y" }))
.collect();
let long_style = "a".repeat(RULES.paragraph_style_max + 1);
let style_cap = format!("must be at most {} characters", RULES.paragraph_style_max);
let wide_row: Vec<Value> = (0..=RULES.table_columns_max)
.map(|column| json!(column))
.collect();
let calls = [
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "nope", "find": "x" }] }),
"unknown docx edit",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "nope", "sheet": "Values", "cell": "A1" }] }),
"unknown xlsx edit",
),
(
json!({ "action": "pptx_edit", "path": "deck.pptx",
"edits": [{ "op": "nope", "slide": 1 }] }),
"unknown pptx edit",
),
(
json!({ "action": "pptx_edit", "path": "deck.pptx",
"edits": [{ "op": "replace_text", "slide": 1, "find": " ",
"replace": "x" }] }),
"must name text",
),
(
json!({ "action": "pptx_edit", "path": "deck.pptx",
"edits": [{ "op": "add_paragraph", "slide": 1, "text": "X",
"after": "\t" }] }),
"must name text",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "replace_text", "replace": "y" }] }),
"must not be empty",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x" }] }),
"at least one of bold",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x", "bold": null }] }),
"at least one of bold",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x", "size": 1e300 }] }),
"size must be",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x", "size": f64::INFINITY }] }),
"at least one of bold",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x", "bold": true,
"level": 1 }] }),
"level is not a field of docx",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_text", "find": "x", "color": "red" }] }),
"got \"red\"",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"align": "middle" }] }),
"got \"middle\"",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x" }] }),
"at least one of align",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x", "level": 2 }] }),
"needs a list to belong to",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x", "list": false,
"level": 2 }] }),
"needs a list to belong to",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"list": "dash" }] }),
"got \"dash\"",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"style": long_style }] }),
style_cap.as_str(),
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"indent_left": 3000 }] }),
"indent_left must be",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"line_spacing": 0 }] }),
"line_spacing must be",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "format_paragraph", "find": "x",
"line_spacing": 3000 }] }),
"line_spacing must be",
),
(
json!({ "action": "docx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_paragraph", "find": "x", "align": "center",
"list": "bullet", "level": 2 }] }),
"edits only a .docx/.docm file",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "set_cell", "sheet": "Values", "cell": "A1",
"number_format": "0.00" }] }),
"has no \"value\"",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "insert_text", "find": "x", "insert": "y",
"position": "middle" }] }),
"position",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "add_image", "image": "picture.png" }] }),
"after must not be empty",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "add_image", "after": "x", "image": "picture.png",
"width": 0 }] }),
"width must be",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "add_table", "after": "x" }] }),
"a table with no rows and no headers",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "add_table", "after": "x", "rows": [[]] }] }),
"a table with no rows and no headers",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "add_table", "after": "x", "rows": [wide_row] }] }),
"a table holds at most",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "set_cell", "sheet": "Values", "cell": "A9999999",
"value": 1 }] }),
"the row must be between",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "set_cell", "sheet": "Values", "cell": "ZZZ1",
"value": 1 }] }),
"the column must be between",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "set_cell", "sheet": "Values", "cell": "A0",
"value": 1 }] }),
"must be an A1 address",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "insert_row", "sheet": "Values", "row": 0 }] }),
"row must be a whole number",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "insert_column", "sheet": "Values", "column": "A1" }] }),
"column must be column letters",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "column": "A1",
"width_chars": 10 }] }),
"column must be column letters",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "column": "b",
"width_chars": 10 }] }),
"column must be column letters",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "row": 99_999_999,
"height_points": 20 }] }),
"row must be a whole number",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "bold": true }] }),
"names no target",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"column": "B", "bold": true }] }),
"names more than one target",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1" }] }),
"names no formatting",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"bold": "yes" }] }),
"must be a boolean",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"shade": "FF0000" }] }),
"is not a field of xlsx",
),
(
json!({ "action": "docx_edit", "path": "doc.docx",
"edits": [{ "op": "replace_text", "find": "x", "replace": "y",
"position": "after" }] }),
"is not a field of docx",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"align": "middle" }] }),
"align must be",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"border": { "leftish": true } }] }),
"is not a side",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"border": {} }] }),
"names no side",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"size": 500 }] }),
"size must be",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "column": "B",
"width_chars": 300 }] }),
"width_chars must be",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "row": 3,
"height_points": 500 }] }),
"height_points must be",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1:A999999",
"bold": true }] }),
"more than one edit formats",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"color": "red" }] }),
"color must be",
),
(
json!({ "action": "xlsx_edit", "path": "book.xlsx",
"edits": [{ "op": "format_cells", "sheet": "Values", "range": "A1",
"fill": "red" }] }),
"fill must be",
),
(
json!({ "action": "pptx_edit", "path": "deck.pptx",
"edits": [{ "op": "delete_slide", "slide": 0 }] }),
"slide must be a whole number",
),
(
json!({ "action": "docx_edit", "path": "doc.docx", "edits": too_many }),
"more than one call takes",
),
];
for (args, expected) in calls {
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("a call the tool cannot carry out");
assert!(
err.to_string().contains(expected),
"expected the refusal to name {expected}: {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[test]
fn edit_nulls_are_dropped_before_the_request_is_forwarded() {
use crate::ooxml::Family;
let docx = validate_edits(
Family::Docx,
&json!({ "edits": [
{ "op": "format_text", "find": "x", "bold": null, "size": 24 },
{ "op": "insert_text", "find": "x", "insert": "y", "position": null },
{ "op": "add_paragraph", "text": "z", "after": null },
]}),
)
.expect("valid docx edits");
assert_eq!(
docx,
json!([
{ "op": "format_text", "find": "x", "size": 24 },
{ "op": "insert_text", "find": "x", "insert": "y" },
{ "op": "add_paragraph", "text": "z" },
])
);
let xlsx = validate_edits(
Family::Xlsx,
&json!({ "edits": [
{ "op": "set_cell", "sheet": "S", "cell": "A1", "value": 1,
"number_format": null },
{ "op": "format_cells", "sheet": "S", "range": "A1",
"border": { "top": null, "left": true } },
]}),
)
.expect("valid xlsx edits");
assert_eq!(
xlsx,
json!([
{ "op": "set_cell", "sheet": "S", "cell": "A1", "value": 1 },
{ "op": "format_cells", "sheet": "S", "range": "A1",
"border": { "left": true } },
])
);
let pptx = validate_edits(
Family::Pptx,
&json!({ "edits": [
{ "op": "add_paragraph", "slide": 1, "text": "z", "after": null,
"level": null },
{ "op": "add_slide", "after": null, "title": null, "bullets": null },
]}),
)
.expect("valid pptx edits");
assert_eq!(
pptx,
json!([
{ "op": "add_paragraph", "slide": 1, "text": "z" },
{ "op": "add_slide" },
])
);
}
#[test]
fn the_schema_declares_the_shapes_the_boundary_accepts() {
use crate::ooxml::Family;
let docx = validate_edits(
Family::Docx,
&json!({ "edits": [{ "op": "format_paragraph", "find": "x", "list": false }] }),
)
.expect("list: false takes the paragraph's list away");
assert_eq!(
docx,
json!([{ "op": "format_paragraph", "find": "x", "list": false }])
);
let xlsx = validate_edits(
Family::Xlsx,
&json!({ "edits": [
{ "op": "format_cells", "sheet": "S", "range": "A1", "fill": false },
{ "op": "set_cell", "sheet": "S", "cell": "A1", "value": 42 },
{ "op": "set_cell", "sheet": "S", "cell": "A2",
"value": { "formula": "SUM(A1:A1)" } },
]}),
)
.expect("a false fill, a number and a formula object are all declared shapes");
assert_eq!(
xlsx,
json!([
{ "op": "format_cells", "sheet": "S", "range": "A1", "fill": false },
{ "op": "set_cell", "sheet": "S", "cell": "A1", "value": 42 },
{ "op": "set_cell", "sheet": "S", "cell": "A2",
"value": { "formula": "SUM(A1:A1)" } },
])
);
let fields = |action: &str| {
action_schema(action)["properties"][action]["properties"]["edits"]["items"]
["properties"]
.clone()
};
let docx_fields = fields("docx_edit");
assert_eq!(docx_fields["list"]["type"], json!(["string", "boolean"]));
assert!(
docx_fields["list"]["enum"]
.as_array()
.is_some_and(|values| values.contains(&json!(false))),
"the list enum must offer false beside the kinds: {}",
docx_fields["list"]
);
let xlsx_fields = fields("xlsx_edit");
assert_eq!(xlsx_fields["fill"]["anyOf"][0]["type"], json!("string"));
assert_eq!(xlsx_fields["fill"]["anyOf"][1]["enum"], json!([false]));
assert_eq!(
xlsx_fields["value"]["anyOf"][0]["type"],
json!(RULES.scalar_kinds)
);
assert_eq!(
xlsx_fields["value"]["anyOf"][1]["required"],
json!(["formula"])
);
}
#[test]
fn copy_extension_follows_the_content_when_the_name_does_not() {
use crate::ooxml::Family;
assert_eq!(
copy_extension(Path::new("report.docm"), Family::Docx),
"docm"
);
assert_eq!(
copy_extension(Path::new("report.docx"), Family::Docx),
"docx"
);
assert_eq!(copy_extension(Path::new("report"), Family::Docx), "docx");
assert_eq!(
copy_extension(Path::new("report.bin"), Family::Docx),
"docx"
);
assert_eq!(copy_extension(Path::new("sheet.xls"), Family::Xlsx), "xlsx");
assert_eq!(copy_extension(Path::new("deck.ppt"), Family::Pptx), "pptx");
}
#[test]
fn pptx_edit_refuses_a_key_its_op_does_not_take() {
let cases = [
(
json!([{ "op": "delete_slide", "slide": 1, "title": "x" }]),
"edits[0].title is not a field of pptx \"delete_slide\"",
),
(
json!([{ "op": "format_text", "slide": 1, "find": "a", "shadow": true }]),
"edits[0].shadow is not a field of pptx \"format_text\"",
),
];
for (edits, expected) in cases {
let err = validate_edits(crate::ooxml::Family::Pptx, &json!({ "edits": edits }))
.expect_err("a key the op does not take must be refused");
let message = err.to_string();
assert!(
message.contains("usage:") && message.contains(expected),
"expected {expected:?}, got: {message}"
);
}
}
#[test]
fn pptx_add_table_refuses_a_key_and_a_table_its_op_does_not_take() {
for (edits, expected) in [
(
json!([{ "op": "add_table", "slide": 1, "image": "x.png" }]),
"edits[0].image is not a field of pptx \"add_table\"",
),
(
json!([{ "op": "add_table", "slide": 1, "headers": [], "rows": [] }]),
"states a table with no rows and no headers",
),
] {
let err = validate_edits(crate::ooxml::Family::Pptx, &json!({ "edits": edits }))
.expect_err("a table the op cannot write must be refused");
let message = err.to_string();
assert!(
message.contains("usage:") && message.contains(expected),
"expected {expected:?}, got: {message}"
);
}
}
#[tokio::test]
async fn create_refuses_a_slide_size_another_format_cannot_have() {
let (_dir, ws) = workspace();
for (args, expected) in [
(
json!({ "action": "create", "format": "docx", "size": "widescreen",
"content": [{ "type": "paragraph", "text": "x" }] }),
"only format \"pptx\" has slides",
),
(
json!({ "action": "create", "format": "pptx", "size": "a4",
"content": [{ "type": "paragraph", "text": "x" }] }),
"unknown slide size \"a4\"",
),
] {
let err = confined_document()
.execute(&ws, args)
.await
.expect_err("a size no create takes");
assert!(
err.to_string().contains(expected),
"expected {expected:?}, got: {err}"
);
}
assert_eq!(generated_count(&ws), 0, "a refused call left an output");
}
#[test]
fn xlsx_edit_reads_a_null_key_as_absent_when_checking_its_op() {
let edits = validate_edits(
crate::ooxml::Family::Xlsx,
&json!({ "edits": [
{ "op": "format_cells", "sheet": "S", "range": "A1", "row": null,
"bold": true },
]}),
)
.expect("a null key must not be read as a key the op does not take");
assert_eq!(
edits,
json!([{ "op": "format_cells", "sheet": "S", "range": "A1", "bold": true }]),
"the null key was not dropped"
);
let err = validate_edits(
crate::ooxml::Family::Xlsx,
&json!({ "edits": [
{ "op": "format_cells", "sheet": "S", "range": "A1", "bold": true,
"weight": 2 },
]}),
)
.expect_err("a stated key the op does not take must still be refused");
assert!(
err.to_string()
.contains("edits[0].weight is not a field of xlsx \"format_cells\""),
"got: {err}"
);
}
#[tokio::test]
async fn a_block_refuses_a_key_its_kind_does_not_take() {
let (_dir, ws) = workspace();
let err = resolve_content(
&ws,
&json!({ "content": [{ "type": "paragraph", "text": "x", "level": 1 }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect_err("a paragraph block does not take a level");
assert!(
err.to_string().contains("usage:")
&& err
.to_string()
.contains("content[0].level is not a field of the paragraph block"),
"got: {err}"
);
let err = resolve_sheets(&json!({ "sheets": [{ "rows": [["x"]], "columns": 3 }] }))
.expect_err("a sheet does not take columns");
assert!(
err.to_string().contains("usage:")
&& err
.to_string()
.contains("sheets[0].columns is not a field of a sheet"),
"got: {err}"
);
resolve_content(
&ws,
&json!({ "content": [{ "type": "paragraph", "text": "ok" }] }),
"docx",
PathAccess::Workspace,
)
.await
.expect("a paragraph with only its own fields is a valid block");
resolve_sheets(&json!({ "sheets": [{ "name": "S", "rows": [["ok"]] }] }))
.expect("a named sheet with rows is a valid sheet");
}
#[test]
fn pptx_edit_format_text_refuses_every_unwritable_property() {
let cases = [
(
json!({ "op": "format_text", "slide": 1, "find": "a" }),
"needs at least one of bold",
),
(
json!({ "op": "format_text", "slide": 1, "find": "a", "size": 1e300 }),
"size must be",
),
(
json!({ "op": "format_text", "slide": 1, "find": "a", "size": 0.5 }),
"size must be",
),
(
json!({ "op": "format_text", "slide": 1, "find": "a", "color": "zzz" }),
"color must be",
),
(
json!({ "op": "format_text", "slide": 1, "find": "a", "align": "middle" }),
"align must be",
),
(
json!({ "op": "format_text", "slide": 1, "find": "a", "bold": "yes" }),
"must be a boolean",
),
];
for (edit, expected) in cases {
let err = validate_edits(crate::ooxml::Family::Pptx, &json!({ "edits": [edit] }))
.expect_err("an unwritable format_text must be refused");
assert!(
err.to_string().contains(expected),
"expected {expected:?}, got: {err}"
);
}
}
#[test]
fn naming_strips_extension_directory_and_markers() {
assert_eq!(sanitize_base(Some("report.docx"), "fallback"), "report");
assert_eq!(sanitize_base(Some("dir/sub/report"), "fallback"), "report");
assert_eq!(sanitize_base(Some("re[po]rt"), "fallback"), "re_po_rt");
assert_eq!(sanitize_base(Some(" "), "fallback"), "fallback");
assert_eq!(sanitize_base(Some("..."), "fallback"), "fallback");
}
#[test]
fn split_groups_refuses_a_request_over_the_page_cap() {
let err = split_groups(&json!({ "pages": ["1-6000", "6001-12000"] }))
.expect_err("a request over the cap must be refused");
assert!(
err.to_string().contains("too many pages"),
"unexpected error: {err}"
);
}
#[test]
fn the_shared_rules_parse_and_are_sane() {
let rules = &*RULES;
assert!(
!rules.heading_levels.is_empty(),
"no heading levels — every heading would be refused"
);
assert!(
rules
.heading_levels
.iter()
.all(|level| level.fract() == 0.0 && (1.0..=3.0).contains(level)),
"a heading level the writers cannot style"
);
assert!(!rules.scalar_kinds.is_empty(), "no scalar kinds");
assert!(!rules.image_extensions.is_empty(), "no image extensions");
assert!(!rules.cell_alignments.is_empty(), "no cell alignments");
assert!(!rules.cell_verticals.is_empty(), "no cell verticals");
assert!(!rules.cell_border_sides.is_empty(), "no cell border sides");
assert!(
!rules.paragraph_alignments.is_empty(),
"no paragraph alignments"
);
assert!(!rules.paragraph_lists.is_empty(), "no paragraph lists");
assert!(
rules.dimension_points.min < rules.dimension_points.max,
"an empty dimension range"
);
assert!(
rules.slide_position_fraction.min < rules.slide_position_fraction.max,
"an empty slide-position range"
);
assert!(
rules.slide_size_fraction.min < rules.slide_size_fraction.max,
"an empty slide-size range"
);
assert!(
rules.text_size_points.min < rules.text_size_points.max,
"an empty text-size range"
);
assert!(
rules.paragraph_indent_points.min < rules.paragraph_indent_points.max,
"an empty paragraph-indent range"
);
assert!(
rules.paragraph_spacing_points.min < rules.paragraph_spacing_points.max,
"an empty paragraph-spacing range"
);
assert!(
rules.paragraph_line_points.min < rules.paragraph_line_points.max,
"an empty paragraph-line range"
);
assert!(
rules.slide_text_size_points.min < rules.slide_text_size_points.max,
"an empty slide-text-size range"
);
assert!(
rules.cell_font_points.min < rules.cell_font_points.max,
"an empty cell-font-size range"
);
assert!(
rules.column_width_chars.min < rules.column_width_chars.max,
"an empty column-width range"
);
assert!(
rules.row_height_points.min < rules.row_height_points.max,
"an empty row-height range"
);
assert!(
rules.color_digits > 0 && rules.color_digits.is_multiple_of(3),
"a colour whose digits do not split into three channels"
);
assert!(
rules.degrees_step > 0 && rules.degrees_step < 360 && 360 % rules.degrees_step == 0,
"a step that leaves no rotation below a full turn"
);
assert!(
rules.sheet_name_max > 0,
"a sheet-name limit that refuses every name"
);
assert!(
!rules.sheet_name_forbidden.is_empty(),
"an empty forbidden set admits every character"
);
for (name, cap) in [
("edit_text_max", rules.edit_text_max),
("edits_max", rules.edits_max),
("bullets_max", rules.bullets_max),
("paragraph_level_max", rules.paragraph_level_max as usize),
("sheet_row_max", rules.sheet_row_max as usize),
("sheet_column_max", rules.sheet_column_max as usize),
("number_format_max", rules.number_format_max),
("font_name_max", rules.font_name_max),
("format_cells_max", rules.format_cells_max),
("paragraph_style_max", rules.paragraph_style_max),
("table_columns_max", rules.table_columns_max),
] {
assert!(cap > 0, "{name} refuses every value");
}
}
fn states_whole_number(text: &str, value: &str) -> bool {
if value.is_empty() {
return false;
}
let mut from = 0;
while let Some(at) = text[from..].find(value) {
let start = from + at;
let end = start + value.len();
let digit_before = text[..start]
.chars()
.next_back()
.is_some_and(|c| c.is_ascii_digit());
let digit_after = text[end..]
.chars()
.next()
.is_some_and(|c| c.is_ascii_digit());
if !digit_before && !digit_after {
return true;
}
from = start + 1;
}
false
}
fn schema_text(value: &Value) -> String {
match value {
Value::String(text) => text.clone(),
Value::Array(values) => values.iter().map(schema_text).collect::<Vec<_>>().join(" "),
Value::Object(object) => object
.values()
.map(schema_text)
.collect::<Vec<_>>()
.join(" "),
other => other.to_string(),
}
}
#[test]
fn the_description_states_every_rule_it_names() {
let description = confined_document().description();
assert!(
!description.contains("{{"),
"the description left a placeholder unreplaced:\n{description}"
);
let schema = schema_text(&confined_document().parameters_schema());
for value in [
listed(&RULES.heading_levels),
RULES.dimension_points.bounds(),
RULES.slide_position_fraction.bounds(),
RULES.slide_size_fraction.bounds(),
RULES.text_size_points.bounds(),
RULES.paragraph_indent_points.bounds(),
RULES.paragraph_spacing_points.bounds(),
RULES.paragraph_line_points.bounds(),
RULES.slide_text_size_points.bounds(),
RULES.cell_font_points.bounds(),
RULES.column_width_chars.bounds(),
RULES.row_height_points.bounds(),
RULES.color_digits.to_string(),
RULES.degrees_step.to_string(),
RULES.sheet_name_max.to_string(),
format!("{:?}", RULES.sheet_name_forbidden),
RULES.edit_text_max.to_string(),
RULES.edits_max.to_string(),
RULES.bullets_max.to_string(),
RULES.paragraph_level_max.to_string(),
RULES.paragraph_style_max.to_string(),
RULES.sheet_row_max.to_string(),
RULES.sheet_column_max.to_string(),
RULES.number_format_max.to_string(),
RULES.font_name_max.to_string(),
RULES.format_cells_max.to_string(),
RULES.table_columns_max.to_string(),
RULES.max_pages_per_request.to_string(),
megabytes(crate::util::FILE_MAX_BYTES).to_string(),
] {
assert!(
states_whole_number(&description, &value) || states_whole_number(&schema, &value),
"neither the description nor the schema states the rule it names — {value:?} is \
missing as a whole number from:\n{description}\n{schema}"
);
}
let default_deck = RULES.pptx_default_size.inches();
assert!(
schema.contains(&default_deck),
"the schema does not state the default deck's size {default_deck:?}:\n{schema}"
);
for word in pair_words(&RULES.paragraph_alignments)
.into_iter()
.chain(pair_words(&RULES.paragraph_lists))
.chain(pair_words(&RULES.slide_alignments))
.chain(RULES.cell_alignments.iter().map(String::as_str))
.chain(RULES.cell_verticals.iter().map(String::as_str))
.chain(RULES.cell_border_sides.iter().map(String::as_str))
.chain(RULES.pptx_sizes.iter().map(|size| size.name.as_str()))
{
assert!(
description.contains(word) || schema.contains(word),
"neither the description nor the schema names the accepted word {word:?}"
);
}
let marks = crate::docgen::ppt_marks();
let labels = crate::docgen::ppt_slide_labels();
let slide_label = crate::reader_output::slide_label(&labels.slide, "<n>");
let notes_label = crate::reader_output::slide_label(&labels.notes, "<n>");
let comments_label = crate::reader_output::slide_label(&labels.comments, "<n>");
for mark in [
&marks.title,
&marks.hidden_slide,
&marks.diagram_text,
&marks.diagram_text_lost,
&marks.no_text,
&slide_label,
¬es_label,
&comments_label,
] {
assert!(
description.contains(mark.as_str()),
"the description does not name the slide mark {mark:?}"
);
}
for mark in crate::docgen::docx_marks() {
assert!(
description.contains(mark.example.as_str()),
"the description does not name the Word mark {:?}",
mark.example
);
}
}
const BASELINE_DESCRIPTION: usize = 9_532;
const BASELINE_TOTAL: usize = 37_402;
#[test]
fn the_schema_is_compact_and_declares_every_action() {
let tool = confined_document();
let schema = tool.parameters_schema();
let entries = schema["properties"]["action"]["oneOf"]
.as_array()
.expect("the action list is a oneOf");
assert_eq!(entries.len(), ACTIONS.len(), "one entry per action");
for (entry, action) in entries.iter().zip(ACTIONS) {
assert_eq!(
entry["properties"][action]["type"], "object",
"{action} must be one entry with its own arguments"
);
assert_eq!(
entry["required"],
json!([action]),
"{action}'s entry must require its own name"
);
assert_eq!(
entry["additionalProperties"], false,
"an entry takes only the actions it names"
);
}
let description = tool.description().len();
assert!(
description <= BASELINE_DESCRIPTION,
"the rendered description is {description} bytes, more than the measured \
{BASELINE_DESCRIPTION}"
);
let size = serde_json::to_string(&schema)
.expect("the schema serializes")
.len();
assert!(
description + size <= BASELINE_TOTAL,
"the description ({description}) and serialized schema ({size}) together are {} bytes, \
more than the measured {BASELINE_TOTAL}",
description + size
);
}
#[test]
fn the_tool_is_offered_exactly_with_the_managed_runtime() {
let (_dir, ws) = workspace();
let (tools, _) = crate::agent::role_tools_and_specs(
crate::Role::Assistant,
&ws,
false,
std::sync::Arc::default(),
None,
);
assert_eq!(
tools.iter().any(|tool| tool.name() == "document"),
crate::tools::bun::bun_binary_path().is_some(),
"the tool must be offered exactly while the managed bun runtime is present"
);
}
#[test]
fn the_xlsx_edit_vocabulary_takes_the_new_sheet_shapes() {
use crate::ooxml::Family;
let edits = validate_edits(
Family::Xlsx,
&json!({ "edits": [
{ "op": "merge_cells", "sheet": "S", "range": "B2:C3" },
{ "op": "unmerge_cells", "sheet": "S", "range": "C3:B2" },
{ "op": "add_sheet", "name": "New" },
{ "op": "rename_sheet", "sheet": "S", "name": "Renamed" },
{ "op": "delete_sheet", "sheet": "S" },
{ "op": "move_sheet", "sheet": "S", "to": 2 },
{ "op": "add_image", "sheet": "S", "cell": "D4", "image": "a.png",
"width": null, "height": 40 },
] }),
)
.expect("every op the vocabulary names");
assert_eq!(
edits[6],
json!({ "op": "add_image", "sheet": "S", "cell": "D4", "image": "a.png",
"height": 40 }),
"a null side is absent"
);
for (edit, expected) in [
(
json!({ "op": "merge_cells", "sheet": "S", "range": "A1" }),
"covers one cell",
),
(
json!({ "op": "add_sheet", "name": "a:b" }),
"must not contain",
),
(
json!({ "op": "move_sheet", "sheet": "S", "to": 0 }),
"at least 1",
),
(
json!({ "op": "add_image", "sheet": "S", "cell": "nope", "image": "a.png" }),
"A1 address",
),
] {
let err = validate_edits(Family::Xlsx, &json!({ "edits": [edit] }))
.expect_err("a shape the writer cannot render");
assert!(err.to_string().contains(expected), "{expected}: {err}");
}
}
}