Skip to main content

Assembler

Struct Assembler 

Source
pub struct Assembler { /* private fields */ }
Expand description

Reassembles multi-part yEnc articles into a complete file.

The caller:

  1. Creates an Assembler with the total file size (from =ybegin size=).
  2. Calls add_part for each decoded part as it arrives (in any order). If a part carries a whole_file_crc32, the assembler records it automatically for use at finish() time — no separate call to set_expected_crc32 is needed.
  3. Polls is_complete and calls finish when 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

Source

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).

Source

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.

Source

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
§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.

Source

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).

Source

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.

Source

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
§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.

Source

pub fn total_size(&self) -> u64

Total declared file size in bytes.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.