Skip to main content

greentic_bundle/setup/
backend.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::Result;
4
5use super::{PersistedSetupState, SETUP_STATE_DIR};
6
7pub trait SetupBackend {
8    fn persist(&self, state: &PersistedSetupState) -> Result<Option<PathBuf>>;
9}
10
11#[derive(Debug, Clone)]
12pub struct FileSetupBackend {
13    root: PathBuf,
14}
15
16impl FileSetupBackend {
17    pub fn new(root: impl AsRef<Path>) -> Self {
18        Self {
19            root: root.as_ref().to_path_buf(),
20        }
21    }
22}
23
24impl SetupBackend for FileSetupBackend {
25    fn persist(&self, state: &PersistedSetupState) -> Result<Option<PathBuf>> {
26        let path = self
27            .root
28            .join(SETUP_STATE_DIR)
29            .join(format!("{}.json", state.provider_id));
30        if let Some(parent) = path.parent() {
31            std::fs::create_dir_all(parent)?;
32        }
33        std::fs::write(&path, format!("{}\n", serde_json::to_string_pretty(state)?))?;
34        Ok(Some(path))
35    }
36}
37
38#[derive(Debug, Default, Clone, Copy)]
39pub struct NoopSetupBackend;
40
41impl SetupBackend for NoopSetupBackend {
42    fn persist(&self, _state: &PersistedSetupState) -> Result<Option<PathBuf>> {
43        Ok(None)
44    }
45}