Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to output when no casm_output_path provided #4

Merged
merged 8 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cairo-lang-starknet-sierra-1_0_0 = { package = "cairo-lang-starknet", git = "htt
cairo-lang-starknet = "=2.4.0"
serde_json = "1.0.108"
serde = "1.0.193"
test-case = "3.3.1"
clap = "4.4.11"
anyhow = "1.0.75"
console = "0.15.7"
Expand All @@ -20,6 +19,7 @@ snapbox = "0.4.15"
tempfile = "3.8.1"
indoc = "2.0.4"
fs_extra = "1.3.0"
test-case = "3.3.1"

[[bin]]
name = "universal-sierra-compiler"
Expand Down
21 changes: 12 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Error, Result};
use clap::Parser;
use console::style;
use serde_json::to_writer;
use std::fs::{File, OpenOptions};
use std::fs::File;
use std::path::PathBuf;
use universal_sierra_compiler::compile;

Expand All @@ -15,7 +15,7 @@ struct Args {

/// Path to where casm json file will be saved
#[arg(short, long)]
casm_output_path: PathBuf,
casm_output_path: Option<PathBuf>,
}

fn print_error_message(error: &Error) {
Expand All @@ -34,14 +34,17 @@ fn main_execution() -> Result<bool> {
let casm = compile(sierra_json)?;
let casm_json = serde_json::to_value(casm)?;

let casm_file = OpenOptions::new()
.write(true)
.create(true)
.append(false)
.open(args.casm_output_path)
.context("Unable to open/create casm json file")?;
match args.casm_output_path {
Some(output_path) => {
let casm_file =
File::create(output_path).context("Unable to open/create casm json file")?;

to_writer(casm_file, &casm_json).context("Unable to save casm json file")?;
to_writer(casm_file, &casm_json).context("Unable to save casm json file")?;
}
None => {
println!("{}", serde_json::to_string(&casm_json)?);
}
};

Ok(true)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ fn write_to_existing_file() {
verify_output_file(temp_dir.path().join(casm_file_name));
}

#[test]
fn write_to_stdout() {
let sierra_file_name = "sierra_1_4_0.json";
let args = vec!["--sierra-input-path", &sierra_file_name];

let temp_dir = temp_dir_with_sierra_file(sierra_file_name);
let snapbox = runner(args, &temp_dir);

let output = String::from_utf8(snapbox.assert().success().get_output().stdout.clone()).unwrap();
assert!(output.contains("bytecode"));
}

#[test]
fn wrong_json() {
let sierra_file_name = "wrong_sierra.json";
Expand Down