# thediff
A small Rust crate for calculating how different two files are, expressed as a **percentage**.
It is useful for file comparison, integrity checks, similarity analysis, or detecting changes
between binary or text files.
---
## Installation
Add the crate to your `Cargo.toml`:
```toml
[dependencies]
thediff = "0.1"
```
---
## Usage
### Compare Two Strings
```rust
use thediff::thediff_strings;
fn main() -> std::io::Result<()> {
let (diff_percent, same_percent) = thediff_strings("0101010", "01010111")?;
println!(
"Different part: {:.2}%, Same part: {:.2}%",
diff_percent, same_percent
);
Ok(())
}
```
### Compare Two Files
```rust
use std::path::Path;
use thediff::thediff_files;
fn main() -> std::io::Result<()> {
let (diff_percent, same_percent) = thediff_files(Path::new("file1.bin"), Path::new("file2.bin"))?;
println!(
"Different part: {:.2}%, Same part: {:.2}%",
diff_percent, same_percent
);
Ok(())
}
```
```rust
use std::path::Path;
use thediff::thediff_files_async;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let (diff_percent, same_percent) =
thediff_files_async(
Path::new("file1.bin"),
Path::new("file2.bin"),
)
.await?;
println!(
"Different part: {:.2}%, Same part: {:.2}%",
diff_percent, same_percent
);
Ok(())
}
```
---
## How It Works
The crate compares files **utf8 char by char**:
- Counts how many chars differ
- Handles files of **different sizes**
- Extra bytes in the longer file are treated as differences
### Examples
| Identical | Identical | `0%` |
| 50% bytes differ | Same length | `50%` |
| One file empty | Other non-empty | `100%` |
---