Skip to content

Commit

Permalink
style(memorable-pass): clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DaRacci committed Sep 25, 2023
1 parent 80a06d9 commit a0a7cac
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 122 deletions.
6 changes: 3 additions & 3 deletions crates/memorable-pass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#![feature(async_fn_in_trait)]

use crate::config::asset::WORDS;
use rand::Rng;
use tracing::{instrument, trace};
use crate::processor::processor::Processor;
use crate::rules::rule::Rule;
use crate::rules::rules::Rules;
use crate::rules::Rules;
use rand::Rng;
use tracing::{instrument, trace};

pub mod config;
pub mod processor;
Expand Down
1 change: 1 addition & 0 deletions crates/memorable-pass/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#[allow(clippy::module_inception)]
pub mod processor;
pub mod word;
8 changes: 0 additions & 8 deletions crates/memorable-pass/src/processor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ impl<'a> Processor<'a> {
Action::Addition(_, position, addition, condition) => {
match condition.should_use(index, position, &start_end) {
false => debug!("Skipping rule as condition failed"),
// ActionCondition::HasNoInput if start_end.0 != 0 || position.is_end() => {
// debug!("Skipping rule as it has no input and is not at the start of the word");
// continue
// },
// ActionCondition::HasInput if start_end.0 == 0 && position.is_start() => {
// debug!("Skipping rule as it has input and is at the start of the word");
// continue
// },
_ => match position {
Position::Start => {
mut_word.insert_str(start_end.0, addition);
Expand Down
26 changes: 19 additions & 7 deletions crates/memorable-pass/src/rules/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub enum ActionCondition {
}

#[derive(Derivative)]
#[derivative(Debug, Ord = "feature_allow_slow_enum", Eq, PartialEq)]
#[derivative(Debug, Eq)]
pub enum Action {
Addition(Priority, Position, String, ActionCondition),
Transformation(
Priority,
ActionCondition,
#[derivative(Debug = "ignore", Ord = "ignore", PartialEq = "ignore")] TransformationFn,
#[derivative(Debug = "ignore")] TransformationFn,
),
}

Expand All @@ -56,16 +56,28 @@ impl ActionCondition {
}
}

impl PartialEq<Self> for Action {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}

impl PartialOrd for Action {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Action {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::Addition(pri1, pos1, ..), Self::Addition(pri2, pos2, ..)) => match pos1 == pos2 {
true => pri1.partial_cmp(pri2),
false => pos1.partial_cmp(pos2),
true => pri1.cmp(pri2),
false => pos1.cmp(pos2),
},
(Self::Transformation(pri1, ..), Self::Transformation(pri2, ..)) => pri1.partial_cmp(pri2),
(Self::Addition(..), Self::Transformation(..)) => Some(Ordering::Less),
(Self::Transformation(..), Self::Addition(..)) => Some(Ordering::Greater),
(Self::Transformation(pri1, ..), Self::Transformation(pri2, ..)) => pri1.cmp(pri2),
(Self::Addition(..), Self::Transformation(..)) => Ordering::Less,
(Self::Transformation(..), Self::Addition(..)) => Ordering::Greater,
}
}
}
7 changes: 0 additions & 7 deletions crates/memorable-pass/src/rules/addition/separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ mod tests {
use regex::Regex;
use std::assert_matches::assert_matches;

fn get_string(action: &Action) -> String {
match action {
Action::Addition(_, _, string, ..) => string.clone(),
_ => String::new(),
}
}

#[test_log::test(test)]
fn separator_none() {
let mut processor = Processor::new(vec!["hello", "world"]);
Expand Down
57 changes: 56 additions & 1 deletion crates/memorable-pass/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,60 @@ pub mod addition;
pub mod position;
pub mod priority;
pub mod rule;
pub mod rules;
pub mod transformation;

use crate::rules::addition::digits::DigitAddition;
use crate::rules::addition::separator::SeparatorAddition;
use crate::rules::transformation::case::CaseTransformation;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// The rules which are used to generate passwords.
#[derive(Debug, Parser, Serialize, Deserialize)]
#[serde(default)]
pub struct Rules {
/// How many words are used.
///
/// This is the number of words which each password will contain.
/// These words are the base of the password with the rules being applied to them.
///
/// The maximum number of words is 10 with the minimum being 1.
#[arg(short = 'w', long = "words", default_value_t = 2, value_parser = clap::value_parser!(u8).range(1..10))]
pub word_count: u8,

/// The minimum length of each word.
#[arg(short = 'm', long = "min-length", default_value_t = 5, value_parser = clap::value_parser!(u8).range(3..=9))]
pub word_length_min: u8,

/// The maximum length of each word.
#[arg(short = 'M', long = "max-length", default_value_t = 7, value_parser = clap::value_parser!(u8).range(3..=9))]
pub word_length_max: u8,

#[command(flatten)]
pub addition_digits: DigitAddition,

#[command(flatten)]
pub addition_separator: SeparatorAddition,

#[arg(long, default_value_t = CaseTransformation::default(), value_enum)]
pub transformation_case: CaseTransformation,

/// The number of passwords to generate.
#[arg(short, long, default_value_t = 3)]
pub amount: usize,
}

impl Default for Rules {
fn default() -> Self {
Rules {
word_count: 2,
word_length_min: 5,
word_length_max: 7,
addition_digits: DigitAddition::default(),
addition_separator: SeparatorAddition::default(),
transformation_case: CaseTransformation::default(),
amount: 3,
}
}
}
2 changes: 1 addition & 1 deletion crates/memorable-pass/src/rules/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Ord for Priority {

impl PartialOrd for Priority {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.u8().partial_cmp(&other.u8())
Some(self.cmp(other))
}
}

Expand Down
86 changes: 0 additions & 86 deletions crates/memorable-pass/src/rules/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,3 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::rules::addition::digits::DigitAddition;
use crate::rules::addition::separator::SeparatorAddition;
use crate::rules::transformation::case::CaseTransformation;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// The rules which are used to generate passwords.
#[derive(Debug, Parser, Serialize, Deserialize)]
#[serde(default)]
pub struct Rules {
/// How many words are used.
///
/// This is the number of words which each password will contain.
/// These words are the base of the password with the rules being applied to them.
///
/// The maximum number of words is 10 with the minimum being 1.
#[arg(short = 'w', long = "words", default_value_t = 2, value_parser = clap::value_parser!(u8).range(1..10))]
pub word_count: u8,

/// The minimum length of each word.
#[arg(short = 'm', long = "min-length", default_value_t = 5, value_parser = clap::value_parser!(u8).range(3..=9))]
pub word_length_min: u8,

/// The maximum length of each word.
#[arg(short = 'M', long = "max-length", default_value_t = 7, value_parser = clap::value_parser!(u8).range(3..=9))]
pub word_length_max: u8,

#[command(flatten)]
pub addition_digits: DigitAddition,

#[command(flatten)]
pub addition_separator: SeparatorAddition,

#[arg(long, default_value_t = CaseTransformation::default(), value_enum)]
pub transformation_case: CaseTransformation,

/// The number of digits to add before the password.
// #[arg(short, long, default_value_t, value_parser = clap::value_parser!(u8).range(0..8))]
// pub digits_before: u8,
//
// /// The number of digits to add after the password.
// #[arg(short = 'D', long, default_value_t, value_parser = clap::value_parser!(u8).range(0..8))]
// pub digits_after: u8,
//
// /// The transformation to apply to each word.
// #[arg(short, long, default_value_t = Rules::default().transform, value_enum)]
// pub transform: Transformation,
//
// /// The separator mode or singular character to use.
// #[serde(alias = "separator_char")]
// #[arg(short = 's', long, default_value_t = Rules::default().separator_mode, value_enum)]
// pub separator_mode: SeparatorMode,
//
// /// The list of characters which can be used for the separator.
// #[arg(short = 'S', long, default_value_t)]
// pub separator_alphabet: Vec<char>,

// /// If all separator characters in the password should be the same.
// #[serde(alias = "match_random_char")]
// #[arg(
// short = 'r',
// long = "match-random-char",
// long = "separator_matching",
// default_value_t
// )]
// pub separator_matching: bool,

/// The number of passwords to generate.
#[arg(short, long, default_value_t = 3)]
pub amount: usize,
}

impl Default for Rules {
fn default() -> Self {
Rules {
word_count: 2,
word_length_min: 5,
word_length_max: 7,
addition_digits: DigitAddition::default(),
addition_separator: SeparatorAddition::default(),
transformation_case: CaseTransformation::default(),
amount: 3,
}
}
}
4 changes: 2 additions & 2 deletions crates/memorable-pass/src/ui/cli/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::rules::rules::Rules;
use crate::rules::Rules;
use clap::Subcommand;

#[derive(Debug, Subcommand)]
pub enum Action {
Generate {
#[command(flatten)]
rules: Rules,
}
},
}
11 changes: 4 additions & 7 deletions crates/memorable-pass/src/ui/cli/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use crate::config;
use crate::rules::rules::Rules;
use crate::rules::Rules;
use crate::ui::cli::action::Action;
use lib::cli::Flags as CommonFlags;
use lib::ui::cli::cli::{AsyncCliUI, CliResult, CliUI};
Expand All @@ -41,10 +41,10 @@ impl CliUI for MemorablePassCli {
let _preload = &config::asset::WORDS;
});

return Ok(Self {
Ok(Self {
_guard: None,
rules: None,
});
})
}
}

Expand All @@ -60,10 +60,7 @@ impl AsyncCliUI for MemorablePassCli {
let passwords = crate::generate(&rules).await;
self.rules.replace(rules);

info!(
"Generated passwords:\n{passwords}",
passwords = passwords.join("\n")
);
info!("Generated passwords:\n{passwords}", passwords = passwords.join("\n"));
}
}

Expand Down

0 comments on commit a0a7cac

Please sign in to comment.