rustlite_core/
format_version.rs

1/// File format versions for RustLite (v1.0.0+)
2///
3/// This module defines version constants for all file formats to ensure
4/// forward/backward compatibility and safe upgrades.
5/// SSTable format version
6pub const SSTABLE_FORMAT_VERSION: u16 = 1;
7
8/// WAL format version  
9pub const WAL_FORMAT_VERSION: u16 = 1;
10
11/// Manifest format version
12pub const MANIFEST_FORMAT_VERSION: u16 = 1;
13
14/// Magic numbers for file validation
15pub mod magic {
16    /// SSTable magic: "RSTL" (RuSTLite)
17    pub const SSTABLE: u32 = 0x5253544C;
18
19    /// WAL magic: "RLWL" (RustLite WAL)
20    pub const WAL: u32 = 0x524C574C;
21
22    /// Manifest magic: "RLMF" (RustLite ManiFest)
23    pub const MANIFEST: u32 = 0x524C4D46;
24}
25
26/// Version compatibility information
27pub struct FormatVersion {
28    /// Current version of this format
29    pub current: u16,
30    /// Minimum supported version for reading
31    pub min_read: u16,
32    /// Minimum supported version for writing
33    pub min_write: u16,
34}
35
36impl FormatVersion {
37    /// Check if a version can be read
38    pub fn can_read(&self, version: u16) -> bool {
39        version >= self.min_read && version <= self.current
40    }
41
42    /// Check if a version can be written
43    pub fn can_write(&self, version: u16) -> bool {
44        version >= self.min_write && version <= self.current
45    }
46}
47
48/// SSTable format version info
49pub fn sstable_version() -> FormatVersion {
50    FormatVersion {
51        current: SSTABLE_FORMAT_VERSION,
52        min_read: 1,
53        min_write: 1,
54    }
55}
56
57/// WAL format version info
58pub fn wal_version() -> FormatVersion {
59    FormatVersion {
60        current: WAL_FORMAT_VERSION,
61        min_read: 1,
62        min_write: 1,
63    }
64}
65
66/// Manifest format version info
67pub fn manifest_version() -> FormatVersion {
68    FormatVersion {
69        current: MANIFEST_FORMAT_VERSION,
70        min_read: 1,
71        min_write: 1,
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_version_compatibility() {
81        let v = sstable_version();
82        assert!(v.can_read(1));
83        assert!(v.can_write(1));
84        assert!(!v.can_read(0));
85        assert!(!v.can_read(999));
86    }
87}