fix rust run cmd

This commit is contained in:
Sandipsinh Rathod 2024-11-17 20:18:39 -05:00
parent a11529717c
commit 2a8251a693
3 changed files with 26 additions and 8 deletions

@ -73,7 +73,7 @@ fn generate(dir_path: &str, lang: &Language) -> anyhow::Result<()> {
]; ];
InnerGenerator::new(cpp_files) InnerGenerator::new(cpp_files)
} }
Language::Rust => { Language::Rs => {
let rust_files = vec![crate::r#static::MAIN_RS]; let rust_files = vec![crate::r#static::MAIN_RS];
InnerGenerator::new(rust_files) InnerGenerator::new(rust_files)
} }

@ -4,7 +4,7 @@ use clap::ValueEnum;
pub enum Language { pub enum Language {
#[default] #[default]
Cpp, Cpp,
Rust, Rs,
} }
impl Language { impl Language {

@ -27,6 +27,9 @@ impl Runner {
.split("/") .split("/")
.last() .last()
.ok_or(anyhow::anyhow!("No file name"))?; .ok_or(anyhow::anyhow!("No file name"))?;
let file_path = (std::env::current_dir()
.map_err(|_| anyhow::anyhow!("Cannot get current directory"))?)
.join(&self.file_path);
match lang { match lang {
Language::Cpp => { Language::Cpp => {
@ -35,17 +38,32 @@ impl Runner {
.arg("-o") .arg("-o")
.arg("main") .arg("main")
.arg("-DLOCAL") .arg("-DLOCAL")
.arg(&self.file_path) .arg(file_path)
.output()?; .output()?;
} }
Language::Rust => { Language::Rs => {
Command::new("rustc") if !file_path.is_file() {
.arg(file_name) return Err(anyhow::anyhow!("File not found: {}", file_path.display()));
.arg("--cfg") }
.arg(r#"'feature="local"'"#)
let output = Command::new("rustc")
.arg(
file_path
.to_str()
.ok_or(anyhow::anyhow!("Invalid file name"))?,
)
.arg("-o") .arg("-o")
.arg("main") .arg("main")
.arg("--cfg")
.arg("feature=\"local\"")
.output()?; .output()?;
if !output.stderr.is_empty() {
return Err(anyhow::anyhow!(
"Compilation failed: {}",
String::from_utf8(output.stderr)?
));
}
println!("{}", String::from_utf8(output.stdout)?);
} }
}; };