pub struct Assembler { /* private fields */ }Expand description
Reassembles multi-part yEnc articles into a complete file.
The caller:
- Creates an
Assemblerwith the total file size (from=ybegin size=). - Calls
add_partfor each decoded part as it arrives (in any order). If a part carries awhole_file_crc32, the assembler records it automatically for use atfinish()time — no separate call toset_expected_crc32is needed. - Polls
is_completeand callsfinishwhen all parts have arrived.
§Byte offset convention
yEnc uses 1-based byte offsets in =ypart begin=/end=. The begin
and end values on DecodedPart reflect the raw 1-based values from the
article. Assembler converts them to 0-based offsets internally.
The missing_ranges() return value uses 0-based ranges.
Implementations§
Source§impl Assembler
impl Assembler
Sourcepub fn new(total_size: u64) -> Result<Self, AssemblyError>
pub fn new(total_size: u64) -> Result<Self, AssemblyError>
Create a new assembler for a file of exactly total_size bytes.
total_size must match the size= field on the =ybegin line of all
articles in the series.
§Errors
Returns AssemblyError::TotalSizeTooLarge if total_size exceeds
MAX_TOTAL_SIZE (512 MiB) or cannot be represented as a usize on
the current platform (e.g. > 4 GiB on 32-bit targets).
Sourcepub fn set_expected_crc32(&mut self, crc32: u32)
pub fn set_expected_crc32(&mut self, crc32: u32)
Set the expected whole-file CRC32.
This is optional. If set, finish verifies the
reassembled bytes against this value and returns
AssemblyError::CrcMismatch on mismatch.
You can extract the whole-file CRC32 directly from a decoded part:
DecodedPart::whole_file_crc32 carries the crc32= field from =yend
(distinct from pcrc32=, which is per-part). Not every encoder includes
it; check DecodedPart::whole_file_crc32.is_some() to determine whether
it is available.
Sourcepub fn add_part(&mut self, part: &DecodedPart) -> Result<(), AssemblyError>
pub fn add_part(&mut self, part: &DecodedPart) -> Result<(), AssemblyError>
Add a decoded yEnc part to the assembler.
part.part_begin and part.part_end (1-based byte offsets from
=ypart begin=/end=) determine where the decoded bytes are written
into the file buffer.
If part.whole_file_crc32 is Some, this method automatically records
it as the expected whole-file CRC32 (equivalent to calling
set_expected_crc32). If a previous part
already recorded a different CRC, Err(AssemblyError::InconsistentCrc) is
returned and the part is not added.
§Errors
AssemblyError::OutOfRange— the part’s byte range extends beyondtotal_sizeor the begin/end offsets are missing.AssemblyError::OverlappingPart— the part’s range overlaps with a previously added part.AssemblyError::DataLengthMismatch— the part’s decoded data length does not match its declaredbegin/endrange.AssemblyError::InconsistentCrc— this part’swhole_file_crc32conflicts with the CRC already recorded from a previous part.
§Per-part CRC and crc32_verified
yencoding::decode() performs per-part CRC verification itself: if a
pcrc32= field is present in =yend and the decoded bytes do not match,
decode() returns Err(YencError::CrcMismatch) and the corrupt part
never reaches this function. A DecodedPart that arrives here with
crc32_verified = false means only that no per-part CRC was present
in the article (e.g. an older encoder that omitted pcrc32=), not that a
CRC check failed silently.
§Notes
Parts may be added in any order. A part with no part_begin/part_end
(i.e. a single-part article passed to the assembler) is written starting
at offset 0.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Returns true iff all bytes in [0, total_size) are covered.
When total_size is 0 this always returns true (a zero-byte file
needs no parts).
Sourcepub fn missing_ranges(&self) -> Vec<Range<u64>>
pub fn missing_ranges(&self) -> Vec<Range<u64>>
Returns the 0-based byte ranges within [0, total_size) not yet covered
by any added part, in ascending order.
An empty Vec means the file is complete.
Sourcepub fn finish(self) -> Result<Vec<u8>, AssemblyError>
pub fn finish(self) -> Result<Vec<u8>, AssemblyError>
Finish assembly and return the reassembled file bytes.
Verifies that all byte ranges are covered. If an expected CRC32 was
set via set_expected_crc32, also verifies
the whole-file CRC32.
§Errors
AssemblyError::Incomplete— some byte ranges are not yet covered.AssemblyError::CrcMismatch— CRC32 mismatch (only when an expected CRC was set).
§Integrity note
If any part supplied to add_part carried a
whole_file_crc32, the CRC was automatically extracted and will be verified here.
Call set_expected_crc32 only when you have the expected
CRC from an out-of-band source (e.g., an NZB file) that the parts themselves did not
carry. If no expected CRC was registered by either path, the data is returned without
CRC verification and you should use your own integrity check.
On success the assembler is consumed and the buffer is returned without copying.
Sourcepub fn total_size(&self) -> u64
pub fn total_size(&self) -> u64
Total declared file size in bytes.