Skip to content

Commit

Permalink
Improved logs and code keeping (#182)
Browse files Browse the repository at this point in the history
* ux and code keeping

* output print
  • Loading branch information
shramee authored Dec 21, 2023
1 parent 3664b3a commit ab4d25a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 161 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod exercise;
mod project;
mod run;
mod scarb;
mod utils;
mod verify;

// In sync with crate version
Expand Down
71 changes: 11 additions & 60 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use std::process::Command;

use crate::exercise::{Exercise, Mode};
use crate::{
exercise::{Exercise, Mode},
utils,
};

// Invoke the rust compiler on the path of the given exercise,
// and run the ensuing binary.
// The verbose argument helps determine whether or not to show
// the output from the test harnesses (if the mode of the exercise is test)
pub fn run(exercise: &Exercise) -> Result<(), ()> {
match exercise.mode {
Mode::Build => build_cairo(exercise)?,
Mode::Run => run_cairo(exercise)?,
Mode::Test => test_cairo(exercise)?,
}
let run_result = match exercise.mode {
Mode::Build => utils::build_exercise(exercise)?,
Mode::Run => utils::run_exercise(exercise)?,
Mode::Test => utils::test_exercise(exercise)?,
};
utils::print_exercise_output(run_result);
utils::print_exercise_success(exercise);
Ok(())
}

Expand All @@ -27,57 +32,3 @@ pub fn reset(exercise: &Exercise) -> Result<(), ()> {
Err(_) => Err(()),
}
}

// Invoke the rust compiler on the path of the given exercise
// and run the ensuing binary.
// This is strictly for non-test binaries, so output is displayed
fn build_cairo(exercise: &Exercise) -> Result<(), ()> {
println!("\nBuilding {exercise}...\n");
let output = exercise.build();

if let Some(error) = output.as_ref().err() {
println!("{error}");
Err(())
} else {
let message = output.unwrap();
println!("{message}");
success!("Successfully built {}", exercise);
Ok(())
}
}

// Invoke the rust compiler on the path of the given exercise
// and run the ensuing binary.
// This is strictly for non-test binaries, so output is displayed
fn run_cairo(exercise: &Exercise) -> Result<(), ()> {
println!("\nRunning {exercise}...\n");
let output = exercise.run();

if let Some(error) = output.as_ref().err() {
println!("{error}");
Err(())
} else {
let message = output.unwrap();
println!("{message}");
success!("Successfully ran {}", exercise);
Ok(())
}
}

// Invoke the rust compiler on the path of the given exercise
// and run the ensuing binary.
// This is strictly for non-test binaries, so output is displayed
fn test_cairo(exercise: &Exercise) -> Result<(), ()> {
println!("\nTesting {exercise}...\n");
let output = exercise.test();

if let Some(error) = output.as_ref().err() {
println!("{error}");
Err(())
} else {
let message = output.unwrap();
println!("{message}");
success!("Successfully tested {}", exercise);
Ok(())
}
}
10 changes: 8 additions & 2 deletions src/scarb.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{env::current_dir, fs, path::PathBuf};

use anyhow::Context;
use cairo_lang_runner::{RunResultValue, SierraCasmRunner, StarknetState};
use cairo_lang_sierra::program::VersionedProgram;
use cairo_lang_test_plugin::TestCompilation;
use cairo_lang_test_runner::{CompiledTestRunner, TestRunConfig};
use camino::Utf8PathBuf;
use console::style;
use std::{env::current_dir, fs, path::PathBuf};

use itertools::Itertools;
use scarb::{
Expand Down Expand Up @@ -54,6 +54,12 @@ pub fn scarb_run(file_path: &PathBuf) -> anyhow::Result<String> {
// Compile before running tests, with test targets true
compile(&config, false)?;

println!(
" {} {}\n",
style("Running").green().bold(),
file_path.to_str().unwrap()
);

let metadata = collect_metadata(
&MetadataOptions {
version: 1,
Expand Down
23 changes: 23 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ macro_rules! warn {
use console::{style, Emoji};
use std::env;
let formatstr = format!($fmt, $ex);
println!();
if env::var("NO_EMOJI").is_ok() {
println!("{} {}", style("!").red(), style(formatstr).red());
} else {
Expand All @@ -12,6 +13,7 @@ macro_rules! warn {
style(formatstr).red()
);
}
println!();
}};
}

Expand All @@ -20,6 +22,7 @@ macro_rules! success {
use console::{style, Emoji};
use std::env;
let formatstr = format!($fmt, $ex);
println!();
if env::var("NO_EMOJI").is_ok() {
println!("{} {}", style("✓").green(), style(formatstr).green());
} else {
Expand All @@ -29,5 +32,25 @@ macro_rules! success {
style(formatstr).green()
);
}
println!();
}};
}

macro_rules! progress {
($fmt:literal, $ex:expr) => {{
use console::{style, Emoji};
use std::env;
let formatstr = format!($fmt, $ex);
println!();
if env::var("NO_EMOJI").is_ok() {
println!("{} {}", style("○").yellow(), style(formatstr).yellow());
} else {
println!(
"{} {}",
style(Emoji("🟡", "○")).yellow(),
style(formatstr).yellow()
);
}
println!();
}};
}
71 changes: 71 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use console::style;

use crate::exercise::{Exercise, Mode};
// use crate::ui::progress;

// Build the given Exercise and return an object with information
// about the state of the compilation
pub fn build_exercise(exercise: &Exercise) -> Result<String, ()> {
progress!("Building {} exercise...", exercise);

let compilation_result = exercise.build();

if let Err(error) = compilation_result {
eprintln!("{error}");

warn!("Compiling of {} failed! Please try again.", exercise);
Err(())
} else {
Ok(compilation_result.unwrap())
}
}

// Build the given Exercise and return an object with information
// about the state of the compilation
pub fn run_exercise(exercise: &Exercise) -> Result<String, ()> {
progress!("Running {} exercise...", exercise);

let compilation_result = exercise.run();

if let Err(error) = compilation_result {
eprintln!("{error}");

warn!("Failed to run {}! Please try again.", exercise);
Err(())
} else {
Ok(compilation_result.unwrap())
}
}

// Tests the given Exercise and return an object with information
// about the state of the tests
pub fn test_exercise(exercise: &Exercise) -> Result<String, ()> {
progress!("Testing {} exercise...", exercise);

let compilation_result = exercise.test();

if let Some(error) = compilation_result.as_ref().err() {
warn!(
"Testing of {} failed! Please try again. Here's the output:",
exercise
);
println!("{error}");
Err(())
} else {
Ok(compilation_result.unwrap())
}
}

pub fn print_exercise_output(exercise_output: String) {
if exercise_output.len() > 0 {
println!(" {} {exercise_output}", style("Output").green().bold());
}
}

pub fn print_exercise_success(exercise: &Exercise) {
match exercise.mode {
Mode::Build => success!("Successfully built {}!", exercise),
Mode::Run => success!("Successfully ran {}!", exercise),
Mode::Test => success!("Successfully tested {}!", exercise),
}
}
Loading

0 comments on commit ab4d25a

Please sign in to comment.