use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::error::AppError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scope {
pub version: u32,
pub outcome: String,
pub created_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub notes: String,
}
impl Scope {
pub fn new(outcome: String) -> Self {
Self {
version: 1,
outcome,
created_at: Utc::now(),
notes: String::new(),
}
}
pub fn write(&self, state_dir: &Path) -> Result<(), AppError> {
std::fs::create_dir_all(state_dir)?;
let path = state_dir.join("scope.yaml");
let yaml = serde_yaml::to_string(self)?;
std::fs::write(path, yaml)?;
Ok(())
}
pub fn read(state_dir: &Path) -> Result<Self, AppError> {
let path = state_dir.join("scope.yaml");
if !path.exists() {
return Err(AppError::ScopeMissing);
}
let yaml = std::fs::read_to_string(path)?;
let scope: Scope = serde_yaml::from_str(&yaml)?;
Ok(scope)
}
}