use crate::wizard::error::WizardError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WizardState {
pub current_step: usize,
pub completed_steps: Vec<usize>,
pub data: HashMap<String, String>,
pub timestamp: chrono::DateTime<chrono::Utc>,
pub wizard_type: WizardType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WizardType {
InitialSetup,
AddModel,
BatchSetup,
}
impl WizardState {
pub fn new(wizard_type: WizardType) -> Self {
Self {
current_step: 0,
completed_steps: Vec::new(),
data: HashMap::new(),
timestamp: chrono::Utc::now(),
wizard_type,
}
}
pub fn save(&self, path: &std::path::Path) -> Result<(), WizardError> {
let content = toml::to_string_pretty(self)?;
std::fs::write(path, content)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(path)?.permissions();
perms.set_mode(0o600);
std::fs::set_permissions(path, perms)?;
}
Ok(())
}
pub fn load(path: &std::path::Path) -> Result<Option<Self>, WizardError> {
if !path.exists() {
return Ok(None);
}
let content = std::fs::read_to_string(path)?;
let state: WizardState = toml::from_str(&content)?;
Ok(Some(state))
}
pub fn is_expired(&self) -> bool {
let now = chrono::Utc::now();
let duration = now.signed_duration_since(self.timestamp);
duration.num_hours() > 24
}
}