leo_lang/cli/commands/
run.rsuse super::*;
use leo_retriever::NetworkName;
use snarkvm::{
cli::Run as SnarkVMRun,
prelude::{CanaryV0, MainnetV0, Network, Parser as SnarkVMParser, TestnetV0},
};
#[derive(Parser, Debug)]
pub struct LeoRun {
#[clap(name = "NAME", help = "The name of the program to run.", default_value = "main")]
pub(crate) name: String,
#[clap(name = "INPUTS", help = "The inputs to the program.")]
pub(crate) inputs: Vec<String>,
#[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")]
pub(crate) file: Option<String>,
#[clap(flatten)]
pub(crate) compiler_options: BuildOptions,
}
impl Command for LeoRun {
type Input = <LeoBuild as Command>::Output;
type Output = ();
fn log_span(&self) -> Span {
tracing::span!(tracing::Level::INFO, "Leo")
}
fn prelude(&self, context: Context) -> Result<Self::Input> {
(LeoBuild { options: self.compiler_options.clone() }).execute(context)
}
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
let network = NetworkName::try_from(context.get_network(&self.compiler_options.network)?)?;
match network {
NetworkName::MainnetV0 => handle_run::<MainnetV0>(self, context),
NetworkName::TestnetV0 => handle_run::<TestnetV0>(self, context),
NetworkName::CanaryV0 => handle_run::<CanaryV0>(self, context),
}
}
}
fn handle_run<N: Network>(command: LeoRun, context: Context) -> Result<<LeoRun as Command>::Output> {
let mut inputs = command.inputs;
let mut arguments = vec![SNARKVM_COMMAND.to_string(), command.name];
match command.file {
Some(file) => {
let path = context.dir()?.join(file);
let raw_content =
std::fs::read_to_string(&path).map_err(|err| PackageError::failed_to_read_file(path.display(), err))?;
let mut content = raw_content.as_str();
let mut values = vec![];
while let Ok((remaining, value)) = snarkvm::prelude::Value::<N>::parse(content) {
content = remaining;
values.push(value);
}
if !content.trim().is_empty() {
return Err(PackageError::failed_to_read_input_file(path.display()).into());
}
let mut inputs_from_file = values.into_iter().map(|value| value.to_string()).collect::<Vec<String>>();
arguments.append(&mut inputs_from_file);
}
None => arguments.append(&mut inputs),
}
let path = context.dir()?;
let build_directory = BuildDirectory::open(&path)?;
std::env::set_current_dir(&build_directory)
.map_err(|err| PackageError::failed_to_set_cwd(build_directory.display(), err))?;
let _ = std::panic::take_hook();
println!();
let command = SnarkVMRun::try_parse_from(&arguments).map_err(CliError::failed_to_parse_run)?;
let res = command.parse().map_err(CliError::failed_to_execute_run)?;
tracing::info!("{}", res);
Ok(())
}