Skip to main content

cc_switch_lib/
import_export.rs

1use crate::error::AppError;
2use crate::Database;
3use serde_json::{json, Value};
4use std::path::PathBuf;
5
6/// Export the SQLite database to a CC Switch compatible SQL file.
7///
8/// This mirrors the upstream command signature style (`Result<Value, String>`).
9pub async fn export_config_to_file(file_path: String) -> Result<Value, String> {
10    let target_path = PathBuf::from(&file_path);
11
12    let Some(parent) = target_path.parent() else {
13        return Err(
14            AppError::InvalidInput(format!("Invalid export path: {file_path}")).to_string(),
15        );
16    };
17    if !parent.as_os_str().is_empty() {
18        std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e).to_string())?;
19    }
20
21    let db = Database::init().map_err(|e| e.to_string())?;
22    db.export_sql(&target_path).map_err(|e| e.to_string())?;
23
24    Ok(json!({
25        "success": true,
26        "message": "SQL exported successfully",
27        "filePath": file_path
28    }))
29}