speck-core 0.2.0

Secure runtime package manager for MMU-less microcontrollers
Documentation
//! Transaction journal for atomic updates
//! 
//! Records operations to allow recovery from interrupted writes.

use crate::flash::{Flash, PageId};
use crate::error::Result;

/// Transaction record
#[derive(Clone, Debug)]
pub struct Transaction {
    slot: usize,
    address: usize,
    expected_size: usize,
    written: usize,
    committed: bool,
}

impl Transaction {
    /// Create new transaction
    pub fn new(slot: usize, address: usize, expected_size: usize) -> Self {
        Self {
            slot,
            address,
            expected_size,
            written: 0,
            committed: false,
        }
    }
    
    /// Get target address
    pub fn address(&self) -> usize {
        self.address
    }
    
    /// Record bytes written
    pub fn record_write(&mut self, bytes: usize) {
        self.written += bytes;
    }
    
    /// Check if complete
    pub fn is_complete(&self) -> bool {
        self.written >= self.expected_size && self.committed
    }
}

/// Simple journal for crash recovery
pub struct Journal {
    region: core::ops::Range<usize>,
}

impl Journal {
    /// Create journal in specified region
    pub fn new(region: core::ops::Range<usize>) -> Self {
        Self { region }
    }
    
    /// Log transaction start
    pub fn log_start(&mut self, flash: &mut Flash, tx: &Transaction) -> Result<()> {
        let entry = JournalEntry::Start {
            slot: tx.slot as u8,
            size: tx.expected_size as u32,
        };
        self.append(flash, entry)
    }
    
    /// Log transaction commit
    pub fn commit(&mut self, tx: Transaction, flash: &mut Flash) -> Result<()> {
        let entry = JournalEntry::Commit {
            slot: tx.slot as u8,
        };
        self.append(flash, entry)
    }
    
    fn append(&mut self, flash: &mut Flash, entry: JournalEntry) -> Result<()> {
        // Simple circular buffer implementation would go here
        // For now, just reserve the capability
        let _ = (flash, entry);
        Ok(())
    }
}

#[derive(Clone, Debug)]
enum JournalEntry {
    Start { slot: u8, size: u32 },
    Commit { slot: u8 },
    Abort { slot: u8 },
}