bokuweb / docx-rs

:memo: A .docx file writer with Rust/WebAssembly.
https://bokuweb.github.io/docx-rs/
MIT License
341 stars 59 forks source link

`unreachable!` called when `.build()` is called on a document. #750

Open JamieMair opened 1 month ago

JamieMair commented 1 month ago

Describe the bug

The code hits an unreachable! block in the source code when running .build(), here is the stack trace:

internal error: entered unreachable code
stack backtrace:
   0: rust_begin_unwind
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:146:5
   3: <docx_rs::documents::elements::run::Run as docx_rs::documents::build_xml::BuildXML>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/run.rs:339:49
   4: <docx_rs::documents::elements::paragraph::ParagraphChild as docx_rs::documents::build_xml::BuildXML>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/paragraph.rs:47:39
   5: docx_rs::xml_builder::XMLBuilder::add_child
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/xml_builder/mod.rs:84:19
   6: docx_rs::xml_builder::XMLBuilder::add_children
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/xml_builder/mod.rs:111:20
   7: <docx_rs::documents::elements::paragraph::Paragraph as docx_rs::documents::build_xml::BuildXML>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/paragraph.rs:492:9
   8: docx_rs::documents::elements::paragraph::<impl docx_rs::documents::build_xml::BuildXML for alloc::boxed::Box<docx_rs::documents::elements::paragraph::Paragraph>>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/paragraph.rs:503:9
   9: <docx_rs::documents::elements::structured_data_tag::StructuredDataTagChild as docx_rs::documents::build_xml::BuildXML>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/structured_data_tag.rs:43:53
  10: docx_rs::xml_builder::XMLBuilder::add_child
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/xml_builder/mod.rs:84:19
  11: docx_rs::xml_builder::XMLBuilder::add_children
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/xml_builder/mod.rs:111:20
  12: docx_rs::documents::elements::structured_data_tag::StructuredDataTag::inner_build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/structured_data_tag.rs:152:9
  13: docx_rs::documents::elements::structured_data_tag::<impl docx_rs::documents::build_xml::BuildXML for alloc::boxed::Box<docx_rs::documents::elements::structured_data_tag::StructuredDataTag>>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/elements/structured_data_tag.rs:171:9
  14: docx_rs::xml_builder::XMLBuilder::add_child
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/xml_builder/mod.rs:84:19
  15: <docx_rs::documents::footer::Footer as docx_rs::documents::build_xml::BuildXML>::build
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/footer.rs:91:58
  16: docx_rs::documents::Docx::build::{{closure}}
             at /home/jamie/.cargo/git/checkouts/docx-rs-cc108461f2f164ad/93eb2e1/docx-core/src/documents/mod.rs:598:22

Reproduced step

Example doc (blank): example_doc.docx

  1. Create a new project and download the example_doc.docx file and place it next to main.rs
  2. Add docx-rs to Cargo.toml of a new binary project (I have tried latest and current git branch with docx-rs = { git = "https://github.com/bokuweb/docx-rs.git" } in Cargo.toml).
  3. Write the following code in main.rs

    use std::io::Cursor;
    static DOC_BYTES: &[u8] = include_bytes!("example_doc.docx");
    
    fn main() {
      let document = docx_rs::read_docx(DOC_BYTES).unwrap();
      // Compile the docx into a zip file in the memory buffer
      let mut output_buffer = Cursor::new(Vec::new());
      // Compile the docx into a zip file in the memory buffer
      document.build().pack(&mut output_buffer).unwrap();
    
      let bytes: Vec<u8> = output_buffer.into_inner();
      format!("Output bytes: {} bytes", bytes.len());
    }
  4. Build and run, which errors.

Expected behavior

The program to run correctly, with the produced document being identical (or almost) to the input document. This document should be stored in bytes within the bytes variable.

Actual behavior

A panic happens when running .build().

Screenshots

N/A

Desktop (please complete the following information)