rustensor 0.1.0

Small Rust-native checkpoint format for model metadata and tensor weights.
Documentation
//! RTEN file reader.
//!
//! This module reads `.rten` files defensively, validates the header,
//! verifies payload integrity and decodes checkpoints.

use crate::checkpoint::ModelCheckpoint;
use crate::error::{Result, RustensorError};
use crate::format::{HEADER_SIZE, RtenHeader};
use crate::integrity::verify_checksum;
use std::path::Path;

pub fn load_checkpoint(path: impl AsRef<Path>) -> Result<ModelCheckpoint> {
    let bytes = std::fs::read(path)?;

    if bytes.len() < HEADER_SIZE {
        return Err(RustensorError::InvalidHeader);
    }

    let header = RtenHeader::decode(&bytes[..HEADER_SIZE])?;

    let found_payload_length = (bytes.len() - HEADER_SIZE) as u64;

    if found_payload_length != header.payload_length {
        return Err(RustensorError::InvalidPayloadLength {
            expected: header.payload_length,
            found: found_payload_length,
        });
    }

    let payload = &bytes[HEADER_SIZE..];

    verify_checksum(payload, &header.checksum)?;

    let checkpoint: ModelCheckpoint = postcard::from_bytes(payload)
        .map_err(|error| RustensorError::DecodeError(error.to_string()))?;

    checkpoint.validate()?;

    Ok(checkpoint)
}