Skip to content

Commit 6b4ba36

Browse files
yassun7010shssoichiro
authored andcommitted
feat: add Display to Feedback.
1 parent 20a8548 commit 6b4ba36

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/feedback.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Contains structs and methods related to generating feedback strings
22
//! for providing help for the user to generate stronger passwords.
33
4+
use itertools::Itertools;
5+
46
use crate::matching::patterns::*;
57
use crate::matching::Match;
68
use crate::{frequency_lists::DictionaryType, scoring::Score};
@@ -153,6 +155,17 @@ impl Feedback {
153155
}
154156
}
155157

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+
156169
pub(crate) fn get_feedback(score: Score, sequence: &[Match]) -> Option<Feedback> {
157170
if sequence.is_empty() {
158171
// default feedback
@@ -321,4 +334,16 @@ mod tests {
321334
Some(Warning::ThisIsSimilarToACommonlyUsedPassword)
322335
);
323336
}
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+
}
324349
}

0 commit comments

Comments
 (0)