From 6b8cb4468ad2372f26fcfff20e154e4da24874db Mon Sep 17 00:00:00 2001
From: Teemu Matilainen <teemu.matilainen@iki.fi>
Date: Sat, 7 Dec 2024 18:22:31 +0200
Subject: [PATCH] Replace redact with derive_more

Next try. Get back the nicer debug output.
---
 Cargo.lock    | 38 ++++++++++++++++++++++++++++----------
 Cargo.toml    |  2 +-
 src/config.rs | 13 +++++++------
 src/mqtt.rs   |  1 -
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 28e0b28..c8b2a55 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -472,6 +472,27 @@ dependencies = [
  "serde",
 ]
 
+[[package]]
+name = "derive_more"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05"
+dependencies = [
+ "derive_more-impl",
+]
+
+[[package]]
+name = "derive_more-impl"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "unicode-xid",
+]
+
 [[package]]
 name = "dunce"
 version = "1.0.5"
@@ -1128,15 +1149,6 @@ dependencies = [
  "crossbeam-utils",
 ]
 
-[[package]]
-name = "redact"
-version = "0.1.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0020ec469b096d56edb1ed0f0f141d957863302170f8d9c4bfda1a12969e5969"
-dependencies = [
- "serde",
-]
-
 [[package]]
 name = "redox_syscall"
 version = "0.5.7"
@@ -1297,11 +1309,11 @@ dependencies = [
  "btleplug",
  "clap",
  "dbus",
+ "derive_more",
  "env_logger",
  "futures",
  "log",
  "rand",
- "redact",
  "rumqttc",
  "ruuvi-sensor-protocol",
  "serde",
@@ -1765,6 +1777,12 @@ version = "1.0.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
 
+[[package]]
+name = "unicode-xid"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
+
 [[package]]
 name = "unsafe-libyaml"
 version = "0.2.11"
diff --git a/Cargo.toml b/Cargo.toml
index c5369c3..24ccbff 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,11 +20,11 @@ strip = true
 anyhow = "1.0"
 btleplug = "0.11.6"
 clap = { version = "4.5.21", features = ["derive", "env", "wrap_help"] }
+derive_more = { version = "1.0.0", features = ["debug"] }
 env_logger = "0.11.3"
 futures = "0.3.19"
 log = "0.4.17"
 rand = "0.8.4"
-redact = { version = "0.1.10", features = ["serde"] }
 rumqttc = "0.24.0"
 ruuvi-sensor-protocol = "0.6.1"
 serde = { version = "1.0", features = ["derive"] }
diff --git a/src/config.rs b/src/config.rs
index 16f2aa1..5decd5b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,9 +1,9 @@
-use std::{collections::HashMap, fmt, fs, path::PathBuf, time::Duration};
+use std::{collections::HashMap, fs, path::PathBuf, time::Duration};
 
 use anyhow::{Context, Result};
 use clap::{CommandFactory, Parser};
+use derive_more::Debug;
 use rand::Rng;
-use redact::Secret;
 use serde::Deserialize;
 use serde_with::{formats::Flexible, serde_as, DisplayFromStr, DurationSeconds};
 use sysinfo::System;
@@ -25,7 +25,8 @@ pub struct Mqtt {
     #[serde(default = "default_mqtt_port")]
     pub port: u16,
     pub user: Option<String>,
-    pub password: Option<Secret<String>>,
+    #[debug("{}", fmt_secret(password))]
+    pub password: Option<String>,
     #[serde(default = "default_mqtt_client_id")]
     pub client_id: String,
     #[serde_as(as = "DurationSeconds<u32, Flexible>")]
@@ -92,10 +93,10 @@ fn default_mqtt_base_topic() -> String {
     String::from("ruuvi2mqtt")
 }
 
-fn fmt_secret(value: &Option<String>, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+fn fmt_secret(value: &Option<String>) -> &str {
     match value {
-        None => formatter.write_str("None"),
-        Some(_) => formatter.write_str("Some(<REDACTED>)"),
+        None => "None",
+        Some(_) => "Some(<REDACTED>)",
     }
 }
 
diff --git a/src/mqtt.rs b/src/mqtt.rs
index 017c0fd..45ed9f7 100644
--- a/src/mqtt.rs
+++ b/src/mqtt.rs
@@ -49,7 +49,6 @@ impl Mqtt {
             let password = config
                 .password
                 .as_ref()
-                .map(|secret| secret.expose_secret())
                 .context("MQTT password not specified")?;
             options.set_credentials(user, password);
         }