Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Display to Feedback. #84

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
);
}
}
Loading