Skip to main content

auths_cli/adapters/
config_store.rs

1//! File-based adapter for the `ConfigStore` port.
2
3use std::path::Path;
4
5use auths_sdk::ports::{ConfigStore, ConfigStoreError};
6
7/// Reads and writes config files from the local filesystem.
8pub struct FileConfigStore;
9
10impl ConfigStore for FileConfigStore {
11    fn read(&self, path: &Path) -> Result<Option<String>, ConfigStoreError> {
12        match std::fs::read_to_string(path) {
13            Ok(content) => Ok(Some(content)),
14            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
15            Err(e) => Err(ConfigStoreError::Read {
16                path: path.to_path_buf(),
17                source: e,
18            }),
19        }
20    }
21
22    fn write(&self, path: &Path, content: &str) -> Result<(), ConfigStoreError> {
23        if let Some(parent) = path.parent() {
24            std::fs::create_dir_all(parent).map_err(|e| ConfigStoreError::Write {
25                path: path.to_path_buf(),
26                source: e,
27            })?;
28        }
29        std::fs::write(path, content).map_err(|e| ConfigStoreError::Write {
30            path: path.to_path_buf(),
31            source: e,
32        })
33    }
34}