prefixload 0.7.0

S3 cli backuper
Documentation
use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

/// Represents a mapping from a file prefix to a cloud directory.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DirectoryEntry {
    pub prefix_file: String,
    pub cloud_dir: String,
}

/// Represents the application's YAML configuration file.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
    pub endpoint: String,
    pub bucket: String,
    pub part_size: u64,
    pub local_directory_path: String,
    pub directory_struct: Vec<DirectoryEntry>,
}

/// Returns the full path to the platform-native config file.
/// - Linux/macOS: ~/.config/prefixload/config.yml
/// - Windows: %APPDATA%\prefixload\config.yml
pub fn config_path() -> PathBuf {
    let mut dir = dirs_next::config_dir().expect("Cannot get config directory");
    dir.push("prefixload");
    if !dir.exists() {
        fs::create_dir_all(&dir).expect("Cannot create config directory");
    }
    dir.push("config.yml");

    dir
}

// These are general functions for loading/saving configs
impl Config {
    pub fn load(path: &PathBuf) -> Result<Self> {
        let s = fs::read_to_string(path)?;
        Ok(serde_yaml::from_str(&s)?)
    }
    pub fn save(&self, path: &PathBuf) -> Result<()> {
        let s = serde_yaml::to_string(self)?;
        fs::write(path, s)?;
        Ok(())
    }
}