mod discovery;
pub use discovery::{discovery, discovery_text};
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Capabilities {
pub schema_version: u32,
pub engine_version: String,
pub runtime: String,
pub operations: Operations,
pub edit_plan_versions: Vec<u32>,
pub edit_operations: Vec<String>,
pub limits: Limits,
#[serde(default)]
pub audit_rules: Vec<String>,
#[serde(default)]
pub validate_rules: Vec<String>,
#[serde(default)]
pub formats: BTreeMap<String, Vec<String>>,
#[serde(default)]
pub ids: Ids,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub settlement_options: BTreeMap<String, String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ids {
pub change: Vec<String>,
pub comment: Vec<String>,
#[serde(default)]
pub comment_text: Vec<String>,
pub paragraph: Vec<String>,
pub table: Vec<String>,
#[serde(default)]
pub control: Vec<String>,
pub note: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Operations {
pub compare: bool,
pub accept_revisions: bool,
pub reject_revisions: bool,
#[serde(default)]
pub selective_revisions: bool,
pub revision_records: bool,
pub pdf: bool,
pub png: bool,
pub inspect_body: bool,
pub markdown: bool,
pub edit: bool,
pub comments: bool,
#[serde(default)]
pub markdown_to_docx: bool,
#[serde(default)]
pub markdown_diff: bool,
#[serde(default)]
pub patch: bool,
#[serde(default)]
pub diff_render: bool,
#[serde(default)]
pub page_ranges: bool,
#[serde(default)]
pub comment_threads: bool,
#[serde(default)]
pub edit_keeps_revisions: bool,
#[serde(default)]
pub append: bool,
#[serde(default)]
pub content_controls: bool,
#[serde(default)]
pub validate: bool,
#[serde(default)]
pub repair: bool,
#[serde(default)]
pub fields: bool,
#[serde(default)]
pub scrub: bool,
#[serde(default)]
pub audit: bool,
#[serde(default)]
pub edit_skip_failed: bool,
}
impl Operations {
pub fn entries(&self) -> Vec<(&'static str, bool)> {
let Self {
compare,
accept_revisions,
reject_revisions,
selective_revisions,
revision_records,
pdf,
png,
inspect_body,
markdown,
edit,
comments,
markdown_to_docx,
markdown_diff,
patch,
diff_render,
page_ranges,
comment_threads,
edit_keeps_revisions,
append,
content_controls,
validate,
repair,
fields,
scrub,
audit,
edit_skip_failed,
} = self;
vec![
("compare", *compare),
("accept_revisions", *accept_revisions),
("reject_revisions", *reject_revisions),
("selective_revisions", *selective_revisions),
("revision_records", *revision_records),
("pdf", *pdf),
("png", *png),
("inspect_body", *inspect_body),
("markdown", *markdown),
("edit", *edit),
("comments", *comments),
("markdown_to_docx", *markdown_to_docx),
("markdown_diff", *markdown_diff),
("patch", *patch),
("diff_render", *diff_render),
("page_ranges", *page_ranges),
("comment_threads", *comment_threads),
("edit_keeps_revisions", *edit_keeps_revisions),
("append", *append),
("content_controls", *content_controls),
("validate", *validate),
("repair", *repair),
("fields", *fields),
("scrub", *scrub),
("audit", *audit),
("edit_skip_failed", *edit_skip_failed),
]
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Limits {
pub stories: Vec<String>,
pub plain_text_runs: bool,
pub refuses_opaque_ranges: bool,
pub reads_legacy_doc: bool,
#[serde(default)]
pub input: InputBudget,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputBudget {
pub max_compressed_bytes: u64,
pub max_entries: usize,
pub max_part_bytes: u64,
pub max_uncompressed_bytes: u64,
pub max_xml_depth: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_xml_elements: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_story_units: Option<u64>,
}
impl From<crate::admission::InputLimits> for InputBudget {
fn from(l: crate::admission::InputLimits) -> Self {
Self {
max_compressed_bytes: l.max_compressed_bytes,
max_entries: l.max_entries,
max_part_bytes: l.max_part_bytes,
max_uncompressed_bytes: l.max_uncompressed_bytes,
max_xml_depth: l.max_xml_depth,
max_xml_elements: Some(l.max_xml_elements),
max_story_units: Some(l.max_story_units),
}
}
}
pub const OUTPUT_FORMATS: &[(&str, &[&str])] = &[
("compare", &["docx", "md", "pdf", "png", "json"]),
("convert", &["docx", "md", "pdf", "png", "json"]),
("read", &["md", "json"]),
("diff", &["md"]),
("accept", &["docx", "md", "pdf", "png", "json"]),
("reject", &["docx", "md", "pdf", "png", "json"]),
("append", &["docx", "md", "pdf", "png", "json"]),
("scrub", &["docx", "md", "pdf", "png", "json"]),
("edit", &["docx"]),
("add", &["docx"]),
];
pub(crate) const DIFF_EXTENSIONS: &[&str] = &["md", "markdown", "txt", "patch", "diff"];
pub(crate) const IDS_NOTE: &str = "body ids print without a story (r12); every other story prints \
{story}.r{id} (header1.r1, footnotes.r3); rows and cells sit under their table \
(t0.row1.cell2); every id is safe unquoted in bash, csh and zsh";
fn dispatches_v2(runtime: &str) -> bool {
matches!(runtime, "cli" | "rust")
}
fn skips_failed(runtime: &str) -> bool {
matches!(runtime, "cli" | "rust")
}
fn dispatched_plan_versions(runtime: &str) -> Vec<u32> {
if dispatches_v2(runtime) {
vec![crate::inspect::SCHEMA_VERSION, 2]
} else {
vec![crate::inspect::SCHEMA_VERSION]
}
}
#[must_use]
pub fn capabilities(runtime: &str) -> Capabilities {
Capabilities {
schema_version: 1,
engine_version: env!("CARGO_PKG_VERSION").to_string(),
runtime: runtime.to_string(),
operations: Operations {
compare: true,
accept_revisions: true,
reject_revisions: true,
selective_revisions: true,
revision_records: true,
pdf: true,
png: true,
inspect_body: true,
markdown: true,
edit: true,
comments: true,
markdown_to_docx: true,
markdown_diff: true,
patch: true,
diff_render: true,
page_ranges: true,
comment_threads: true,
edit_keeps_revisions: true,
append: true,
content_controls: true,
validate: true,
repair: true,
fields: true,
scrub: true,
audit: true,
edit_skip_failed: skips_failed(runtime),
},
edit_plan_versions: dispatched_plan_versions(runtime),
edit_operations: crate::edit::dispatched_operation_kinds(),
limits: Limits {
stories: std::iter::once(crate::inspect::BODY_STORY)
.chain(crate::inspect::STORY_KINDS.iter().copied())
.map(str::to_string)
.collect(),
plain_text_runs: false,
refuses_opaque_ranges: true,
reads_legacy_doc: false,
input: crate::admission::InputLimits::default().into(),
},
audit_rules: crate::audit::RULES
.iter()
.map(|rule| rule.0.to_string())
.collect(),
validate_rules: crate::validate::RULE_SETS
.iter()
.map(|set| set.to_string())
.collect(),
formats: OUTPUT_FORMATS
.iter()
.map(|(task, names)| {
let names = if *task == "diff" {
DIFF_EXTENSIONS
} else {
names
};
let extensions = names.iter().map(|name| format!(".{name}")).collect();
((*task).to_string(), extensions)
})
.collect(),
ids: Ids {
change: strings(crate::ids::CHANGE_SPELLINGS),
comment: strings(crate::ids::COMMENT_SPELLINGS),
comment_text: strings(crate::ids::COMMENT_TEXT_SPELLINGS),
paragraph: strings(crate::ids::PARAGRAPH_SPELLINGS),
table: strings(crate::ids::TABLE_SPELLINGS),
control: strings(crate::ids::CONTROL_SPELLINGS),
note: IDS_NOTE.to_string(),
},
settlement_options: if runtime == "cli" {
[("--keep-nested-comment".into(), "accept / reject; convert with --track-changes accept|reject: keep wholly removed comment threads on empty anchors (default: remove)".into())].into()
} else {
BTreeMap::new()
},
}
}
fn strings(values: &[&str]) -> Vec<String> {
values.iter().map(|value| (*value).to_string()).collect()
}
#[must_use]
pub fn capabilities_text(c: &Capabilities) -> String {
let yes = |b: bool| if b { "yes" } else { "no" };
let mut out = format!("engine: jubarte {} ({})\n", c.engine_version, c.runtime);
for (name, has) in c.operations.entries() {
out.push_str(&format!("{name}: {}\n", yes(has)));
}
let versions: Vec<String> = c.edit_plan_versions.iter().map(u32::to_string).collect();
out.push_str(&format!("edit plan versions: {}\n", versions.join(", ")));
out.push_str(&format!(
"edit operations: {}\n",
c.edit_operations.join(", ")
));
out.push_str(&format!("stories: {}\n", c.limits.stories.join(", ")));
let input = &c.limits.input;
out.push_str(&format!(
"limits: plain text runs {}, refuses opaque ranges {}, reads legacy .doc {}, \
{} bytes compressed, {} entries, {} bytes per part, {} bytes uncompressed, \
XML depth {}, {}, {}\n",
yes(c.limits.plain_text_runs),
yes(c.limits.refuses_opaque_ranges),
yes(c.limits.reads_legacy_doc),
input.max_compressed_bytes,
input.max_entries,
input.max_part_bytes,
input.max_uncompressed_bytes,
input.max_xml_depth,
input.max_xml_elements.map_or_else(
|| "no XML element limit".to_string(),
|n| format!("{n} XML elements")
),
input.max_story_units.map_or_else(
|| "no story unit limit".to_string(),
|n| format!("{n} story units per compared side")
)
));
let rules: Vec<String> = c
.validate_rules
.iter()
.map(|rule| {
if rule == "word" {
format!("{rule} (default)")
} else {
rule.clone()
}
})
.collect();
out.push_str(&format!("validate rules: {}\n", rules.join(", ")));
out.push_str(&format!("audit codes: {}\n", c.audit_rules.join(", ")));
let formats: Vec<String> = OUTPUT_FORMATS
.iter()
.filter_map(|(task, _)| {
let extensions = c.formats.get(*task)?;
Some(format!("{task} {}", extensions.join(" ")))
})
.collect();
out.push_str(&format!("formats: {}\n", formats.join("; ")));
out.push_str(&format!(
"ids: revisions {}; comments {}; comment text {}; paragraphs {}; tables {}; controls {}\n",
c.ids.change.join(", "),
c.ids.comment.join(", "),
c.ids.comment_text.join(", "),
c.ids.paragraph.join(", "),
c.ids.table.join(", "),
c.ids.control.join(", ")
));
out.push_str(&format!("ids note: {}\n", c.ids.note));
for (flag, meaning) in &c.settlement_options {
out.push_str(&format!("{flag}: {meaning}\n"));
}
out.push_str("run with --json for the manifest\n");
out
}
#[must_use]
pub fn capabilities_json(runtime: &str) -> String {
serde_json::to_string_pretty(&capabilities(runtime)).expect("manifest serializes")
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
use super::*;
#[test]
fn an_older_manifest_has_no_element_limit() {
let older: InputBudget = serde_json::from_str(
r#"{"max_compressed_bytes":1,"max_entries":2,"max_part_bytes":3,"max_uncompressed_bytes":4,"max_xml_depth":5}"#,
)
.unwrap();
assert_eq!(older.max_xml_elements, None);
assert_eq!(older.max_story_units, None);
assert!(
!serde_json::to_string(&older)
.unwrap()
.contains("max_xml_elements")
);
let today = InputBudget::from(crate::admission::InputLimits::default());
assert_eq!(today.max_xml_elements, Some(2_000_000));
assert_eq!(today.max_story_units, Some(2_000_000));
}
#[test]
fn manifest_reports_the_crate_version_and_every_edit_kind() {
let c = capabilities("rust");
assert_eq!(c.engine_version, env!("CARGO_PKG_VERSION"));
assert_eq!(c.runtime, "rust");
assert_eq!(c.edit_plan_versions, [1, 2]);
assert_eq!(c.edit_operations.len(), 26);
assert!(c.operations.fields);
for op in ["insert_toc", "redact", "settings", "strike", "restore"] {
assert!(c.edit_operations.iter().any(|o| o == op), "{op}");
}
assert!(c.edit_operations.iter().any(|kind| kind == "rewrite"));
assert!(
c.edit_operations
.iter()
.any(|kind| kind == "delete_comment")
);
assert!(c.operations.comment_threads);
assert!(c.edit_operations.iter().any(|kind| kind == "fill_control"));
assert!(c.operations.content_controls);
assert!(c.operations.markdown_to_docx && c.operations.markdown_diff);
let json: serde_json::Value = serde_json::from_str(&capabilities_json("cli")).unwrap();
assert_eq!(json["runtime"], "cli");
assert_eq!(json["operations"]["png"], true);
assert_eq!(json["operations"]["comment_threads"], true);
assert_eq!(json["operations"]["selective_revisions"], true);
assert_eq!(json["operations"]["diff_render"], true);
assert_eq!(json["operations"]["page_ranges"], true);
assert_eq!(json["operations"]["edit_keeps_revisions"], true);
assert_eq!(json["operations"]["scrub"], true);
assert_eq!(json["operations"]["append"], true);
assert_eq!(json["limits"]["reads_legacy_doc"], false);
assert_eq!(json["limits"]["input"]["max_entries"], 10_000);
assert_eq!(json["limits"]["input"]["max_xml_depth"], 256);
let back: Capabilities = serde_json::from_value(json).unwrap();
assert_eq!(
back.limits.stories,
["body", "header", "footer", "footnotes", "endnotes"]
);
assert!(back.operations.validate && back.operations.repair);
assert_eq!(back.limits.input.max_part_bytes, 64 * 1024 * 1024);
}
}