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)
}
Language::Rust => {
Language::Rs => {
let rust_files = vec![crate::r#static::MAIN_RS];
InnerGenerator::new(rust_files)
}

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

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