Skip to main content

kaish_kernel/
backend_walker_fs.rs

1//! `WalkerFs` adapter for `KernelBackend`.
2//!
3//! Bridges kaish-kernel's `KernelBackend` trait to kaish-glob's `WalkerFs`
4//! trait so `FileWalker` and `IgnoreFilter` can work with any backend.
5
6use async_trait::async_trait;
7use std::path::Path;
8
9use crate::backend::KernelBackend;
10use crate::vfs::DirEntry;
11use kaish_glob::{WalkerDirEntry, WalkerError, WalkerFs};
12
13/// Newtype wrapper to impl the external `WalkerDirEntry` trait for `DirEntry`
14/// (orphan rule: both types are now in external crates).
15pub struct BackendDirEntry(pub DirEntry);
16
17impl WalkerDirEntry for BackendDirEntry {
18    fn name(&self) -> &str {
19        &self.0.name
20    }
21
22    fn is_dir(&self) -> bool {
23        self.0.is_dir()
24    }
25
26    fn is_file(&self) -> bool {
27        self.0.is_file()
28    }
29
30    fn is_symlink(&self) -> bool {
31        self.0.is_symlink()
32    }
33}
34
35/// Wraps a `&dyn KernelBackend` to implement `WalkerFs`.
36pub struct BackendWalkerFs<'a>(pub &'a dyn KernelBackend);
37
38#[async_trait]
39impl WalkerFs for BackendWalkerFs<'_> {
40    type DirEntry = BackendDirEntry;
41
42    async fn list_dir(&self, path: &Path) -> Result<Vec<BackendDirEntry>, WalkerError> {
43        self.0
44            .list(path)
45            .await
46            .map(|entries| entries.into_iter().map(BackendDirEntry).collect())
47            .map_err(|e| WalkerError::Io(e.to_string()))
48    }
49
50    async fn read_file(&self, path: &Path) -> Result<Vec<u8>, WalkerError> {
51        self.0.read(path, None).await.map_err(|e| WalkerError::Io(e.to_string()))
52    }
53
54    async fn is_dir(&self, path: &Path) -> bool {
55        self.0.stat(path).await.is_ok_and(|info| info.is_dir())
56    }
57
58    async fn exists(&self, path: &Path) -> bool {
59        self.0.exists(path).await
60    }
61}