lumer 0.1.4

Lumer is a tool for managing Lumi projects
Documentation
//! Package manifest model and helpers.
//!
//! Reads and writes `Package.toml` and provides small utilities to mutate
//! dependencies. The format is intentionally simple to keep the CLI fast and
//! predictable.
use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use std::{
    fs::{read_to_string, write},
    path::Path,
};
use toml::{from_str, to_string_pretty};

/// In-memory representation of `Package.toml`.
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageConfig {
    /// Name of the package.
    pub name: String,
    /// Version of the package.
    pub version: String,
    /// Description of the package.
    pub description: Option<String>,
    /// Authors of the package.
    pub authors: Vec<String>,
    /// License of the package.
    pub license: Option<String>,
    /// Repository URL of the package.
    pub repository: Option<String>,
}

impl PackageConfig {
    /// Creates a new package config with defaults.
    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,
        }
    }
    /// Loads a `Package.toml` from disk.
    ///
    /// # Errors
    /// Returns an error if the file cannot be read or parsed.
    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)
    }
    /// Saves this configuration to disk in TOML format.
    ///
    /// # Errors
    /// Returns an error if serialization or writing to the destination fails.
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let content = to_string_pretty(self).into_diagnostic()?;
        write(path, content).into_diagnostic()?;
        Ok(())
    }
}