From b2bbb614963dc2517e17587bd7db00feea8f26f9 Mon Sep 17 00:00:00 2001
From: Robin Krahl <robin.krahl@ireas.org>
Date: Mon, 24 Dec 2018 02:06:27 +0100
Subject: Extract print_status from print_storage_status function

This patch extracts the print_status function that prints the status
fields common to all supported Nitrokey devices from the
print_storage_status function.
---
 nitrocli/src/commands.rs | 44 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 10 deletions(-)

diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs
index 1fbeb79..c4d4598 100644
--- a/nitrocli/src/commands.rs
+++ b/nitrocli/src/commands.rs
@@ -171,25 +171,51 @@ where
   .map_err(|(_data, err)| err)
 }
 
+/// Pretty print the status that is common to all Nitrokey devices.
+fn print_status(
+  model: &'static str,
+  smartcard_id: &str,
+  firmware_major: i32,
+  firmware_minor: i32,
+  user_retries: u8,
+  admin_retries: u8,
+) {
+  println!(
+    r#"Status:
+  model:             {model}
+  smart card ID:     {id}
+  firmware version:  {fwv0}.{fwv1}
+  user retry count:  {urc}
+  admin retry count: {arc}"#,
+    model = model,
+    id = smartcard_id,
+    fwv0 = firmware_major,
+    fwv1 = firmware_minor,
+    urc = user_retries,
+    arc = admin_retries,
+  );
+}
+
 /// Pretty print the response of a status command for the Nitrokey Storage.
 fn print_storage_status(status: &nitrokey::StorageStatus) {
-  // We omit displaying information about the smartcard here as this
-  // program really is only about the SD card portion of the device.
+  print_status(
+    "Storage",
+    &format!("{:#x}", status.serial_number_smart_card),
+    status.firmware_version_major as i32,
+    status.firmware_version_minor as i32,
+    status.user_retry_count,
+    status.admin_retry_count,
+  );
   println!(
-    r#"Status:
+    r#"
   SD card ID:        {id:#x}
-  firmware version:  {fwv0}.{fwv1}
   firmware:          {fw}
   storage keys:      {sk}
-  user retry count:  {urc}
-  admin retry count: {arc}
   volumes:
     unencrypted:     {vu}
     encrypted:       {ve}
     hidden:          {vh}"#,
     id = status.serial_number_sd_card,
-    fwv0 = status.firmware_version_major,
-    fwv1 = status.firmware_version_minor,
     fw = if status.firmware_locked {
       "locked"
     } else {
@@ -200,8 +226,6 @@ fn print_storage_status(status: &nitrokey::StorageStatus) {
     } else {
       "not created"
     },
-    urc = status.user_retry_count,
-    arc = status.admin_retry_count,
     vu = get_volume_status(&status.unencrypted_volume),
     ve = get_volume_status(&status.encrypted_volume),
     vh = get_volume_status(&status.hidden_volume),
-- 
cgit v1.2.3