Skip to content

Commit cbacf97

Browse files
committed
simplified code by unwrapping write! to String, rather than bubble up errors that never occur; increment version to 0.5.0
1 parent df3574b commit cbacf97

4 files changed

Lines changed: 57 additions & 61 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "git-graph"
3-
version = "0.4.3"
3+
version = "0.5.0"
44
authors = ["Martin Lange <martin_lange_@gmx.net>"]
55
description = "Command line tool to show clear git graphs arranged for your branching model"
66
repository = "https://github.com/mlange-42/git-graph.git"

src/print/format.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,20 @@ pub fn format_commit(
106106
let mut lines = vec![];
107107
let mut out = String::new();
108108
if replacements.is_empty() {
109-
write!(out, "{}", format).map_err(|err| err.to_string())?;
109+
write!(out, "{}", format).unwrap();
110110
add_line(&mut lines, &mut out, &wrapping);
111111
} else {
112112
let mut curr = 0;
113113
for (start, len, idx, mode) in replacements {
114114
if idx == NEW_LINE {
115-
write!(out, "{}", &format[curr..start]).map_err(|err| err.to_string())?;
115+
write!(out, "{}", &format[curr..start]).unwrap();
116116
add_line(&mut lines, &mut out, &wrapping);
117117
} else {
118-
write!(out, "{}", &format[curr..start]).map_err(|err| err.to_string())?;
118+
write!(out, "{}", &format[curr..start]).unwrap();
119119
match idx {
120120
HASH => {
121121
match mode {
122-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
122+
MODE_SPACE => write!(out, " ").unwrap(),
123123
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
124124
_ => {}
125125
}
@@ -131,7 +131,7 @@ pub fn format_commit(
131131
}
132132
HASH_ABBREV => {
133133
match mode {
134-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
134+
MODE_SPACE => write!(out, " ").unwrap(),
135135
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
136136
_ => {}
137137
}
@@ -147,26 +147,21 @@ pub fn format_commit(
147147
}
148148
PARENT_HASHES => {
149149
match mode {
150-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
150+
MODE_SPACE => write!(out, " ").unwrap(),
151151
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
152152
_ => {}
153153
}
154154
for i in 0..commit.parent_count() {
155-
write!(
156-
out,
157-
"{}",
158-
commit.parent_id(i).map_err(|err| err.to_string())?
159-
)
160-
.map_err(|err| err.to_string())?;
155+
write!(out, "{}", commit.parent_id(i).unwrap()).unwrap();
161156
if i < commit.parent_count() - 1 {
162-
write!(out, " ").map_err(|err| err.to_string())?;
157+
write!(out, " ").unwrap();
163158
}
164159
}
165160
Ok(())
166161
}
167162
PARENT_HASHES_ABBREV => {
168163
match mode {
169-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
164+
MODE_SPACE => write!(out, " ").unwrap(),
170165
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
171166
_ => {}
172167
}
@@ -179,9 +174,9 @@ pub fn format_commit(
179174
.map_err(|err| err.to_string())?
180175
.to_string()[..7]
181176
)
182-
.map_err(|err| err.to_string())?;
177+
.unwrap();
183178
if i < commit.parent_count() - 1 {
184-
write!(out, " ").map_err(|err| err.to_string())?;
179+
write!(out, " ").unwrap();
185180
}
186181
}
187182
Ok(())
@@ -190,7 +185,7 @@ pub fn format_commit(
190185
match mode {
191186
MODE_SPACE => {
192187
if !branches.is_empty() {
193-
write!(out, " ").map_err(|err| err.to_string())?
188+
write!(out, " ").unwrap()
194189
}
195190
}
196191
MODE_PLUS => {
@@ -212,7 +207,7 @@ pub fn format_commit(
212207
match mode {
213208
MODE_SPACE => {
214209
if !summary.is_empty() {
215-
write!(out, " ").map_err(|err| err.to_string())?
210+
write!(out, " ").unwrap()
216211
}
217212
}
218213
MODE_PLUS => {
@@ -231,23 +226,23 @@ pub fn format_commit(
231226
}
232227
AUTHOR => {
233228
match mode {
234-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
229+
MODE_SPACE => write!(out, " ").unwrap(),
235230
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
236231
_ => {}
237232
}
238233
write!(out, "{}", &commit.author().name().unwrap_or(""))
239234
}
240235
AUTHOR_EMAIL => {
241236
match mode {
242-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
237+
MODE_SPACE => write!(out, " ").unwrap(),
243238
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
244239
_ => {}
245240
}
246241
write!(out, "{}", &commit.author().email().unwrap_or(""))
247242
}
248243
AUTHOR_DATE => {
249244
match mode {
250-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
245+
MODE_SPACE => write!(out, " ").unwrap(),
251246
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
252247
_ => {}
253248
}
@@ -259,31 +254,31 @@ pub fn format_commit(
259254
}
260255
AUTHOR_DATE_SHORT => {
261256
match mode {
262-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
257+
MODE_SPACE => write!(out, " ").unwrap(),
263258
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
264259
_ => {}
265260
}
266261
write!(out, "{}", format_date(commit.author().when(), "%F"))
267262
}
268263
COMMITTER => {
269264
match mode {
270-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
265+
MODE_SPACE => write!(out, " ").unwrap(),
271266
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
272267
_ => {}
273268
}
274269
write!(out, "{}", &commit.committer().name().unwrap_or(""))
275270
}
276271
COMMITTER_EMAIL => {
277272
match mode {
278-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
273+
MODE_SPACE => write!(out, " ").unwrap(),
279274
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
280275
_ => {}
281276
}
282277
write!(out, "{}", &commit.committer().email().unwrap_or(""))
283278
}
284279
COMMITTER_DATE => {
285280
match mode {
286-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
281+
MODE_SPACE => write!(out, " ").unwrap(),
287282
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
288283
_ => {}
289284
}
@@ -295,7 +290,7 @@ pub fn format_commit(
295290
}
296291
COMMITTER_DATE_SHORT => {
297292
match mode {
298-
MODE_SPACE => write!(out, " ").map_err(|err| err.to_string())?,
293+
MODE_SPACE => write!(out, " ").unwrap(),
299294
MODE_PLUS => add_line(&mut lines, &mut out, &wrapping),
300295
_ => {}
301296
}
@@ -312,7 +307,7 @@ pub fn format_commit(
312307
match mode {
313308
MODE_SPACE => {
314309
if num_parts > 2 {
315-
write!(out, " ").map_err(|err| err.to_string())?
310+
write!(out, " ").unwrap()
316311
}
317312
}
318313
MODE_PLUS => {
@@ -329,7 +324,7 @@ pub fn format_commit(
329324
}
330325
for (cnt, line) in message.iter().enumerate() {
331326
if cnt > 1 && (cnt < num_parts - 1 || !line.is_empty()) {
332-
write!(out, "{}", line).map_err(|err| err.to_string())?;
327+
write!(out, "{}", line).unwrap();
333328
add_line(&mut lines, &mut out, &wrapping);
334329
}
335330
}
@@ -347,7 +342,7 @@ pub fn format_commit(
347342
match mode {
348343
MODE_SPACE => {
349344
if !message.is_empty() {
350-
write!(out, " ").map_err(|err| err.to_string())?
345+
write!(out, " ").unwrap()
351346
}
352347
}
353348
MODE_PLUS => {
@@ -364,19 +359,19 @@ pub fn format_commit(
364359
}
365360
for (cnt, line) in message.iter().enumerate() {
366361
if cnt < num_parts - 1 || !line.is_empty() {
367-
write!(out, "{}", line).map_err(|err| err.to_string())?;
362+
write!(out, "{}", line).unwrap();
368363
add_line(&mut lines, &mut out, &wrapping);
369364
}
370365
}
371366
Ok(())
372367
}
373368
x => return Err(format!("No commit field at index {}", x)),
374369
}
375-
.map_err(|err| err.to_string())?;
370+
.unwrap();
376371
}
377372
curr = start + len;
378373
}
379-
write!(out, "{}", &format[curr..(format.len())]).map_err(|err| err.to_string())?;
374+
write!(out, "{}", &format[curr..(format.len())]).unwrap();
380375
if !out.is_empty() {
381376
add_line(&mut lines, &mut out, &wrapping);
382377
}
@@ -390,7 +385,7 @@ pub fn format_oneline(
390385
branches: String,
391386
wrapping: &Option<Options<HyphenSplitter>>,
392387
hash_color: Option<u8>,
393-
) -> Result<Vec<String>, String> {
388+
) -> Vec<String> {
394389
let mut out = String::new();
395390
if let Some(color) = hash_color {
396391
write!(
@@ -401,18 +396,17 @@ pub fn format_oneline(
401396
} else {
402397
write!(out, "{}", &commit.id().to_string()[..7])
403398
}
404-
.map_err(|err| err.to_string())?;
399+
.unwrap();
405400

406-
write!(out, "{} {}", branches, commit.summary().unwrap_or(""))
407-
.map_err(|err| err.to_string())?;
401+
write!(out, "{} {}", branches, commit.summary().unwrap_or("")).unwrap();
408402

409403
if let Some(wrap) = wrapping {
410-
Ok(textwrap::fill(&out, wrap)
404+
textwrap::fill(&out, wrap)
411405
.lines()
412406
.map(|str| str.to_string())
413-
.collect())
407+
.collect()
414408
} else {
415-
Ok(vec![out])
409+
vec![out]
416410
}
417411
}
418412

@@ -425,7 +419,9 @@ pub fn format(
425419
format: &CommitFormat,
426420
) -> Result<Vec<String>, String> {
427421
match format {
428-
CommitFormat::OneLine => return format_oneline(&commit, branches, &wrapping, hash_color),
422+
CommitFormat::OneLine => {
423+
return Ok(format_oneline(&commit, branches, &wrapping, hash_color))
424+
}
429425
CommitFormat::Format(format) => {
430426
return format_commit(format, &commit, branches, &wrapping, hash_color)
431427
}

0 commit comments

Comments
 (0)