snipr/models/
snip_config_model.rs

1use crate::helpers::expand_home_dir::expand_home_dir;
2use anyhow::Context;
3use serde::{Deserialize, Serialize};
4use std::fs;
5use std::io::{stdout, Write};
6
7#[derive(Serialize, Deserialize, Debug)]
8pub struct SnipConfig {
9    pub path: String,
10}
11
12impl SnipConfig {
13    pub fn load(path: &str) -> anyhow::Result<SnipConfig> {
14        let config_content = fs::read_to_string(path).context("Failed to read config file")?;
15        let config: SnipConfig =
16            serde_json::from_str(&config_content).context("Failed to parse config file")?;
17        writeln!(stdout(), "{}", &config.path).unwrap();
18        Ok(config)
19    }
20
21    pub fn save(&self, path: &str) -> anyhow::Result<()> {
22        let config_content =
23            serde_json::to_string_pretty(self).context("Failed to serialize config")?;
24        fs::write(path, config_content).context("Failed to write config file")?;
25        Ok(())
26    }
27
28    pub fn update_path(&mut self, new_path: String) {
29        self.path = expand_home_dir(&new_path).to_string_lossy().into_owned();
30    }
31}