From ad065d1ef8c93915979e10267040b8a8207c0b83 Mon Sep 17 00:00:00 2001 From: K V Varun Krishnan Date: Sun, 7 Jan 2024 15:40:35 +0530 Subject: [PATCH] Piston API working, need to make few changes --- README.md | 12 ++++ src/bin/temp.rs | 154 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 151 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5a643d7..0fdd02d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ +

kRusty Krab Code

+ +

This is a small GUI app, built with Slint UI framework and the Rust Programming language. This app lets users write small snippets of rust code and run it using the machine, without installing rustc/cargo on their machine.

+ +

For completing the project, I have used the API of Rust playground and used reqwest crate to execute

+ +

Big Thanks to github.com/menard-codes which i refered for completing the project.

+ +

As well as youtube.com/@TravisMediab> for inspiring me in building a gui with rust

+ + + # Slint Rust Template A template for a Rust application that's using [Slint](https://slint.rs) for the user interface. diff --git a/src/bin/temp.rs b/src/bin/temp.rs index c25231d..702f23e 100644 --- a/src/bin/temp.rs +++ b/src/bin/temp.rs @@ -1,37 +1,161 @@ use std::collections::HashMap; use reqwest; -use serde_json::json; +use serde::{Serialize, Deserialize}; +use piston_rs; + +enum Args { + Text(String), + Bool(bool), +} + +impl Serialize for Args { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + Args::Text(text) => serializer.serialize_str(text), + Args::Bool(boolean) => serializer.serialize_bool(*boolean), + } + } +} async fn compile_rust() -> Result<(), reqwest::Error> { let code = "//hello world! I'm Krusty the Krab and i like money lol. \n \n//Insert your rusty code over here and i will do the rest\n \n//Also,I tell NO to code that asks input from user \n \n \nfn main(){\n println!(6969696); //write code here\n}"; - let url = "https://play.rust-lang.org/compile"; - let mut data = HashMap::new(); - data.insert("channel", "stable"); - data.insert("mode", "debug"); - data.insert("edition", "2021"); - data.insert("crateType", "bin"); - data.insert("code", code); + //let mut data:HashMap<&str, Args> = HashMap::new(); + // rust playground api + // let url = "https://play.rust-lang.org/execute"; + // data.insert("channel", Args::Text(String::from("stable"))); + // data.insert("mode", Args::Text(String::from("debug"))); + // data.insert("edition", Args::Text(String::from("2021"))); + // data.insert("crateType", Args::Text(String::from("bin"))); + // data.insert("tests", Args::Bool(false)); + // data.insert("code", Args::Text(String::from(code))); + // data.insert("backtrace", Args::Bool(false)); + + + //piston api + let mut data:HashMap<&str, &str> = HashMap::new(); + let url = "https://emkc.org/api/v2/piston/execute"; + + data.insert("files", code); + data.insert("version", "1.67.1"); + data.insert("language", "rust"); + let client = reqwest::Client::new(); let res = client.post(url) .header(reqwest::header::CONTENT_TYPE, "application/json") - .json(&json!(data)) // Convert the HashMap to serde_json::Value using json!() + .json(&data) // Convert the HashMap to serde_json::Value using json!() .send() .await?; -println!("{:?}", res); - + // let response_json: serde_json::Value = res.json().await?; + println!("{:#?}", data); + println!("{:#?}", res); Ok(()) } #[tokio::main] async fn main() { - if let Err(err) = compile_rust().await { - // Handle the error here - eprintln!("Error: {:?}", err); - } + // if let Err(err) = compile_rust().await { + // // Handle the error here + // eprintln!("Error: {:?}", err); + // } + let code = "//hello world! I'm Krusty the Krab and i like money lol. \n \n//Insert your rusty code over here and i will do the rest\n \n//Also,I tell NO to code that asks input from user \n \n \nfn main(){\n println!(); //write code here\n}"; + let client = piston_rs::Client::new(); + let executor = piston_rs::Executor::new() + .set_language("rust") + .set_version("*") + .add_file(piston_rs::File::default() + .set_name("main.rs") + .set_content(code)); + + match client.execute(&executor).await { + Ok(response) => { + println!("Ok response, "); + println!("Language: {} \n \n",response.language); + println!("version: {} \n \n",response.version); + + if let Some(c) = &response.compile{ + println!("Compiled: {:#?} \n \n",response.compile); + } + println!("StdOut : {:#?} \n \n",response.run.output); + } + Err(err) =>{ + println!("Some error happend {:?}",err); + } + } } + + +//curl + +// { +// "channel": "stable", +// "mode": "debug", +// "edition": "2021", +// "crateType": "bin", +// "tests": False, +// "code": code, +// "backtrace": False +// } + + +// curl -X POST -H "Content-Type: application/json" \ +// -d '{ +// "channel": "stable", +// "mode": "debug", +// "edition": "2021", +// "crateType": "bin", +// "tests": false, +// "code": "//hello world! I'm Krusty the Krab and i like money lol. \n \n//Insert your rusty code over here and i will do the rest\n \n//Also,I tell NO to code that asks input from user \n \n \nfn main(){\n println!(6969696); //write code here\n}", +// "backtrace": false +// }' \ +// https://play.rust-lang.org/execute + +//piston api + + +// "files": "//hello world! I'm Krusty the Krab and i like money lol. \n \n//Insert your rusty code over here and i will do the rest\n \n//Also,I tell NO to code that asks input from user \n \n \nfn main(){\n println!(6969696); //write code here\n}", +// "version": "1.67.1", +// "language": "rust", +// } + +// curl -X POST -H "Content-Type: application/json" \ +// -d ' "files": "//hello world! Im Krusty the Krab and i like money lol. \n \n//Insert your rusty code over here and i will do the rest\n \n//Also,I tell NO to code that asks input from user \n \n \nfn main(){\n println!(6969696); //write code here\n}", +// "version": "1.67.1", +// "language": "rust", +// }' \ +// https://emkc.org/api/v2/piston/execute + +// let client = piston_rs::Client::new(); +// let executor = piston_rs::Executor::new() +// .set_language("rust") +// .set_version("*") +// .add_file( +// piston_rs::File::default() +// .set_name("main.rs") +// .set_content("fn main() { println!(\"42\"); }") +// ); + +// match client.execute(&executor).await { +// Ok(response) => { +// println!("Language: {}", response.language); +// println!("Version: {}", response.version); + +// if let Some(c) = response.compile { +// println!("Compilation: {}", c.output); +// } + +// println!("Output: {}", response.run.output); +// } +// Err(e) => { +// println!("Something went wrong contacting Piston."); +// println!("{}", e); +// } +// } \ No newline at end of file