use std::path::Path;
use serde::Serialize;
use crate::error::Result;
use crate::protocol::Protocol;
#[derive(Debug, Clone, Default, Serialize)]
pub struct RestoreReport {
pub restored: usize,
pub failed: Vec<(String, String)>,
}
impl RestoreReport {
pub fn is_success(&self) -> bool {
self.failed.is_empty()
}
}
pub(crate) fn parse_backup(json: &str) -> Result<Vec<Protocol>> {
let protocols: Vec<Protocol> = serde_json::from_str(json)?;
Ok(protocols)
}
pub(crate) fn to_json(protocols: &[Protocol]) -> Result<String> {
Ok(serde_json::to_string_pretty(protocols)?)
}
pub(crate) fn write_to_file(protocols: &[Protocol], path: impl AsRef<Path>) -> Result<()> {
let json = to_json(protocols)?;
std::fs::write(path.as_ref(), json)?;
Ok(())
}
pub(crate) fn read_from_file(path: impl AsRef<Path>) -> Result<Vec<Protocol>> {
let json = std::fs::read_to_string(path.as_ref())?;
parse_backup(&json)
}