Skip to content

Commit

Permalink
Refactor: parse file meta line
Browse files Browse the repository at this point in the history
In preparation for #392
  • Loading branch information
dandavison committed Nov 23, 2020
1 parent 54f363d commit f52464c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
15 changes: 12 additions & 3 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
let mut painter = Painter::new(writer, config);
let mut minus_file = "".to_string();
let mut plus_file = "".to_string();
let mut file_event = parse::FileEvent::NoEvent;
let mut state = State::Unknown;
let mut source = Source::Unknown;

Expand Down Expand Up @@ -99,7 +100,11 @@ where
} else if (state == State::FileMeta || source == Source::DiffUnified)
&& (line.starts_with("--- ") || line.starts_with("rename from "))
{
minus_file = parse::get_file_path_from_file_meta_line(&line, source == Source::GitDiff);
let parsed_file_meta_line =
parse::parse_file_meta_line(&line, source == Source::GitDiff);
minus_file = parsed_file_meta_line.0;
file_event = parsed_file_meta_line.1;

if source == Source::DiffUnified {
state = State::FileMeta;
painter.set_syntax(parse::get_file_extension_from_marker_line(&line));
Expand All @@ -111,7 +116,9 @@ where
} else if (state == State::FileMeta || source == Source::DiffUnified)
&& (line.starts_with("+++ ") || line.starts_with("rename to "))
{
plus_file = parse::get_file_path_from_file_meta_line(&line, source == Source::GitDiff);
let parsed_file_meta_line =
parse::parse_file_meta_line(&line, source == Source::GitDiff);
plus_file = parsed_file_meta_line.0;
painter.set_syntax(parse::get_file_extension_from_file_meta_line_file_path(
&plus_file,
));
Expand All @@ -125,6 +132,7 @@ where
&minus_file,
&plus_file,
config,
&file_event,
source == Source::DiffUnified,
)?;
handled_file_meta_header_line_file_pair = current_file_pair
Expand Down Expand Up @@ -288,10 +296,11 @@ fn handle_file_meta_header_line(
minus_file: &str,
plus_file: &str,
config: &Config,
file_event: &parse::FileEvent,
comparing: bool,
) -> std::io::Result<()> {
let line = parse::get_file_change_description_from_file_paths(
minus_file, plus_file, comparing, config,
minus_file, plus_file, comparing, file_event, config,
);
// FIXME: no support for 'raw'
handle_generic_file_meta_header_line(painter, &line, &line, config)
Expand Down
83 changes: 49 additions & 34 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,36 @@ pub fn get_file_extension_from_marker_line(line: &str) -> Option<&str> {
.and_then(|file| file.split('.').last())
}

pub fn get_file_path_from_file_meta_line(line: &str, git_diff_name: bool) -> String {
#[derive(Debug, PartialEq)]
pub enum FileEvent {
Change,
Rename,
NoEvent,
}

pub fn parse_file_meta_line(line: &str, git_diff_name: bool) -> (String, FileEvent) {
match line {
line if line.starts_with("--- ") || line.starts_with("+++ ") => {
let offset = 4;
match &line[offset..] {
let file = match &line[offset..] {
path if path == "/dev/null" => "/dev/null",
path if git_diff_name && DIFF_PREFIXES.iter().any(|s| path.starts_with(s)) => {
&path[2..]
}
path if git_diff_name => &path,
path => path.split('\t').next().unwrap_or(""),
}
.to_string();
(file, FileEvent::Change)
}
line if line.starts_with("rename from ") => {
&line[12..] // "rename from ".len()
(line[12..].to_string(), FileEvent::Rename) // "rename from ".len()
}
line if line.starts_with("rename to ") => {
&line[10..] // "rename to ".len()
(line[10..].to_string(), FileEvent::Rename) // "rename to ".len()
}
_ => "",
_ => ("".to_string(), FileEvent::NoEvent),
}
.to_string()
}

pub fn get_file_extension_from_file_meta_line_file_path(path: &str) -> Option<&str> {
Expand All @@ -55,6 +63,7 @@ pub fn get_file_change_description_from_file_paths(
minus_file: &str,
plus_file: &str,
comparing: bool,
file_event: &FileEvent,
config: &Config,
) -> String {
if comparing {
Expand Down Expand Up @@ -92,7 +101,10 @@ pub fn get_file_change_description_from_file_paths(
),
(minus_file, plus_file) => format!(
"{}{} ⟶ {}",
format_label(&config.file_renamed_label),
format_label(match file_event {
FileEvent::Rename => &config.file_renamed_label,
_ => "",
}),
format_file(minus_file),
format_file(plus_file)
),
Expand Down Expand Up @@ -224,74 +236,77 @@ mod tests {
#[test]
fn test_get_file_path_from_git_file_meta_line() {
assert_eq!(
get_file_path_from_file_meta_line("--- /dev/null", true),
"/dev/null"
parse_file_meta_line("--- /dev/null", true),
("/dev/null".to_string(), FileEvent::Change)
);
for prefix in &DIFF_PREFIXES {
assert_eq!(
get_file_path_from_file_meta_line(&format!("--- {}src/delta.rs", prefix), true),
"src/delta.rs"
parse_file_meta_line(&format!("--- {}src/delta.rs", prefix), true),
("src/delta.rs".to_string(), FileEvent::Change)
);
}
assert_eq!(
get_file_path_from_file_meta_line("--- src/delta.rs", true),
"src/delta.rs"
parse_file_meta_line("--- src/delta.rs", true),
("src/delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ src/delta.rs", true),
"src/delta.rs"
parse_file_meta_line("+++ src/delta.rs", true),
("src/delta.rs".to_string(), FileEvent::Change)
);
}

#[test]
fn test_get_file_path_from_git_file_meta_line_containing_spaces() {
assert_eq!(
get_file_path_from_file_meta_line("+++ a/my src/delta.rs", true),
"my src/delta.rs"
parse_file_meta_line("+++ a/my src/delta.rs", true),
("my src/delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ my src/delta.rs", true),
"my src/delta.rs"
parse_file_meta_line("+++ my src/delta.rs", true),
("my src/delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ a/src/my delta.rs", true),
"src/my delta.rs"
parse_file_meta_line("+++ a/src/my delta.rs", true),
("src/my delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ a/my src/my delta.rs", true),
"my src/my delta.rs"
parse_file_meta_line("+++ a/my src/my delta.rs", true),
("my src/my delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ b/my src/my enough/my delta.rs", true),
"my src/my enough/my delta.rs"
parse_file_meta_line("+++ b/my src/my enough/my delta.rs", true),
(
"my src/my enough/my delta.rs".to_string(),
FileEvent::Change
)
);
}

#[test]
fn test_get_file_path_from_git_file_meta_line_rename() {
assert_eq!(
get_file_path_from_file_meta_line("rename from nospace/file2.el", true),
"nospace/file2.el"
parse_file_meta_line("rename from nospace/file2.el", true),
("nospace/file2.el".to_string(), FileEvent::Rename)
);
}

#[test]
fn test_get_file_path_from_git_file_meta_line_rename_containing_spaces() {
assert_eq!(
get_file_path_from_file_meta_line("rename from with space/file1.el", true),
"with space/file1.el"
parse_file_meta_line("rename from with space/file1.el", true),
("with space/file1.el".to_string(), FileEvent::Rename)
);
}

#[test]
fn test_get_file_path_from_file_meta_line() {
fn test_parse_file_meta_line() {
assert_eq!(
get_file_path_from_file_meta_line("--- src/delta.rs", false),
"src/delta.rs"
parse_file_meta_line("--- src/delta.rs", false),
("src/delta.rs".to_string(), FileEvent::Change)
);
assert_eq!(
get_file_path_from_file_meta_line("+++ src/delta.rs", false),
"src/delta.rs"
parse_file_meta_line("+++ src/delta.rs", false),
("src/delta.rs".to_string(), FileEvent::Change)
);
}

Expand Down

0 comments on commit f52464c

Please sign in to comment.