Skip to main content

reassemble

Function reassemble 

Source
pub fn reassemble(
    collection: &PartCollection,
) -> Result<ReassembledFile, MultiUuError>
Expand description

Reassemble a multi-part UU-encoded file from its parts.

Iterates over all PartEntry values with part_number >= 1 in ascending order (the PartCollection’s BTreeMap guarantees this). Each part’s body_bytes is independently decoded via uuencoding::decode and the decoded payloads are concatenated. filename and mode are taken from the first part only.

The TOC part (part_number = 0), if present, is silently ignored.

§Errors

  • MultiUuError::EmptyCollection — the collection contains no parts with part_number >= 1 (including the case of a TOC-only collection).
  • MultiUuError::DecodeErroruuencoding::decode returned an error for one of the parts. Reassembly stops at the first failing part.

§Partial results

When parts are missing the function still returns Ok rather than an error. The result has is_truncated = true and missing_parts listing the absent part numbers. data contains the decoded bytes of only the present parts concatenated in order.

This is not a contiguous file region. The bytes from the missing parts are absent, so the data does not correspond to a valid byte range within the original file. Do not write this to disk as a complete file. It is suitable for diagnostics only. Call PartCollection::is_complete() before reassembling if you require a usable result.

§Never panics

This function never panics. The expect on the internal get() call is unreachable by construction: present_parts() only yields numbers that are keys in the underlying map.

§Security

The decoded data may be a compressed archive. Any subsequent decompression is the caller’s responsibility and must be independently guarded against decompression-bomb attacks. This crate does not decompress.

The filename field of the returned ReassembledFile comes from the email subject or UU begin line and is not sanitised. Sanitise it before using it as a filesystem path to prevent directory traversal attacks.

§Example

use uuencoding_multi::{PartCollection, PartEntry, reassemble};

// In practice `body_bytes` comes from message parts fetched from NNTP
// or a mailbox; `no_run` is used here because constructing valid UU
// bodies inline is verbose.
let mut coll = PartCollection::with_total(2);
coll.add(PartEntry { part_number: 1, body_bytes: todo!(), subject: None }).unwrap();
coll.add(PartEntry { part_number: 2, body_bytes: todo!(), subject: None }).unwrap();

let file = reassemble(&coll).unwrap();
assert!(!file.is_truncated);
// Apply size limits before decompressing file.data.
println!("{}: {} bytes", file.filename, file.data.len());