Skip to content

Commit

Permalink
simplified code by unwrapping write! to String, rather than bubble up…
Browse files Browse the repository at this point in the history
… errors that never occur; increment version to 0.5.0
  • Loading branch information
mlange-42 committed Feb 1, 2021
1 parent df3574b commit cbacf97
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-graph"
version = "0.4.3"
version = "0.5.0"
authors = ["Martin Lange <[email protected]>"]
description = "Command line tool to show clear git graphs arranged for your branching model"
repository = "https://github.com/mlange-42/git-graph.git"
Expand Down
76 changes: 36 additions & 40 deletions src/print/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,20 @@ pub fn format_commit(
let mut lines = vec![];
let mut out = String::new();
if replacements.is_empty() {
write!(out, "{}", format).map_err(|err| err.to_string())?;
write!(out, "{}", format).unwrap();
add_line(&mut lines, &mut out, &wrapping);
} else {
let mut curr = 0;
for (start, len, idx, mode) in replacements {
if idx == NEW_LINE {
write!(out, "{}", &format[curr..start]).map_err(|err| err.to_string())?;
write!(out, "{}", &format[curr..start]).unwrap();
add_line(&mut lines, &mut out, &wrapping);
} else {
write!(out, "{}", &format[curr..start]).map_err(|err| err.to_string())?;
write!(out, "{}", &format[curr..start]).unwrap();
match idx {
HASH => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -131,7 +131,7 @@ pub fn format_commit(
}
HASH_ABBREV => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -147,26 +147,21 @@ pub fn format_commit(
}
PARENT_HASHES => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
for i in 0..commit.parent_count() {
write!(
out,
"{}",
commit.parent_id(i).map_err(|err| err.to_string())?
)
.map_err(|err| err.to_string())?;
write!(out, "{}", commit.parent_id(i).unwrap()).unwrap();
if i < commit.parent_count() - 1 {
write!(out, " ").map_err(|err| err.to_string())?;
write!(out, " ").unwrap();
}
}
Ok(())
}
PARENT_HASHES_ABBREV => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -179,9 +174,9 @@ pub fn format_commit(
.map_err(|err| err.to_string())?
.to_string()[..7]
)
.map_err(|err| err.to_string())?;
.unwrap();
if i < commit.parent_count() - 1 {
write!(out, " ").map_err(|err| err.to_string())?;
write!(out, " ").unwrap();
}
}
Ok(())
Expand All @@ -190,7 +185,7 @@ pub fn format_commit(
match mode {
MODE_SPACE => {
if !branches.is_empty() {
write!(out, " ").map_err(|err| err.to_string())?
write!(out, " ").unwrap()
}
}
MODE_PLUS => {
Expand All @@ -212,7 +207,7 @@ pub fn format_commit(
match mode {
MODE_SPACE => {
if !summary.is_empty() {
write!(out, " ").map_err(|err| err.to_string())?
write!(out, " ").unwrap()
}
}
MODE_PLUS => {
Expand All @@ -231,23 +226,23 @@ pub fn format_commit(
}
AUTHOR => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
write!(out, "{}", &commit.author().name().unwrap_or(""))
}
AUTHOR_EMAIL => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
write!(out, "{}", &commit.author().email().unwrap_or(""))
}
AUTHOR_DATE => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -259,31 +254,31 @@ pub fn format_commit(
}
AUTHOR_DATE_SHORT => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
write!(out, "{}", format_date(commit.author().when(), "%F"))
}
COMMITTER => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
write!(out, "{}", &commit.committer().name().unwrap_or(""))
}
COMMITTER_EMAIL => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
write!(out, "{}", &commit.committer().email().unwrap_or(""))
}
COMMITTER_DATE => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -295,7 +290,7 @@ pub fn format_commit(
}
COMMITTER_DATE_SHORT => {
match mode {
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
MODE_SPACE => write!(out, " ").unwrap(),
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
_ => {}
}
Expand All @@ -312,7 +307,7 @@ pub fn format_commit(
match mode {
MODE_SPACE => {
if num_parts > 2 {
write!(out, " ").map_err(|err| err.to_string())?
write!(out, " ").unwrap()
}
}
MODE_PLUS => {
Expand All @@ -329,7 +324,7 @@ pub fn format_commit(
}
for (cnt, line) in message.iter().enumerate() {
if cnt > 1 && (cnt < num_parts - 1 || !line.is_empty()) {
write!(out, "{}", line).map_err(|err| err.to_string())?;
write!(out, "{}", line).unwrap();
add_line(&mut lines, &mut out, &wrapping);
}
}
Expand All @@ -347,7 +342,7 @@ pub fn format_commit(
match mode {
MODE_SPACE => {
if !message.is_empty() {
write!(out, " ").map_err(|err| err.to_string())?
write!(out, " ").unwrap()
}
}
MODE_PLUS => {
Expand All @@ -364,19 +359,19 @@ pub fn format_commit(
}
for (cnt, line) in message.iter().enumerate() {
if cnt < num_parts - 1 || !line.is_empty() {
write!(out, "{}", line).map_err(|err| err.to_string())?;
write!(out, "{}", line).unwrap();
add_line(&mut lines, &mut out, &wrapping);
}
}
Ok(())
}
x => return Err(format!("No commit field at index {}", x)),
}
.map_err(|err| err.to_string())?;
.unwrap();
}
curr = start + len;
}
write!(out, "{}", &format[curr..(format.len())]).map_err(|err| err.to_string())?;
write!(out, "{}", &format[curr..(format.len())]).unwrap();
if !out.is_empty() {
add_line(&mut lines, &mut out, &wrapping);
}
Expand All @@ -390,7 +385,7 @@ pub fn format_oneline(
branches: String,
wrapping: &Option<Options<HyphenSplitter>>,
hash_color: Option<u8>,
) -> Result<Vec<String>, String> {
) -> Vec<String> {
let mut out = String::new();
if let Some(color) = hash_color {
write!(
Expand All @@ -401,18 +396,17 @@ pub fn format_oneline(
} else {
write!(out, "{}", &commit.id().to_string()[..7])
}
.map_err(|err| err.to_string())?;
.unwrap();

write!(out, "{} {}", branches, commit.summary().unwrap_or(""))
.map_err(|err| err.to_string())?;
write!(out, "{} {}", branches, commit.summary().unwrap_or("")).unwrap();

if let Some(wrap) = wrapping {
Ok(textwrap::fill(&out, wrap)
textwrap::fill(&out, wrap)
.lines()
.map(|str| str.to_string())
.collect())
.collect()
} else {
Ok(vec![out])
vec![out]
}
}

Expand All @@ -425,7 +419,9 @@ pub fn format(
format: &CommitFormat,
) -> Result<Vec<String>, String> {
match format {
CommitFormat::OneLine => return format_oneline(&commit, branches, &wrapping, hash_color),
CommitFormat::OneLine => {
return Ok(format_oneline(&commit, branches, &wrapping, hash_color))
}
CommitFormat::Format(format) => {
return format_commit(format, &commit, branches, &wrapping, hash_color)
}
Expand Down
Loading

0 comments on commit cbacf97

Please sign in to comment.