graft_sqlite/
file.rs

1use std::fmt::Debug;
2
3use culprit::Result;
4use enum_dispatch::enum_dispatch;
5use mem_file::MemFile;
6use sqlite_plugin::{flags::LockLevel, vfs::VfsHandle};
7use vol_file::VolFile;
8
9use crate::vfs::ErrCtx;
10
11pub mod mem_file;
12pub mod vol_file;
13
14#[enum_dispatch]
15pub trait VfsFile: Debug {
16    fn readonly(&self) -> bool;
17    fn in_memory(&self) -> bool;
18
19    fn lock(&mut self, level: LockLevel) -> Result<(), ErrCtx>;
20    fn unlock(&mut self, level: LockLevel) -> Result<(), ErrCtx>;
21
22    fn file_size(&mut self) -> Result<usize, ErrCtx>;
23    fn truncate(&mut self, size: usize) -> Result<(), ErrCtx>;
24
25    fn write(&mut self, offset: usize, data: &[u8]) -> Result<usize, ErrCtx>;
26    fn read(&mut self, offset: usize, data: &mut [u8]) -> Result<usize, ErrCtx>;
27}
28
29#[enum_dispatch(VfsFile)]
30#[derive(Debug)]
31#[allow(clippy::large_enum_variant)]
32pub enum FileHandle {
33    MemFile,
34    VolFile,
35}
36
37impl VfsHandle for FileHandle {
38    fn readonly(&self) -> bool {
39        VfsFile::readonly(self)
40    }
41
42    fn in_memory(&self) -> bool {
43        VfsFile::in_memory(self)
44    }
45}