Set up repo for Rust problems
This commit is contained in:
32
rust/src/main.rs
Normal file
32
rust/src/main.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
mod solutions;
|
||||
mod utils;
|
||||
use clap::Parser;
|
||||
use utils::run_solution;
|
||||
|
||||
#[derive(clap::ValueEnum, Clone, Debug)]
|
||||
enum Problem {
|
||||
REVP,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(
|
||||
name = "Rosalind Problems",
|
||||
version = "v0.0.0",
|
||||
author = "Peter Vlasveld <peter@vlasveld.info>",
|
||||
about = "Solves Problems from rosalind.info"
|
||||
)]
|
||||
struct Args {
|
||||
#[arg(short, long)]
|
||||
problem: Problem,
|
||||
|
||||
#[arg(short, long)]
|
||||
input_file: String,
|
||||
|
||||
#[arg(short, long, default_value = "output.txt")]
|
||||
output_file: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
run_solution(args.problem, &args.input_file, &args.output_file);
|
||||
}
|
||||
2
rust/src/solutions/mod.rs
Normal file
2
rust/src/solutions/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod revp;
|
||||
pub use revp::revp;
|
||||
3
rust/src/solutions/revp.rs
Normal file
3
rust/src/solutions/revp.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub fn revp(file_content: String) -> String {
|
||||
"placeholder".to_string()
|
||||
}
|
||||
20
rust/src/utils.rs
Normal file
20
rust/src/utils.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::{solutions, Problem};
|
||||
use std::{fs, io};
|
||||
|
||||
pub fn run_solution(problem: Problem, in_file: &String, out_file: &String) {
|
||||
// parse the input file into a string
|
||||
let solution: io::Result<String> = match fs::read_to_string(in_file) {
|
||||
Ok(content) => match problem {
|
||||
Problem::REVP => Ok(solutions::revp(content)),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
|
||||
match solution {
|
||||
Ok(out_content) => match fs::write(out_file, &out_content) {
|
||||
Ok(_) => println!("{:?}", out_content),
|
||||
Err(e) => println!("Error writing to output file: {e}"),
|
||||
},
|
||||
Err(e) => println!("An error occured reading the input file: {e}"),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user