Skip to main content

conduit_cli/core/engine/
io.rs

1use serde::{Serialize, de::DeserializeOwned};
2use std::path::Path;
3use tokio::fs;
4
5use crate::{
6    core::schemas::{lock, manifest},
7    errors::ConduitResult,
8};
9
10pub trait TomlFile: Serialize + DeserializeOwned + Sync {
11    fn load<P: AsRef<Path> + Send>(
12        path: P,
13    ) -> impl std::future::Future<Output = ConduitResult<Self>> + Send {
14        async {
15            let content = fs::read_to_string(path).await?;
16            let data = toml::from_str(&content)?;
17            Ok(data)
18        }
19    }
20
21    fn save<P: AsRef<Path> + Send>(
22        &self,
23        path: P,
24    ) -> impl std::future::Future<Output = ConduitResult<()>> + Send {
25        async move {
26            let content = toml::to_string_pretty(self)?;
27            fs::write(path, content).await?;
28            Ok(())
29        }
30    }
31}
32
33impl TomlFile for manifest::Manifest {}
34impl TomlFile for lock::Lockfile {}