snarkvm_debug/file/
readme_file.rs

1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    prelude::{Network, ProgramID},
17    synthesizer::Program,
18};
19
20use anyhow::{ensure, Result};
21use std::{
22    fs::File,
23    io::Write,
24    path::{Path, PathBuf},
25};
26
27pub struct README {
28    /// The file path.
29    path: PathBuf,
30}
31
32impl README {
33    /// Creates a new README file with the given directory path and program ID.
34    pub fn create<N: Network>(directory: &Path, id: &ProgramID<N>) -> Result<Self> {
35        // Ensure the directory path exists.
36        ensure!(directory.exists(), "The program directory does not exist: {}", directory.display());
37        // Ensure the program name is valid.
38        ensure!(!Program::is_reserved_keyword(id.name()), "Program name is invalid (reserved): {id}");
39
40        // Construct the initial README string.
41        let readme_string = format!(
42            r"# {id}
43
44## Build Guide
45
46To compile this Aleo program, run:
47```bash
48snarkvm build
49```
50
51To execute this Aleo program, run:
52```bash
53snarkvm run hello
54```
55"
56        );
57
58        // Construct the file name.
59        let file_name = "README.md".to_string();
60
61        // Construct the file path.
62        let path = directory.join(file_name);
63
64        // Ensure the file path does not already exist.
65        ensure!(!path.exists(), "README file already exists: {}", path.display());
66
67        // Write the file.
68        File::create(&path)?.write_all(readme_string.as_bytes())?;
69
70        // Return the README file.
71        Ok(Self { path })
72    }
73
74    /// Returns the file path.
75    pub const fn path(&self) -> &PathBuf {
76        &self.path
77    }
78}