Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ license-file = "LICENSE"
[dependencies]
iced = { git = "https://github.com/iced-rs/iced" }
native-dialog = "0.6.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[profile.release]
lto = true
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![GitHub](https://img.shields.io/badge/language-Rust-green)
![GitHub](https://img.shields.io/github/license/CodeDead/text-diff-rs)

![text-diff](https://i.imgur.com/PxbT90B.png)
![text-diff](https://i.imgur.com/VrcSyMD.png)

`text-diff` can be used to compare two text files and output the difference between them. `text-diff` was written in `Rust` and offers a simple and intuitive user-interface for Windows, Linux and macOS.

Expand Down Expand Up @@ -40,6 +40,8 @@ cargo run
* [Rust](https://www.rust-lang.org/)
* [iced](https://iced.rs/)
* [native-dialog](https://github.com/balthild/native-dialog-rs)
* [serde](https://serde.rs/)
* [serde_json](https://serde.rs/)

## About

Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use iced::window;
use iced::{Sandbox, Settings};

mod view;
mod style;
mod filereader;
mod style;
mod vector_comparer;
mod vector_exporter;
mod view;

pub fn main() -> iced::Result {
view::ApplicationContext::run(Settings {
id: Some(String::from("text-diff")),
Expand Down
2 changes: 1 addition & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ mod dark {
a: 0.8,
..if is_checked { ACTIVE } else { SURFACE }
}
.into(),
.into(),
..self.active(is_checked)
}
}
Expand Down
78 changes: 78 additions & 0 deletions src/vector_comparer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pub trait IVectorComparer<T> {
fn new(vec1: Vec<T>, vec2: Vec<T>) -> Self;
fn get_differences(&self) -> Vec<T>;
}

#[derive(Debug, Clone)]
pub struct VectorComparer<T> {
pub vec1: Vec<T>,
pub vec2: Vec<T>,
}

impl IVectorComparer<String> for VectorComparer<String> {
/// Initialize a new VectorComparer for type `String`
///
/// # Example
///
/// ```rust
/// let vector_comparer: VectorComparer<String> = IVectorComparer::<String>::new(vec![], vec![]);
/// ```
///
/// # Returns
///
/// A `VectorComparer` that can be used to compare two `Vec` structs of type `String`
fn new(vec1: Vec<String>, vec2: Vec<String>) -> VectorComparer<String> {
VectorComparer::<String> { vec1, vec2 }
}

/// Get the differences between the two given `Vec` structs of type `String`
///
/// # Example
///
/// ```rust
/// let differences: Vec<String> = vector_comparer.get_differences();
/// ```
///
/// # Returns
///
/// A `Vec` struct of type `String` that contains the differences between the two given `Vec` structs of type `String`
fn get_differences(&self) -> Vec<String> {
if self.vec1.is_empty() {
return self.vec2.clone();
} else if self.vec2.is_empty() {
return self.vec1.clone();
}

let mut diff = vec![];
for f in &self.vec1 {
let mut included = false;
for d in &self.vec2 {
if f.eq(d) {
included = true;
}
}

if !included {
diff.push(String::from(f));
}
}

for f in &self.vec2 {
let mut included = false;
for d in &self.vec1 {
if f.eq(d) {
included = true;
}
}

if !included {
let n = String::from(f);
if !diff.contains(&n) {
diff.push(n);
}
}
}

diff
}
}
107 changes: 107 additions & 0 deletions src/vector_exporter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use std::{fs::File, io::Write};

#[derive(Debug, Clone)]
pub enum ExportType {
Text,
Csv,
Json,
}

pub enum ExportError {
IoError(std::io::Error),
JsonError(serde_json::Error),
}

impl Default for ExportType {
fn default() -> Self {
ExportType::Text
}
}

pub trait IVectorExporter<T> {
fn new(vec: Vec<T>, export_type: ExportType, export_type: &str) -> Self;

fn export(&self) -> Result<(), ExportError>;
}

#[derive(Debug, Clone)]
pub struct VectorExporter<T> {
pub vec: Vec<T>,
pub export_type: ExportType,
pub export_path: String,
}

impl IVectorExporter<String> for VectorExporter<String> {
/// Initialize a new `VectorExporter` for type `String`
///
/// # Example
///
/// ```rust
/// let vec_exporter: VectorExporter<String> = IVectorExporter::<String>::new(vec![], ExportType::default(), "/path/to/file");
/// ```
///
/// # Returns
///
/// The `VectorExporter` struct for type `String`
fn new(vec: Vec<String>, export_type: ExportType, export_path: &str) -> VectorExporter<String> {
VectorExporter {
vec,
export_type,
export_path: String::from(export_path),
}
}

/// Export the `Vec` of type `String` to a file
///
/// # Example
///
/// ```rust
/// let res = vec_exporter.export();
/// ```
///
/// # Returns
///
/// A `Result` that can either contain an `Ok` or an `Error` struct
fn export(&self) -> Result<(), ExportError> {
let file = File::create(&self.export_path);
let mut file = match file {
Ok(file) => file,
Err(e) => return Err(ExportError::IoError(e)),
};
match self.export_type {
ExportType::Text => {
let mut data = String::new();
for l in &self.vec {
data.push_str(&format!("{}\n", l));
}

match write!(file, "{}", data) {
Ok(_) => Ok(()),
Err(e) => Err(ExportError::IoError(e)),
}
}
ExportType::Csv => {
let mut data = String::new();
for l in &self.vec {
data.push_str(&format!("\"{}\"\n", l));
}

match write!(file, "{}", data) {
Ok(_) => Ok(()),
Err(e) => Err(ExportError::IoError(e)),
}
}
ExportType::Json => {
let serialized = match serde_json::to_string(&self.vec) {
Ok(d) => d,
Err(e) => return Err(ExportError::JsonError(e)),
};

match write!(file, "{}", serialized) {
Ok(_) => Ok(()),
Err(e) => Err(ExportError::IoError(e)),
}
}
}
}
}
Loading