Skip to content

Commit

Permalink
auto-infer Java class names
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodingwizard committed Jul 17, 2024
1 parent 19047e4 commit f9c1518
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
use anyhow::{anyhow, Context, Result};
use axum::Json;
use base64::{prelude::BASE64_STANDARD, Engine};
use regex::Regex;
use serde::{Deserialize, Serialize};
use tempdir::TempDir;

Expand Down Expand Up @@ -55,6 +56,9 @@ fn precompile_headers(compile_request: &CompileRequest) -> Result<()> {
|| !compile_request
.compiler_options
.contains(&format!("--std=c++{cpp_version}"))
|| !compile_request
.source_code
.contains("#include <bits/stdc++.h>")
{
return Ok(());
}
Expand Down Expand Up @@ -91,15 +95,22 @@ pub fn compile(compile_request: CompileRequest) -> Result<CompileResponse> {

let program_filename: PathBuf = match compile_request.language {
Language::Cpp => "program.cpp".into(),
Language::Java21 => "Main.java".into(),
Language::Java21 => {
let re = Regex::new(r"public\s+class\s+(\w+)").unwrap();
if let Some(captures) = re.captures(&compile_request.source_code) {
format!("{}.java", &captures[1]).into()
} else {
"Main.java".into() // fallback, something went wrong
}
}
Language::Py12 => "program.py".into(),
};

let mut source_file = File::create(tmp_dir.path().join(&program_filename))?;
source_file.write_all(compile_request.source_code.as_bytes())?;
drop(source_file);

if let Err(err) = precompile_headers(compile_request) {
if let Err(err) = precompile_headers(&compile_request) {
println!("Warning: Failed to precompile headers: {err}");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;

use axum::{
routing::{get, post}, Json, Router
routing::{get, post}, Router
};
use lambda_http::{run, tracing, Error};

Expand Down

0 comments on commit f9c1518

Please sign in to comment.