Skip to content

Commit

Permalink
Refactor source code parsing and updating code into functions
Browse files Browse the repository at this point in the history
- For parsing and converting source code to `String` variables, refactored into separate functions
- For removing and creating a file with updated code, refactored into separate function

Signed-off-by: Muhammad Mahad <[email protected]>
  • Loading branch information
MahadMuhammad authored and CohenArthur committed Aug 2, 2024
1 parent c9b3584 commit 430f19e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
34 changes: 33 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{fs, path};

use anyhow::{Context, Result};
use clap::Parser;
use std::path;

#[derive(Parser, Debug)]
#[command(
Expand All @@ -25,6 +27,36 @@ pub struct Arguments {
pub stderr_file: Option<path::PathBuf>,
}

pub fn parse_arguments_and_read_file(args: &Arguments) -> Result<(String, Option<String>)> {
//TODO: maybe to use sanitization to prevent reading files outside the project directory
let source_code = fs::read_to_string(&args.source_file)
.with_context(|| format!("could not read sourcefile `{}`", args.source_file.display()))?;

let err_file =
match &args.stderr_file {
Some(stderr_file) => Some(fs::read_to_string(stderr_file).with_context(|| {
format!("could not read stderr file `{}`", stderr_file.display())
})?),
None => None,
};

Ok((source_code, err_file))
}

pub fn update_source_code(args: &Arguments, new_code: String) -> Result<()> {
fs::remove_file(&args.source_file)
.with_context(|| format!("could not remove file `{}`", &args.source_file.display()))?;

fs::write(&args.source_file, new_code).with_context(|| {
format!(
"could not write updated code to file `{}`",
&args.source_file.display()
)
})?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 4 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
mod cli;
mod transform;

use anyhow::{Context, Result};
use clap::Parser;
use std::fs;

const RUSTTEST_ERROR: &str = "//~^ ERROR ";
const DG_ERROR: &str = "// { dg-error \"";
mod cli;
mod transform;

fn main() -> Result<()> {
try_parse()
Expand All @@ -15,8 +11,7 @@ fn main() -> Result<()> {
fn try_parse() -> Result<()> {
let args = cli::Arguments::parse();

let code = fs::read_to_string(&args.source_file)
.with_context(|| format!("could not read file `{}`", args.source_file.display()))?;
let (code, _stderr_code) = cli::parse_arguments_and_read_file(&args)?;

let new_code = transform::transform_code(&code).with_context(|| {
format!(
Expand All @@ -25,8 +20,7 @@ fn try_parse() -> Result<()> {
)
})?;

fs::remove_file(&args.source_file)?;
fs::write(&args.source_file, new_code.join("\n"))?;
cli::update_source_code(&args, new_code)?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;

// TODO: Add more directive relative to rustc and DejaGnu
pub const RUSTTEST_ERROR: &'static str = "//~^ ERROR ";
pub const DG_ERROR: &'static str = "// { dg-error \"";
pub const RUSTTEST_ERROR: &str = "//~^ ERROR ";
pub const DG_ERROR: &str = "// { dg-error \"";

/// This function takes the rust code and rust directive
/// and returns the code with DejaGnu directive
Expand Down

0 comments on commit 430f19e

Please sign in to comment.