Skip to main content

auths_core/ports/
config_store.rs

1//! Config file I/O port for reading and writing `config.toml`.
2
3use std::path::PathBuf;
4
5use thiserror::Error;
6
7/// Errors that can occur during config store operations.
8#[derive(Debug, Error)]
9pub enum ConfigStoreError {
10    /// Failed to read the config file.
11    #[error("failed to read config from {path}")]
12    Read {
13        /// The path that could not be read.
14        path: PathBuf,
15        /// The underlying I/O error.
16        #[source]
17        source: std::io::Error,
18    },
19    /// Failed to write the config file.
20    #[error("failed to write config to {path}")]
21    Write {
22        /// The path that could not be written.
23        path: PathBuf,
24        /// The underlying I/O error.
25        #[source]
26        source: std::io::Error,
27    },
28}
29
30impl auths_crypto::AuthsErrorInfo for ConfigStoreError {
31    fn error_code(&self) -> &'static str {
32        match self {
33            Self::Read { .. } => "AUTHS-E3951",
34            Self::Write { .. } => "AUTHS-E3952",
35        }
36    }
37
38    fn suggestion(&self) -> Option<&'static str> {
39        match self {
40            Self::Read { .. } => Some("Check that ~/.auths/config.toml exists and is readable"),
41            Self::Write { .. } => Some("Check file permissions for ~/.auths/config.toml"),
42        }
43    }
44}
45
46/// Abstracts filesystem access for config file operations.
47///
48/// Args:
49/// * `path` - The path to the config file.
50///
51/// Usage:
52/// ```ignore
53/// let content = store.read(Path::new("~/.auths/config.toml"))?;
54/// store.write(Path::new("~/.auths/config.toml"), "content")?;
55/// ```
56pub trait ConfigStore: Send + Sync {
57    /// Read the config file content.
58    /// Returns `None` if the file does not exist.
59    fn read(&self, path: &std::path::Path) -> Result<Option<String>, ConfigStoreError>;
60
61    /// Write content to the config file, creating parent dirs as needed.
62    fn write(&self, path: &std::path::Path, content: &str) -> Result<(), ConfigStoreError>;
63}