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." + ); + } }