use crate::*;
use leo_errors::PackageError;
use serde::{Deserialize, Serialize};
use std::path::Path;
pub const MANIFEST_FILENAME: &str = "program.json";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Manifest {
pub program: String,
pub version: String,
pub description: String,
pub license: String,
#[serde(default = "current_version")]
pub leo: String,
pub dependencies: Option<Vec<Dependency>>,
pub dev_dependencies: Option<Vec<Dependency>>,
}
impl Manifest {
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), PackageError> {
let mut contents = serde_json::to_string_pretty(&self)
.map_err(|err| PackageError::failed_to_serialize_manifest_file(path.as_ref().display(), err))?;
contents.push('\n');
std::fs::write(path, contents).map_err(PackageError::failed_to_write_manifest)
}
pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, PackageError> {
let contents = std::fs::read_to_string(&path)
.map_err(|_| PackageError::failed_to_load_package(path.as_ref().display()))?;
serde_json::from_str(&contents)
.map_err(|err| PackageError::failed_to_deserialize_manifest_file(path.as_ref().display(), err))
}
}
fn current_version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}