-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdiff.rs
167 lines (134 loc) · 4.59 KB
/
diff.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
// TODO:
// - Just a start with the core algorithm; -C and -U both need context output
// - Implement -r (recurse)
// - Research and implement -f alternate output format properly
//
mod diff_util;
use clap::Parser;
use diff_util::{
common::{FormatOptions, OutputFormat},
diff_exit_status::DiffExitStatus,
dir_diff::DirDiff,
file_diff::FileDiff,
functions::check_existence,
};
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use std::{fs, io, path::PathBuf};
/// diff - compare two files
#[derive(Parser, Clone)]
#[command(version, about)]
struct Args {
/// Cause EOL whitespace to be treated as blanks
#[arg(short = 'b', long = "ignore-space-change")]
ignore_eol_space: bool,
/// Output 3 lines of copied context
#[arg(short)]
context3: bool,
/// Output <N> lines of copied context
#[arg(short='C', value_parser = clap::value_parser!(u32).range(1..))]
context: Option<u32>,
/// Produce output in a form suitable as input for the ed utility
#[arg(short, long)]
ed: bool,
/// Produce output in an alternative form, similar in format to -e
#[arg(short)]
fed: bool,
/// Apply diff recursively to files and directories of the same name
#[arg(short, long)]
recurse: bool,
/// Print a message even when there are no differences between files
#[arg(short = 's', long = "report-identical-files")]
report_identical_files: bool,
/// Output 3 lines of unified context
#[arg(short)]
unified3: bool,
/// Output <N> lines of unified context
#[arg(short='U', value_parser = clap::value_parser!(u32).range(0..))]
unified: Option<u32>,
/// First comparison file (or directory, if -r is specified)
file1: String,
#[arg(long, value_parser= clap::value_parser!(String))]
label: Option<String>,
#[arg(long, value_parser= clap::value_parser!(String))]
label2: Option<String>,
/// Second comparison file (or directory, if -r is specified)
file2: String,
}
impl From<&Args> for OutputFormat {
fn from(args: &Args) -> Self {
let mut args = args.clone();
if args.context3 {
args.context = Some(3);
}
if args.unified3 {
args.unified = Some(3);
}
if args.ed {
OutputFormat::EditScript
} else if args.fed {
OutputFormat::ForwardEditScript
} else if let Some(n) = args.context {
let n = if n == 0 { 1 } else { n };
OutputFormat::Context(n as usize)
} else if let Some(n) = args.unified {
OutputFormat::Unified(n as usize)
} else {
OutputFormat::Default
}
}
}
fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
let path1 = PathBuf::from(args.file1.as_str());
let path2 = PathBuf::from(args.file2.as_str());
let path1_path = path1.as_path();
let path2_path = path2.as_path();
let path1_exists = check_existence(path1_path);
let path2_exists = check_existence(path2_path);
if !path1_exists || !path2_exists {
return Ok(DiffExitStatus::Trouble);
}
let output_format: OutputFormat = (&args).into();
let format_options = FormatOptions::try_new(
args.ignore_eol_space,
output_format,
args.label,
args.label2,
args.report_identical_files,
);
let format_options = format_options.unwrap();
let path1_is_file = fs::metadata(path1_path)?.is_file();
let path2_is_file = fs::metadata(path2_path)?.is_file();
if path1_is_file && path2_is_file {
FileDiff::file_diff(path1_path, path2_path, &format_options, None)
} else if !path1_is_file && !path2_is_file {
DirDiff::dir_diff(path1_path, path2_path, &format_options, args.recurse)
} else {
Ok(FileDiff::file_dir_diff(
path1_path,
path2_path,
&format_options,
)?)
}
}
fn main() -> DiffExitStatus {
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME")).unwrap();
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();
let args = Args::parse();
let result = check_difference(args);
match result {
Ok(diff_exit_status) => diff_exit_status,
Err(error) => {
eprintln!("diff: {error}");
DiffExitStatus::Trouble
}
}
}