leo_package/
manifest.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::*;
18
19use leo_errors::PackageError;
20
21use serde::{Deserialize, Serialize};
22use std::path::Path;
23
24pub const MANIFEST_FILENAME: &str = "program.json";
25
26/// Struct representation of program's `program.json` specification.
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct Manifest {
29    pub program: String,
30    pub version: String,
31    pub description: String,
32    pub license: String,
33    pub dependencies: Option<Vec<Dependency>>,
34    pub dev_dependencies: Option<Vec<Dependency>>,
35}
36
37impl Manifest {
38    /// Write the manifest to the given `path` as a JSON string.
39    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), PackageError> {
40        // Serialize the manifest to a JSON string.
41        let mut contents = serde_json::to_string_pretty(&self)
42            .map_err(|err| PackageError::failed_to_serialize_manifest_file(path.as_ref().display(), err))?;
43
44        // The seralized string doesn't end in a newline.
45        contents.push('\n');
46
47        // Write the manifest to the file.
48        std::fs::write(path, contents).map_err(PackageError::failed_to_write_manifest)
49    }
50
51    /// Read a Manifest from the given file as a JSON string.
52    pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, PackageError> {
53        // Read the manifest file.
54        let contents = std::fs::read_to_string(&path)
55            .map_err(|_| PackageError::failed_to_load_package(path.as_ref().display()))?;
56        // Deserialize the manifest.
57        serde_json::from_str(&contents)
58            .map_err(|err| PackageError::failed_to_deserialize_manifest_file(path.as_ref().display(), err))
59    }
60}