Skip to content

Commit

Permalink
Split scarb-execute into subfiles (#1924)
Browse files Browse the repository at this point in the history
## Stack
- #1924
- #1900
- #1922 

To expose args and `main_inner` so these are usable by scarb-prove
  • Loading branch information
DelevoXDG authored Jan 31, 2025
1 parent d9fd2ec commit f2a53fc
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 410 deletions.
104 changes: 104 additions & 0 deletions extensions/scarb-execute/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use anyhow::{Context, Result};
use cairo_lang_runner::Arg;
use cairo_lang_utils::bigint::BigUintAsHex;
use camino::Utf8PathBuf;
use clap::{arg, Parser, ValueEnum};
use num_bigint::BigInt;
use scarb_ui::args::{PackagesFilter, VerbositySpec};
use std::fs;

/// Compiles a Cairo project and runs a function marked `#[executable]`.
/// Exits with 1 if the compilation or run fails, otherwise 0.
#[derive(Parser, Clone, Debug)]
#[clap(version, verbatim_doc_comment)]
pub struct Args {
/// Name of the package.
#[command(flatten)]
pub packages_filter: PackagesFilter,

/// Do not rebuild the package.
#[arg(long, default_value_t = false)]
pub no_build: bool,

#[clap(flatten)]
pub run: ExecutionArgs,

/// Logging verbosity.
#[command(flatten)]
pub verbose: VerbositySpec,
}

#[derive(Parser, Clone, Debug)]
pub struct ExecutionArgs {
#[command(flatten)]
pub arguments: ProgramArguments,

/// Desired execution output, either default Standard or CairoPie
#[arg(long, default_value = "standard")]
pub output: OutputFormat,

/// Execution target.
#[arg(long, default_value = "standalone")]
pub target: ExecutionTarget,

/// Whether to print the program outputs.
#[arg(long, default_value_t = false)]
pub print_program_output: bool,
}

#[derive(Parser, Debug, Clone)]
pub struct ProgramArguments {
/// Serialized arguments to the executable function.
#[arg(long, value_delimiter = ',')]
pub arguments: Vec<BigInt>,

/// Serialized arguments to the executable function from a file.
#[arg(long, conflicts_with = "arguments")]
pub arguments_file: Option<Utf8PathBuf>,
}

impl ProgramArguments {
pub fn read_arguments(self) -> Result<Vec<Arg>> {
if let Some(path) = self.arguments_file {
let file = fs::File::open(&path).with_context(|| "reading arguments file failed")?;
let as_vec: Vec<BigUintAsHex> = serde_json::from_reader(file)
.with_context(|| "deserializing arguments file failed")?;
Ok(as_vec
.into_iter()
.map(|v| Arg::Value(v.value.into()))
.collect())
} else {
Ok(self
.arguments
.iter()
.map(|v| Arg::Value(v.into()))
.collect())
}
}
}

#[derive(ValueEnum, Clone, Debug)]
pub enum OutputFormat {
CairoPie,
Standard,
}
impl OutputFormat {
pub fn is_standard(&self) -> bool {
matches!(self, OutputFormat::Standard)
}
pub fn is_cairo_pie(&self) -> bool {
matches!(self, OutputFormat::CairoPie)
}
}

#[derive(ValueEnum, Clone, Debug)]
pub enum ExecutionTarget {
Bootloader,
Standalone,
}

impl ExecutionTarget {
pub fn is_standalone(&self) -> bool {
matches!(self, ExecutionTarget::Standalone)
}
}
Loading

0 comments on commit f2a53fc

Please sign in to comment.