Skip to content

Commit

Permalink
feat: add Display to Feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 authored and shssoichiro committed Jun 18, 2024
1 parent 20a8548 commit 6b4ba36
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/feedback.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Feedback> {
if sequence.is_empty() {
// default feedback
Expand Down Expand Up @@ -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."
);
}
}

0 comments on commit 6b4ba36

Please sign in to comment.