pub struct FileChunk { /* private fields */ }Expand description
Represents an immutable chunk of file data for processing
This is a Value Object in Domain-Driven Design terms - it represents data without identity that cannot be modified once created. Any “changes” create new instances, ensuring data integrity and preventing accidental mutations during processing.
§Key Features
- Immutability: Once created, chunks cannot be modified
- Unique Identity: Each chunk has a UUID for tracking and identification
- Sequence Ordering: Maintains sequence numbers for proper file reassembly
- Integrity Verification: Optional checksums for data integrity validation
- Metadata Tracking: Creation timestamps and processing metadata
§Design Principles
- Value Object: Compared by value, not identity
- Self-Validation: Validates its own data integrity
- Builder Pattern: Use methods like
with_checksum()for modifications - Thread Safety: Fully thread-safe due to immutability
§Examples
§Developer Notes
- Use builder methods like
with_checksum()to create modified versions - Processing stages should create new chunks rather than modifying existing ones
- This design prevents data corruption and ensures thread safety
Implementations§
Source§impl FileChunk
impl FileChunk
Sourcepub fn new(
sequence_number: u64,
offset: u64,
data: Vec<u8>,
is_final: bool,
) -> Result<Self, PipelineError>
pub fn new( sequence_number: u64, offset: u64, data: Vec<u8>, is_final: bool, ) -> Result<Self, PipelineError>
Creates a new file chunk
§Purpose
Creates an immutable file chunk value object for pipeline processing. Chunks are the fundamental unit of file processing in the adaptive pipeline.
§Why
File chunking enables:
- Parallel processing of large files
- Memory-efficient streaming
- Independent processing units
- Granular error recovery
§Arguments
sequence_number- The order of this chunk in the file (0-based)offset- Byte offset in the original file where this chunk startsdata- The actual chunk data bytes (must not be empty)is_final- Whether this is the last chunk in the file
§Returns
Ok(FileChunk)- Successfully created chunk with unique UUIDErr(PipelineError::InvalidChunk)- Data is empty
§Errors
Returns PipelineError::InvalidChunk when data is empty.
§Side Effects
- Generates new UUID for chunk identification
- Sets creation timestamp to current UTC time
- Calculates chunk size from data length
§Examples
§Developer Notes
- Each chunk gets a unique UUID for tracking across pipeline stages
- Chunk size is automatically validated against system limits
- Checksum is initially None - use
with_calculated_checksum()to add - This is a Value Object - create new instances for “changes”
Sourcepub fn new_with_checksum(
sequence_number: u64,
offset: u64,
data: Vec<u8>,
checksum: String,
is_final: bool,
) -> Result<Self, PipelineError>
pub fn new_with_checksum( sequence_number: u64, offset: u64, data: Vec<u8>, checksum: String, is_final: bool, ) -> Result<Self, PipelineError>
Creates a new file chunk with checksum
§Developer Notes
- This is a convenience constructor for chunks that already have checksums
- Prefer using
new()followed bywith_checksum()for clarity
Sourcepub fn sequence_number(&self) -> u64
pub fn sequence_number(&self) -> u64
Gets the sequence number
Sourcepub fn created_at(&self) -> DateTime<Utc>
pub fn created_at(&self) -> DateTime<Utc>
Gets the creation timestamp
Sourcepub fn with_data(&self, data: Vec<u8>) -> Result<Self, PipelineError>
pub fn with_data(&self, data: Vec<u8>) -> Result<Self, PipelineError>
Creates a new FileChunk with updated data
§Developer Notes
- This creates a completely new chunk instance
- The old chunk remains unchanged (immutability)
- Checksum is cleared since data changed
- Use this pattern:
let new_chunk = old_chunk.with_data(new_data).unwrap();
Sourcepub fn with_checksum(&self, checksum: String) -> Self
pub fn with_checksum(&self, checksum: String) -> Self
Creates a new FileChunk with a checksum
§Developer Notes
- This preserves all other data and adds/updates the checksum
- Use this after processing:
let verified_chunk = chunk.with_checksum(hash);
Sourcepub fn with_calculated_checksum(&self) -> Result<Self, PipelineError>
pub fn with_calculated_checksum(&self) -> Result<Self, PipelineError>
Creates a new FileChunk with calculated SHA-256 checksum
§Developer Notes
- Calculates SHA-256 hash of current data
- Returns new chunk with checksum set
- Original chunk remains unchanged
Sourcepub fn without_data(&self) -> Self
pub fn without_data(&self) -> Self
Creates a new FileChunk without data (for security)
§Developer Notes
- Creates new chunk with empty data vector
- Useful for secure cleanup while preserving metadata
- Checksum is cleared since data is gone
Sourcepub fn verify_integrity(&self) -> Result<bool, PipelineError>
pub fn verify_integrity(&self) -> Result<bool, PipelineError>
Verifies the chunk integrity using the stored checksum
§Purpose
Validates that chunk data has not been corrupted by comparing the stored SHA-256 checksum against a freshly calculated hash of the current data.
§Why
Integrity verification provides:
- Detection of data corruption during processing or storage
- Confidence in pipeline operations
- Early error detection before expensive operations
- Compliance with data integrity requirements
§Returns
Ok(true)- Checksum matches, data is intactOk(false)- Checksum mismatch, data corruptedErr(PipelineError::InvalidChunk)- No checksum available
§Errors
Returns PipelineError::InvalidChunk when the chunk has no stored
checksum.
§Examples
§Developer Notes
- This method is read-only and doesn’t modify the chunk
- Use before critical processing to ensure data integrity
- Consider verification before expensive operations like encryption
Sourcepub fn calculate_checksum(&self) -> Result<String, PipelineError>
pub fn calculate_checksum(&self) -> Result<String, PipelineError>
Calculates SHA-256 checksum without modifying the chunk
§Developer Notes
- This is a pure function - doesn’t modify the chunk
- Use when you need the checksum but don’t want to create a new chunk
- For creating a chunk with checksum, use
with_calculated_checksum()
Trait Implementations§
Source§impl<'de> Deserialize<'de> for FileChunk
impl<'de> Deserialize<'de> for FileChunk
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for FileChunk
impl StructuralPartialEq for FileChunk
Auto Trait Implementations§
impl Freeze for FileChunk
impl RefUnwindSafe for FileChunk
impl Send for FileChunk
impl Sync for FileChunk
impl Unpin for FileChunk
impl UnwindSafe for FileChunk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more