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    #[serde(default = "current_version")]
34    pub leo: String,
35    pub dependencies: Option<Vec<Dependency>>,
36    pub dev_dependencies: Option<Vec<Dependency>>,
37}
38
39impl Manifest {
40    /// Write the manifest to the given `path` as a JSON string.
41    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), PackageError> {
42        // Serialize the manifest to a JSON string.
43        let mut contents = serde_json::to_string_pretty(&self)
44            .map_err(|err| PackageError::failed_to_serialize_manifest_file(path.as_ref().display(), err))?;
45
46        // The seralized string doesn't end in a newline.
47        contents.push('\n');
48
49        // Write the manifest to the file.
50        std::fs::write(path, contents).map_err(PackageError::failed_to_write_manifest)
51    }
52
53    /// Read a Manifest from the given file as a JSON string.
54    pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, PackageError> {
55        // Read the manifest file.
56        let contents = std::fs::read_to_string(&path)
57            .map_err(|_| PackageError::failed_to_load_package(path.as_ref().display()))?;
58        // Deserialize the manifest.
59        serde_json::from_str(&contents)
60            .map_err(|err| PackageError::failed_to_deserialize_manifest_file(path.as_ref().display(), err))
61    }
62}
63
64// Returns the current version of Leo.
65fn current_version() -> String {
66    env!("CARGO_PKG_VERSION").to_string()
67}