Skip to main content

smbcloud_utils/
write_config.rs

1use {
2    crate::config::Config,
3    smbcloud_model::error_codes::{ErrorCode, ErrorResponse},
4    std::{fs, path::Path},
5};
6
7pub fn write_config(repo_path: &str, config: Config) -> Result<(), ErrorResponse> {
8    // Ensure .smb directory exists
9    let smb_dir = Path::new(&repo_path).join(".smb");
10    if !smb_dir.exists() {
11        fs::create_dir(smb_dir).map_err(|_| ErrorResponse::Error {
12            error_code: ErrorCode::MissingConfig,
13            message: ErrorCode::MissingConfig.message(None).to_string(),
14        })?;
15    }
16
17    // Write config to .smb/config.toml
18    let config_toml = toml::to_string(&config).map_err(|_| ErrorResponse::Error {
19        error_code: ErrorCode::MissingConfig,
20        message: ErrorCode::MissingConfig.message(None).to_string(),
21    })?;
22
23    fs::write(".smb/config.toml", config_toml).map_err(|_| ErrorResponse::Error {
24        error_code: ErrorCode::MissingConfig,
25        message: ErrorCode::MissingConfig.message(None).to_string(),
26    })?;
27
28    Ok(())
29}