Skip to main content

objectiveai_sdk/filesystem/config/
client.rs

1use super::super::{Client, Error};
2
3impl Client {
4    pub async fn read_config(&self) -> Result<super::Config, Error> {
5        let path = self.config_path();
6        match tokio::fs::read(&path).await {
7            Ok(bytes) => serde_json::from_slice(&bytes).map_err(|e| Error::Parse(path, e)),
8            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(super::Config::default()),
9            Err(e) => Err(Error::Read(path, e)),
10        }
11    }
12
13    pub async fn write_config(&self, config: &super::Config) -> Result<(), Error> {
14        let path = self.config_path();
15        if let Some(parent) = path.parent() {
16            tokio::fs::create_dir_all(parent)
17                .await
18                .map_err(|e| Error::Write(parent.to_path_buf(), e))?;
19        }
20        let bytes = serde_json::to_vec_pretty(config).map_err(Error::Serialize)?;
21        tokio::fs::write(&path, bytes)
22            .await
23            .map_err(|e| Error::Write(path, e))?;
24        Ok(())
25    }
26}