use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use std::{
fs::{read_to_string, write},
path::Path,
};
use toml::{from_str, to_string_pretty};
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageConfig {
pub name: String,
pub version: String,
pub description: Option<String>,
pub authors: Vec<String>,
pub license: Option<String>,
pub repository: Option<String>,
}
impl PackageConfig {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
version: "0.1.0".to_string(),
description: None,
authors: vec![],
license: Some("MIT".to_string()),
repository: None,
}
}
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
let content = read_to_string(path).into_diagnostic()?;
let config: PackageConfig = from_str(&content).into_diagnostic()?;
Ok(config)
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let content = to_string_pretty(self).into_diagnostic()?;
write(path, content).into_diagnostic()?;
Ok(())
}
}