first framework
This commit is contained in:
parent
fc5a1a74eb
commit
bfdcc1ebef
5 changed files with 305 additions and 2 deletions
53
src/diff.rs
Normal file
53
src/diff.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::{fs::File, io::{BufReader, Read}};
|
||||
|
||||
use crate::grid::Grid;
|
||||
|
||||
macro_rules! safe_unwrap {
|
||||
($expression: expr) => {
|
||||
match $expression {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(err.to_string())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
enum Arrow {
|
||||
#[default] LEFT,
|
||||
UP,
|
||||
DIAGONAL
|
||||
}
|
||||
|
||||
fn open_and_read(file: String) -> Result<String, std::io::Error> {
|
||||
let fp = File::open(file)?;
|
||||
let mut buf_reader = BufReader::new(fp);
|
||||
let mut content = String::new();
|
||||
|
||||
buf_reader.read_to_string(&mut content)?;
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
pub struct Diff {
|
||||
first: String,
|
||||
second: String,
|
||||
}
|
||||
|
||||
impl Diff {
|
||||
pub fn new(first: String, second: String) -> Result<Diff, String> {
|
||||
let first_string = safe_unwrap!(open_and_read(first));
|
||||
let second_string = safe_unwrap!(open_and_read(second));
|
||||
|
||||
let mut diff = Diff {
|
||||
first: first_string,
|
||||
second: second_string
|
||||
};
|
||||
|
||||
|
||||
|
||||
Ok(diff)
|
||||
}
|
||||
|
||||
fn create_lcs(&self) -> Grid<Arrow> {
|
||||
Grid::new(self.first.len() as u32, self.second.len() as u32)
|
||||
}
|
||||
}
|
33
src/grid.rs
Normal file
33
src/grid.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use std::ops;
|
||||
|
||||
pub struct Grid<T: Default + Clone> {
|
||||
width: usize,
|
||||
height: usize,
|
||||
|
||||
buf: Vec<T>
|
||||
}
|
||||
|
||||
impl<T: Default + Clone> Grid<T> {
|
||||
pub fn new(width: u32, height: u32) -> Grid<T> {
|
||||
Grid {
|
||||
width: width as usize,
|
||||
height: height as usize,
|
||||
|
||||
buf: vec![T::default(); width as usize * height as usize]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default + Clone> ops::Index<(u32, u32)> for Grid<T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: (u32, u32)) -> &Self::Output {
|
||||
&self.buf[index.0 as usize * self.width + index.1 as usize]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default + Clone> ops::IndexMut<(u32, u32)> for Grid<T> {
|
||||
fn index_mut(&mut self, index: (u32, u32)) -> &mut Self::Output {
|
||||
&mut self.buf[index.0 as usize * self.width + index.1 as usize]
|
||||
}
|
||||
}
|
25
src/main.rs
25
src/main.rs
|
@ -1,3 +1,24 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod diff;
|
||||
mod grid;
|
||||
|
||||
use clap::Parser;
|
||||
use clap_derive::Parser;
|
||||
use diff::Diff;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
first: String,
|
||||
second: String
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let diff = match Diff::new(args.first, args.second) {
|
||||
Ok(res) => res,
|
||||
Err(err) => {
|
||||
eprintln!("{err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue