9. Storage trait for dependency injection
Date: 2026-04-22
Status
Accepted
Relates to 17. Forbid unsafe code workspace-wide with env-free config tests
Context
Collectors read files and access the git and OpenCode databases. They also have to work in tests without a real filesystem.
Decision
Cerebro defines traits for dependency injection:
#![allow(unused)]
fn main() {
pub trait Storage: Send + Sync {
async fn exists(&self, path: &Path) -> bool;
async fn read_to_string(&self, path: &Path) -> Result<String>;
// ...
}
pub trait Git: Send + Sync {
async fn commits(&self, repo_path: &Path) -> Result<Vec<GitCommit>>;
async fn modified_files(&self, repo_path: &Path) -> Result<Vec<FileChange>>;
}
}
Implementations:
TokioStorageuses tokio for async file I/OGit2uses thegit2crate for git operations and caps commits at 20 per projectSqlxDbusessqlxfor OpenCode SQLite queries
Consequences
Pros
- Tests run without a real filesystem
- Tests can swap in mock storage
- Clear abstraction boundaries
Cons
- Trait indirection overhead
- More complex DI setup
Notes
See cerebro-core/src/storage.rs.