Skip to main content

cqlite_core/platform/
mod.rs

1//! Platform abstraction layer for cross-platform compatibility
2
3use crate::{Config, Result};
4
5pub mod fs;
6
7/// Opt-in filesystem seam for cross-platform compatibility.
8///
9/// NOTE: This is an opt-in `fs` seam only; most code paths use `std`/`tokio`
10/// fs directly — this is NOT an enforced I/O boundary. The `Platform` handle is
11/// threaded through many call sites, but only [`Platform::fs`] is ever read.
12/// Full removal of that threading is a possible later refactor (out of scope).
13#[derive(Debug)]
14pub struct Platform {
15    /// File system abstraction
16    fs: fs::FileSystem,
17
18    /// Configuration
19    config: Config,
20}
21
22impl Platform {
23    /// Create a new platform abstraction
24    pub async fn new(config: &Config) -> Result<Self> {
25        Ok(Self {
26            fs: fs::FileSystem::new().await?,
27            config: config.clone(),
28        })
29    }
30
31    /// Get file system abstraction
32    pub fn fs(&self) -> &fs::FileSystem {
33        &self.fs
34    }
35
36    /// Get configuration
37    pub fn config(&self) -> &Config {
38        &self.config
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use std::path::Path;
46    use tempfile::TempDir;
47
48    #[tokio::test]
49    async fn test_platform_creation() {
50        let config = Config::default();
51        let platform = Platform::new(&config).await.unwrap();
52
53        assert!(platform.fs().exists(Path::new(".")).await.unwrap());
54    }
55
56    #[tokio::test]
57    async fn test_file_system() {
58        let config = Config::default();
59        let platform = Platform::new(&config).await.unwrap();
60        let fs = platform.fs();
61
62        let temp_dir = TempDir::new().unwrap();
63        let test_file = temp_dir.path().join("test.txt");
64        let content = b"Hello, World!";
65
66        // Write file
67        fs.write_file(&test_file, content).await.unwrap();
68
69        // Check exists
70        assert!(fs.exists(&test_file).await.unwrap());
71
72        // Read file
73        let read_content = fs.read_file(&test_file).await.unwrap();
74        assert_eq!(read_content, content);
75
76        // Get file size
77        let size = fs.file_size(&test_file).await.unwrap();
78        assert_eq!(size, content.len() as u64);
79
80        // Remove file
81        fs.remove_file(&test_file).await.unwrap();
82        assert!(!fs.exists(&test_file).await.unwrap());
83    }
84}