Skip to content

Commit

Permalink
Remove GeneratorError and use explicit errors instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Jan 18, 2024
1 parent 6254365 commit f1d2157
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license-file = "LICENSE"
repository = "https://github.com/bitwarden/sdk"
homepage = "https://bitwarden.com"
description = """
Bitwarden Cryptographic primitives
Internal crate for the bitwarden crate. Do not use.
"""
keywords = ["bitwarden"]
edition = "2021"
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-crypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bitwarden Crypto

This is an internal crate for the Bitwarden SDK do not depend on this directly and use the
[`bitwarden`](https://crates.io/crates/bitwarden) crate instead.

This crate does not follow semantic versioning and the public interface may change at any time.
10 changes: 9 additions & 1 deletion crates/bitwarden-generators/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[package]
name = "bitwarden-generators"
version = "0.1.0"
authors = ["Bitwarden Inc"]
license-file = "LICENSE"
repository = "https://github.com/bitwarden/sdk"
homepage = "https://bitwarden.com"
description = """
Internal crate for the bitwarden crate. Do not use.
"""
keywords = ["bitwarden"]
edition = "2021"

rust-version = "1.57"

[features]
mobile = ["uniffi"] # Mobile-specific features
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-generators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bitwarden Generators

This is an internal crate for the Bitwarden SDK do not depend on this directly and use the
[`bitwarden`](https://crates.io/crates/bitwarden) crate instead.

This crate does not follow semantic versioning and the public interface may change at any time.
13 changes: 0 additions & 13 deletions crates/bitwarden-generators/src/error.rs

This file was deleted.

10 changes: 4 additions & 6 deletions crates/bitwarden-generators/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod passphrase;
pub use passphrase::{passphrase, PassphraseGeneratorRequest};
mod error;
mod util;
pub use error::GeneratorError;
pub use passphrase::{passphrase, PassphraseError, PassphraseGeneratorRequest};
mod password;
pub use password::{password, PasswordGeneratorRequest};
mod util;
pub use password::{password, PasswordError, PasswordGeneratorRequest};
mod username;
pub use username::{username, ForwarderServiceType, UsernameGeneratorRequest};
pub use username::{username, ForwarderServiceType, UsernameError, UsernameGeneratorRequest};
mod username_forwarders;

#[cfg(feature = "mobile")]
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden-generators/src/passphrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{error::GeneratorError, util::capitalize_first_letter};
use crate::util::capitalize_first_letter;

#[derive(Debug, Error)]

Check warning on line 9 in crates/bitwarden-generators/src/passphrase.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/passphrase.rs#L9

Added line #L9 was not covered by tests
pub enum PassphraseError {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl PassphraseGeneratorRequest {
}

/// Implementation of the random passphrase generator.
pub fn passphrase(request: PassphraseGeneratorRequest) -> Result<String, GeneratorError> {
pub fn passphrase(request: PassphraseGeneratorRequest) -> Result<String, PassphraseError> {

Check warning on line 83 in crates/bitwarden-generators/src/passphrase.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/passphrase.rs#L83

Added line #L83 was not covered by tests
let options = request.validate_options()?;
Ok(passphrase_with_rng(rand::thread_rng(), options))
}
Expand Down
4 changes: 1 addition & 3 deletions crates/bitwarden-generators/src/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::GeneratorError;

#[derive(Debug, Error)]

Check warning on line 8 in crates/bitwarden-generators/src/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/password.rs#L8

Added line #L8 was not covered by tests
pub enum PasswordError {
#[error("No character set enabled")]
Expand Down Expand Up @@ -218,7 +216,7 @@ impl PasswordGeneratorRequest {
}

/// Implementation of the random password generator.
pub fn password(input: PasswordGeneratorRequest) -> Result<String, GeneratorError> {
pub fn password(input: PasswordGeneratorRequest) -> Result<String, PasswordError> {

Check warning on line 219 in crates/bitwarden-generators/src/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/password.rs#L219

Added line #L219 was not covered by tests
let options = input.validate_options()?;
Ok(password_with_rng(rand::thread_rng(), options))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden-generators/src/username.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{util::capitalize_first_letter, GeneratorError};
use crate::util::capitalize_first_letter;

#[derive(Debug, Error)]
pub enum UsernameError {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl ForwarderServiceType {
pub async fn username(

Check warning on line 134 in crates/bitwarden-generators/src/username.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/username.rs#L134

Added line #L134 was not covered by tests
input: UsernameGeneratorRequest,
http: &reqwest::Client,
) -> Result<String, GeneratorError> {
) -> Result<String, UsernameError> {

Check warning on line 137 in crates/bitwarden-generators/src/username.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-generators/src/username.rs#L137

Added line #L137 was not covered by tests
use rand::thread_rng;
use UsernameGeneratorRequest::*;
match input {
Expand All @@ -144,7 +144,7 @@ pub async fn username(
} => Ok(username_word(&mut thread_rng(), capitalize, include_number)),
Subaddress { r#type, email } => Ok(username_subaddress(&mut thread_rng(), r#type, email)),
Catchall { r#type, domain } => Ok(username_catchall(&mut thread_rng(), r#type, domain)),
Forwarded { service, website } => Ok(service.generate(http, website).await?),
Forwarded { service, website } => service.generate(http, website).await,
}
}

Expand Down
8 changes: 6 additions & 2 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{borrow::Cow, fmt::Debug};

use bitwarden_api_api::apis::Error as ApiError;
use bitwarden_api_identity::apis::Error as IdentityError;
use bitwarden_generators::GeneratorError;
use bitwarden_generators::{PassphraseError, PasswordError, UsernameError};
use reqwest::StatusCode;
use thiserror::Error;

Expand Down Expand Up @@ -51,7 +51,11 @@ pub enum Error {
InvalidStateFile,

#[error(transparent)]
GeneratorError(#[from] GeneratorError),
UsernameError(#[from] UsernameError),
#[error(transparent)]
PassphraseError(#[from] PassphraseError),
#[error(transparent)]
PasswordError(#[from] PasswordError),

#[error("Internal error: {0}")]
Internal(Cow<'static, str>),
Expand Down

0 comments on commit f1d2157

Please sign in to comment.