first framework

This commit is contained in:
Robert Altner 2022-11-20 22:59:03 +01:00
parent fc5a1a74eb
commit bfdcc1ebef
5 changed files with 305 additions and 2 deletions

194
Cargo.lock generated
View file

@ -2,6 +2,200 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "4.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2148adefda54e14492fb9bddcc600b4344c5d1a3123bd666dcb939c6f0e0e57e"
dependencies = [
"atty",
"bitflags",
"clap_lex",
"strsim",
"termcolor",
]
[[package]]
name = "clap_derive"
version = "4.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "diff-tool"
version = "0.1.0"
dependencies = [
"clap",
"clap_derive",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "os_str_bytes"
version = "6.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5bf27447411e9ee3ff51186bf7a08e16c341efdde93f4d823e8844429bed7e"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View file

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "4.0.26"
clap_derive = "4.0.21"

53
src/diff.rs Normal file
View 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
View 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]
}
}

View file

@ -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;
}
};
}