|
1 | 1 | //! Contains structs and methods related to generating feedback strings
|
2 | 2 | //! for providing help for the user to generate stronger passwords.
|
3 | 3 |
|
| 4 | +use itertools::Itertools; |
| 5 | + |
4 | 6 | use crate::matching::patterns::*;
|
5 | 7 | use crate::matching::Match;
|
6 | 8 | use crate::{frequency_lists::DictionaryType, scoring::Score};
|
@@ -153,6 +155,17 @@ impl Feedback {
|
153 | 155 | }
|
154 | 156 | }
|
155 | 157 |
|
| 158 | +impl fmt::Display for Feedback { |
| 159 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 160 | + if let Some(warning) = self.warning { |
| 161 | + write!(f, "{} ", warning)?; |
| 162 | + } |
| 163 | + write!(f, "{}", self.suggestions.iter().join(" "))?; |
| 164 | + |
| 165 | + Ok(()) |
| 166 | + } |
| 167 | +} |
| 168 | + |
156 | 169 | pub(crate) fn get_feedback(score: Score, sequence: &[Match]) -> Option<Feedback> {
|
157 | 170 | if sequence.is_empty() {
|
158 | 171 | // default feedback
|
@@ -321,4 +334,16 @@ mod tests {
|
321 | 334 | Some(Warning::ThisIsSimilarToACommonlyUsedPassword)
|
322 | 335 | );
|
323 | 336 | }
|
| 337 | + |
| 338 | + #[test] |
| 339 | + fn test_feedback_display() { |
| 340 | + let feedback = Feedback { |
| 341 | + warning: Some(Warning::ThisIsATop10Password), |
| 342 | + suggestions: vec![Suggestion::UseAFewWordsAvoidCommonPhrases], |
| 343 | + }; |
| 344 | + assert_eq!( |
| 345 | + format!("{}", feedback), |
| 346 | + "This is a top-10 common password. Use a few words, avoid common phrases." |
| 347 | + ); |
| 348 | + } |
324 | 349 | }
|
0 commit comments