icarus_core/
state.rs

1//! State management abstractions for persistent storage
2
3use crate::error::Result;
4use serde::{Deserialize, Serialize};
5
6/// Trait for types that can be stored in ICP stable memory
7pub trait Storable: Sized {
8    /// Serialize the type to bytes
9    fn to_bytes(&self) -> Result<Vec<u8>>;
10
11    /// Deserialize from bytes
12    fn from_bytes(bytes: &[u8]) -> Result<Self>;
13
14    /// Maximum size in bytes when serialized
15    const MAX_SIZE: u32;
16
17    /// Fixed size in bytes (None if variable size)
18    const FIXED_SIZE: Option<u32> = None;
19}
20
21/// Version information for state migrations
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct StateVersion {
24    pub version: u32,
25    pub created_at: u64,
26}
27
28/// Trait for state that can be migrated between versions
29pub trait Migratable: Storable {
30    /// Current version of the state schema
31    fn current_version() -> u32;
32
33    /// Migrate from an older version
34    fn migrate_from(version: u32, data: &[u8]) -> Result<Self>;
35}
36
37/// Metadata about server state
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct StateMetadata {
40    pub version: StateVersion,
41    pub size_bytes: u64,
42    pub last_modified: u64,
43}