From 6b4ba3661a364a85b3a0c25df9bc93964de3447c Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Mon, 17 Jun 2024 21:54:52 +0900 Subject: [PATCH] feat: add Display to Feedback. --- src/feedback.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/feedback.rs b/src/feedback.rs index ebd4eca..56d00ae 100644 --- a/src/feedback.rs +++ b/src/feedback.rs @@ -1,6 +1,8 @@ //! Contains structs and methods related to generating feedback strings //! for providing help for the user to generate stronger passwords. +use itertools::Itertools; + use crate::matching::patterns::*; use crate::matching::Match; use crate::{frequency_lists::DictionaryType, scoring::Score}; @@ -153,6 +155,17 @@ impl Feedback { } } +impl fmt::Display for Feedback { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(warning) = self.warning { + write!(f, "{} ", warning)?; + } + write!(f, "{}", self.suggestions.iter().join(" "))?; + + Ok(()) + } +} + pub(crate) fn get_feedback(score: Score, sequence: &[Match]) -> Option { if sequence.is_empty() { // default feedback @@ -321,4 +334,16 @@ mod tests { Some(Warning::ThisIsSimilarToACommonlyUsedPassword) ); } + + #[test] + fn test_feedback_display() { + let feedback = Feedback { + warning: Some(Warning::ThisIsATop10Password), + suggestions: vec![Suggestion::UseAFewWordsAvoidCommonPhrases], + }; + assert_eq!( + format!("{}", feedback), + "This is a top-10 common password. Use a few words, avoid common phrases." + ); + } }