diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index aaf7570..3509dee 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,35 +5,33 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly with: toolchain: stable - run: cargo build --verbose fmt: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly with: toolchain: stable - run: cargo fmt --verbose --all --check test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly with: toolchain: stable - run: cargo test --verbose clippy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly with: toolchain: stable components: clippy - - uses: giraffate/clippy-action@v1 - with: - clippy_flags: -- -D clippy::pedantic -D clippy::all + - run: cargo clippy -- -D clippy::pedantic -D clippy::all diff --git a/src/passphrase.rs b/src/passphrase.rs index 26b8344..40c63ff 100644 --- a/src/passphrase.rs +++ b/src/passphrase.rs @@ -12,16 +12,16 @@ pub fn new( words.len(), num_words ); - return "".to_string(); + return String::new(); } (0..num_words).for_each(|i| { let j = rng.gen_range(i..words.len()); - words.swap(i, j) + words.swap(i, j); }); (0..num_words) - .map(|i| words[i].to_owned()) + .map(|i| words[i].clone()) .collect::>() .join(separator) } diff --git a/src/words.rs b/src/words.rs index 8a5c8a5..10789fb 100644 --- a/src/words.rs +++ b/src/words.rs @@ -13,25 +13,39 @@ lazy_static! { pub(crate) fn list(path: Option>) -> Result> { match path { - Some(path) => words_from_file(path), - None => words_from_fixture(), + Some(path) => WordsFromFile { path }.words(), + None => WordsFromFixture {}.words(), } } -fn words_from_fixture() -> Result> { - let bytes = include_bytes!("fixtures/words"); - Ok(String::from_utf8_lossy(bytes) - .split('\n') - .filter(|w| RE.is_match(w)) - .map(|l| l.to_owned()) - .collect()) +trait Words { + fn words(&self) -> Result>; } -fn words_from_file(path: impl AsRef) -> Result> { - let file = File::open(path)?; - Ok(BufReader::new(file) - .lines() - .map_while(|l| l.ok()) - .filter(|w| RE.is_match(w)) - .collect()) +struct WordsFromFixture; + +impl Words for WordsFromFixture { + fn words(&self) -> Result> { + let bytes = include_bytes!("fixtures/words"); + Ok(String::from_utf8_lossy(bytes) + .split('\n') + .filter(|w| RE.is_match(w)) + .map(std::borrow::ToOwned::to_owned) + .collect()) + } +} + +struct WordsFromFile> { + path: P, +} + +impl> Words for WordsFromFile

{ + fn words(&self) -> Result> { + let file = File::open(&self.path)?; + Ok(BufReader::new(file) + .lines() + .map_while(std::result::Result::ok) + .filter(|w| RE.is_match(w)) + .collect()) + } }