From 59dcfd55829da3a606855d983a84559839d053b5 Mon Sep 17 00:00:00 2001 From: stranger80 Date: Sat, 11 Nov 2023 16:43:02 +0100 Subject: [PATCH 1/2] Fixed error processing for new cairo file --- api/src/handlers/compile_casm.rs | 3 +- api/src/handlers/compile_sierra.rs | 62 +++++++++++++++++++----------- api/src/handlers/process.rs | 1 + api/src/handlers/scarb_compile.rs | 2 +- api/src/handlers/scarb_test.rs | 2 +- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/api/src/handlers/compile_casm.rs b/api/src/handlers/compile_casm.rs index 8b96ae17..f0655342 100644 --- a/api/src/handlers/compile_casm.rs +++ b/api/src/handlers/compile_casm.rs @@ -11,8 +11,7 @@ use rocket::tokio::fs; use rocket::State; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -use tracing::info; -use tracing::instrument; +use tracing::{debug, info, instrument}; #[instrument] #[get("/compile-to-casm//")] diff --git a/api/src/handlers/compile_sierra.rs b/api/src/handlers/compile_sierra.rs index f56716f8..7406234a 100644 --- a/api/src/handlers/compile_sierra.rs +++ b/api/src/handlers/compile_sierra.rs @@ -11,7 +11,7 @@ use rocket::tokio::fs; use rocket::State; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -use tracing::{debug, instrument}; +use tracing::{debug, info, instrument}; #[instrument] #[get("/compile-to-sierra//")] @@ -138,16 +138,8 @@ pub async fn do_compile_to_sierra( .wait_with_output() .map_err(ApiError::FailedToReadOutput)?; - let file_content = fs::read_to_string( - NamedFile::open(&sierra_path) - .await - .map_err(ApiError::FailedToReadFile)? - .path() - .to_str() - .ok_or(ApiError::FailedToParseString)?, - ) - .await - .map_err(ApiError::FailedToReadFile)?; + + // Process the output messages let message = String::from_utf8(output.stderr) .map_err(ApiError::UTF8Error)? @@ -167,16 +159,42 @@ pub async fn do_compile_to_sierra( ); let status = match output.status.code() { - Some(0) => "Success", - Some(_) => "CompilationFailed", - None => "UnknownError", + Some(0) => "Success", + Some(_) => "CompilationFailed", + None => "UnknownError", + } + .to_string(); + + // Prepare response based on the compilation result + match output.status.code() { + Some(0) => { + let file_content = fs::read_to_string( + NamedFile::open(&sierra_path) + .await + .map_err(ApiError::FailedToReadFile)? + .path() + .to_str() + .ok_or(ApiError::FailedToParseString)?, + ) + .await + .map_err(ApiError::FailedToReadFile)?; + + Ok(Json(CompileResponse { + file_content, + message, + status, + cairo_version, + })) + + }, + _ => { + Ok(Json(CompileResponse { + file_content: String::from(""), + message, + status, + cairo_version, + })) + } } - .to_string(); - - Ok(Json(CompileResponse { - file_content, - message, - status, - cairo_version, - })) + } diff --git a/api/src/handlers/process.rs b/api/src/handlers/process.rs index ff6574dd..e0d67a34 100644 --- a/api/src/handlers/process.rs +++ b/api/src/handlers/process.rs @@ -2,6 +2,7 @@ use crate::handlers::types::{ApiCommand, ApiCommandResult}; use crate::worker::{ProcessState, WorkerEngine}; use rocket::State; use tracing::instrument; +use tracing::info; use uuid::Uuid; #[instrument] diff --git a/api/src/handlers/scarb_compile.rs b/api/src/handlers/scarb_compile.rs index 2bd686c5..e3b4a169 100644 --- a/api/src/handlers/scarb_compile.rs +++ b/api/src/handlers/scarb_compile.rs @@ -10,7 +10,7 @@ use rocket::serde::json::Json; use rocket::State; use std::path::PathBuf; use std::process::{Command, Stdio}; -use tracing::instrument; +use tracing::{debug, info, instrument}; #[instrument] #[get("/compile-scarb/")] diff --git a/api/src/handlers/scarb_test.rs b/api/src/handlers/scarb_test.rs index 4cd36993..3cd1c2f5 100644 --- a/api/src/handlers/scarb_test.rs +++ b/api/src/handlers/scarb_test.rs @@ -10,7 +10,7 @@ use rocket::serde::json::Json; use rocket::State; use std::path::PathBuf; use std::process::{Command, Stdio}; -use tracing::instrument; +use tracing::{debug, info, instrument}; #[instrument] #[get("/scarb-test-async/")] From 083d4e7dba212dd1eda15d1f7146438fec03f11b Mon Sep 17 00:00:00 2001 From: stranger80 Date: Sat, 11 Nov 2023 16:48:15 +0100 Subject: [PATCH 2/2] cargo fmt --- api/src/handlers/compile_sierra.rs | 31 +++++++++++++----------------- api/src/handlers/process.rs | 2 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/api/src/handlers/compile_sierra.rs b/api/src/handlers/compile_sierra.rs index 7406234a..c319175e 100644 --- a/api/src/handlers/compile_sierra.rs +++ b/api/src/handlers/compile_sierra.rs @@ -138,7 +138,6 @@ pub async fn do_compile_to_sierra( .wait_with_output() .map_err(ApiError::FailedToReadOutput)?; - // Process the output messages let message = String::from_utf8(output.stderr) @@ -159,12 +158,12 @@ pub async fn do_compile_to_sierra( ); let status = match output.status.code() { - Some(0) => "Success", - Some(_) => "CompilationFailed", - None => "UnknownError", - } - .to_string(); - + Some(0) => "Success", + Some(_) => "CompilationFailed", + None => "UnknownError", + } + .to_string(); + // Prepare response based on the compilation result match output.status.code() { Some(0) => { @@ -178,23 +177,19 @@ pub async fn do_compile_to_sierra( ) .await .map_err(ApiError::FailedToReadFile)?; - + Ok(Json(CompileResponse { file_content, message, status, cairo_version, })) - - }, - _ => { - Ok(Json(CompileResponse { - file_content: String::from(""), - message, - status, - cairo_version, - })) } + _ => Ok(Json(CompileResponse { + file_content: String::from(""), + message, + status, + cairo_version, + })), } - } diff --git a/api/src/handlers/process.rs b/api/src/handlers/process.rs index e0d67a34..379705d7 100644 --- a/api/src/handlers/process.rs +++ b/api/src/handlers/process.rs @@ -1,8 +1,8 @@ use crate::handlers::types::{ApiCommand, ApiCommandResult}; use crate::worker::{ProcessState, WorkerEngine}; use rocket::State; -use tracing::instrument; use tracing::info; +use tracing::instrument; use uuid::Uuid; #[instrument]