1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use super::*;

use std::fs;

/// Initialize a new Leo example.
#[derive(Parser, Debug)]
pub struct Example {
    #[clap(name = "NAME", help = "The example to initialize.")]
    pub(crate) name: String,
    #[clap(short = 'n', long, help = "Name of the network to use", default_value = "testnet")]
    pub(crate) network: String,
    #[clap(
        short = 'e',
        long,
        help = "Endpoint to retrieve network state from.",
        default_value = "https://api.explorer.provable.com/v1"
    )]
    pub(crate) endpoint: String,
}

impl Command for Example {
    type Input = <New as Command>::Output;
    type Output = ();

    fn prelude(&self, context: Context) -> Result<Self::Input> {
        // Run leo new <name> --network <network>
        (New { name: self.name.clone(), network: self.network.clone(), endpoint: self.endpoint.clone() })
            .execute(context)
    }

    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output>
    where
        Self: Sized,
    {
        let package_dir = context.dir()?;

        // Parse the example variant.
        let example_variant =
            ExampleVariant::try_from(self.name.as_str()).map_err(|_| CliError::invalid_example(self.name.as_str()))?;

        // Write the main file.
        let main_file_path = package_dir.join("src").join("main.leo");
        fs::write(main_file_path, example_variant.main_file_string()).map_err(CliError::failed_to_write_file)?;

        // Write the README file.
        let readme_file_path = package_dir.join("README.md");
        let readme_file_path_string = readme_file_path.display().to_string();
        fs::write(readme_file_path, example_variant.readme_file_string()).map_err(CliError::failed_to_write_file)?;

        // Write the run.sh file.
        let run_file_path = package_dir.join("run.sh");
        fs::write(run_file_path, example_variant.run_file_string()).map_err(CliError::failed_to_write_file)?;

        tracing::info!(
            "🚀 To run the '{}' program follow the instructions at {}",
            example_variant.name().bold(),
            readme_file_path_string
        );

        Ok(())
    }
}

/// The example programs that can be generated.
#[derive(Parser, Debug, Copy, Clone)]
pub enum ExampleVariant {
    #[clap(name = "lottery", about = "A public lottery program")]
    Lottery,
    #[clap(name = "tictactoe", about = "A standard tic-tac-toe game program")]
    TicTacToe,
    #[clap(name = "token", about = "A transparent & shielded custom token program")]
    Token,
}

impl ExampleVariant {
    fn name(&self) -> String {
        match self {
            Self::Lottery => "lottery".to_string(),
            Self::TicTacToe => "tictactoe".to_string(),
            Self::Token => "token".to_string(),
        }
    }

    fn main_file_string(&self) -> String {
        match self {
            Self::Lottery => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/lottery/src/main.leo")).to_string()
            }
            Self::TicTacToe => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/tictactoe/src/main.leo")).to_string()
            }
            Self::Token => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/token/src/main.leo")).to_string()
            }
        }
    }

    fn readme_file_string(&self) -> String {
        match self {
            Self::Lottery => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/lottery/README.md")).to_string()
            }
            Self::TicTacToe => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/tictactoe/README.md")).to_string()
            }
            Self::Token => include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/token/README.md")).to_string(),
        }
    }

    fn run_file_string(&self) -> String {
        match self {
            Self::Lottery => include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/lottery/run.sh")).to_string(),
            Self::TicTacToe => {
                include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/tictactoe/run.sh")).to_string()
            }
            Self::Token => include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/token/run.sh")).to_string(),
        }
    }
}

impl TryFrom<&str> for ExampleVariant {
    type Error = ();

    fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
        match value {
            "lottery" => Ok(Self::Lottery),
            "tictactoe" => Ok(Self::TicTacToe),
            "token" => Ok(Self::Token),
            _ => Err(()),
        }
    }
}