docx: set_application_name for apps that embed the writer

An app that ships the writer under its own name names itself in docProps/app.xml
(<Application>); "WordCraft" until set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
StDenis1980andClaude Opus 5.5 committed 2026-11-16 14:37:37 +01:00
1 parent 6ef2c053df
commit 0b0e5accd5
3 files changed
+33 -1

No files matched your search

+13
View File
@@ -20,6 +20,19 @@ mod custom;
mod flat;
mod fonts;
mod package;
static APPLICATION_NAME: std::sync::OnceLock<String> = std::sync::OnceLock::new();
/// Names the application written to `docProps/app.xml` (`<Application>`), for an app that
/// embeds this writer under its own name; "WordCraft" until set. Only the first call counts.
pub fn set_application_name(name: &str) {
let _ = APPLICATION_NAME.set(name.to_owned());
}
/// The application `docProps/app.xml` names (see [`set_application_name`]).
pub fn application_name() -> &'static str {
APPLICATION_NAME.get().map_or("WordCraft", String::as_str)
}
mod read;
mod smart_art;
#[cfg(test)]
+1 -1
View File
@@ -349,7 +349,7 @@ pub(super) fn app_xml(doc: &Document) -> Vec<u8> {
("xmlns:vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"),
],
);
w.leaf("Application", &[], "WordCraft");
w.leaf("Application", &[], crate::application_name());
w.leaf("Words", &[], &doc.word_count().to_string());
w.leaf("Paragraphs", &[], &doc.paragraph_count().to_string());
w.leaf("DocSecurity", &[], "0");
+19
View File
@@ -0,0 +1,19 @@
//! An app that embeds the writer names itself in `docProps/app.xml` (its own test binary: the
//! name is set once per process).
use std::io::Read;
#[test]
fn the_embedding_app_names_itself_in_app_xml() {
assert_eq!(craft_docx::application_name(), "WordCraft", "the default");
craft_docx::set_application_name("Example Writer");
craft_docx::set_application_name("Other");
assert_eq!(craft_docx::application_name(), "Example Writer", "only the first call counts");
let mut d = craft_wordmodel::Document::new();
d.body = vec![craft_wordmodel::para_block(craft_wordmodel::Paragraph::with_text("Text", Default::default()))];
let bytes = craft_docx::write(&d).unwrap();
let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
let mut app = String::new();
z.by_name("docProps/app.xml").unwrap().read_to_string(&mut app).unwrap();
assert!(app.contains("<Application>Example Writer</Application>"), "{app}");
}