Skip to content
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
3 changes: 2 additions & 1 deletion file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ impl Value {

/// Parses Hexadecimal, Octal and Unsigned Decimal
// TODO
#[allow(clippy::needless_match)]
#[allow(clippy::manual_map)] // TODO remove this
#[allow(clippy::needless_match)] // TODO remove this
fn parse_number(input: &mut String) -> Option<u64> {
if let Some(hex_num) = parse_hexadecimal(input) {
Some(hex_num)
Expand Down
19 changes: 10 additions & 9 deletions text/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

let output_format: OutputFormat = (&args).into();

let path1 = PathBuf::from(&args.file1);
let path2 = PathBuf::from(&args.file2);

Expand All @@ -124,16 +122,19 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
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,
);
let format_options = format_options.unwrap();

let path1_is_file = fs::metadata(&path1)?.is_file();
let path2_is_file = fs::metadata(&path2)?.is_file();

let format_options = FormatOptions {
ignore_trailing_white_spaces: args.ignore_eol_space,
label1: args.label,
label2: args.label2,
output_format: output_format,
};

if path1_is_file && path2_is_file {
return FileDiff::file_diff(path1, path2, &format_options, None);
} else if !path1_is_file && !path2_is_file {
Expand Down
32 changes: 30 additions & 2 deletions text/diff_util/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
pub struct FormatOptions {
pub ignore_trailing_white_spaces: bool,
pub output_format: OutputFormat,
pub label1: Option<String>,
pub label2: Option<String>,
label1: Option<String>,
label2: Option<String>,
}

impl FormatOptions {
pub fn try_new(
ignore_trailing_white_spaces: bool,
output_format: OutputFormat,
label1: Option<String>,
label2: Option<String>,
) -> Result<Self, &'static str> {
if label1.is_none() && label2.is_some() {
return Err("label1 can not be NONE when label2 is available");
}

Ok(Self {
ignore_trailing_white_spaces,
output_format,
label1,
label2,
})
}

pub fn label1(&self) -> &Option<String> {
&self.label1
}

pub fn label2(&self) -> &Option<String> {
&self.label2
}
}

#[allow(dead_code)]
Expand Down
15 changes: 6 additions & 9 deletions text/diff_util/dir_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ pub struct DirData {
}

impl DirData {
pub fn load(path: PathBuf) -> io::Result<DirData> {
let mut dir_data = DirData {
path: path,
files: Default::default(),
};
let entries = fs::read_dir(&dir_data.path)?;
pub fn load(path: PathBuf) -> io::Result<Self> {
let mut files: HashMap<OsString, DirEntry> = Default::default();

let entries = fs::read_dir(&path)?;

for entry in entries {
let entry = entry?;

dir_data.files.insert(entry.file_name(), entry);
files.insert(entry.file_name(), entry);
}

Ok(dir_data)
Ok(Self { path, files })
}

pub fn files(&self) -> &HashMap<OsString, DirEntry> {
Expand Down
8 changes: 4 additions & 4 deletions text/diff_util/dir_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ impl<'a> DirDiff<'a> {
show_if_different.push_str("-b ");
}

if let Some(label1) = &self.format_options.label1 {
if let Some(label1) = &self.format_options.label1() {
show_if_different.push_str(format!("--label {} ", label1).as_str())
}

if let Some(label2) = &self.format_options.label2 {
if let Some(label2) = &self.format_options.label2() {
show_if_different.push_str(format!("--label2 {} ", label2).as_str())
}

if let Some(label1) = &self.format_options.label1 {
if let Some(label1) = &self.format_options.label1() {
show_if_different.push_str(format!("{} ", label1).as_str())
} else {
show_if_different
.push_str(path1.to_str().unwrap_or(COULD_NOT_UNWRAP_FILENAME));
show_if_different.push(' ');
}

if let Some(label2) = &self.format_options.label2 {
if let Some(label2) = &self.format_options.label2() {
show_if_different.push_str(format!("{} ", label2).as_str())
} else {
show_if_different
Expand Down
22 changes: 9 additions & 13 deletions text/diff_util/file_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,24 @@ pub struct FileDiff<'a> {
}

impl<'a> FileDiff<'a> {
pub fn are_different(&self) -> bool {
self.are_different
}

fn new(
file1: &'a mut FileData,
file2: &'a mut FileData,
format_options: &'a FormatOptions,
) -> Self {
if format_options.label1.is_none() && format_options.label2.is_some() {
panic!("label1 can not be NONE when label2 is available");
}

Self {
file1,
file2,
hunks: Hunks::new(),
format_options: format_options,
format_options,
are_different: false,
}
}

pub fn are_different(&self) -> bool {
self.are_different
}

pub fn file_diff(
path1: PathBuf,
path2: PathBuf,
Expand Down Expand Up @@ -362,11 +358,11 @@ impl<'a> FileDiff<'a> {
fn print_context(&mut self, context: usize) {
println!(
"*** {}",
Self::get_header(self.file1, &self.format_options.label1)
Self::get_header(self.file1, &self.format_options.label1())
);
println!(
"--- {}",
Self::get_header(self.file2, &self.format_options.label2)
Self::get_header(self.file2, &self.format_options.label2())
);

let change_ranges = self.get_context_ranges(context);
Expand Down Expand Up @@ -423,11 +419,11 @@ impl<'a> FileDiff<'a> {
fn print_unified(&mut self, unified: usize) {
println!(
"--- {}",
Self::get_header(self.file1, &self.format_options.label1)
Self::get_header(self.file1, &self.format_options.label1())
);
println!(
"+++ {}",
Self::get_header(self.file2, &self.format_options.label2)
Self::get_header(self.file2, &self.format_options.label2())
);

let context_ranges = self.get_context_ranges(unified);
Expand Down