Skip to content

Commit

Permalink
Add user gen to uniffi, comments, and make username Send
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Dec 8, 2023
1 parent 15fdb1b commit 9d8ddde
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
17 changes: 16 additions & 1 deletion crates/bitwarden-uniffi/src/tool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::sync::Arc;

use bitwarden::{
tool::{ExportFormat, PassphraseGeneratorRequest, PasswordGeneratorRequest},
tool::{
ExportFormat, PassphraseGeneratorRequest, PasswordGeneratorRequest,
UsernameGeneratorRequest,
},
vault::{Cipher, Collection, Folder},
};

Expand Down Expand Up @@ -35,6 +38,18 @@ impl ClientGenerators {
.passphrase(settings)
.await?)
}

/// **API Draft:** Generate Username
pub async fn username(&self, settings: UsernameGeneratorRequest) -> Result<String> {
Ok(self
.0
.0
.read()
.await
.generator()
.username(settings)
.await?)
}
}

#[derive(uniffi::Object)]
Expand Down
20 changes: 20 additions & 0 deletions crates/bitwarden/src/tool/generators/client_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ impl<'a> ClientGenerator<'a> {
passphrase(input)
}

/// Generates a random username.
/// There are different username generation strategies, which can be customized using the `input` parameter.
///
/// Note that most generation strategies will be executed on the client side, but `Forwarded` will use third-party
/// services, which may require a specific setup or API key.
///
/// ```
/// use bitwarden::{Client, tool::{UsernameGeneratorRequest, UsernameGeneratorType}, error::Result};
/// async fn test() -> Result<()> {
/// let input = UsernameGeneratorRequest {
/// r#type: UsernameGeneratorType::Word {
/// capitalize: Some(true),
/// include_number: Some(true),
/// }
/// };
/// let username = Client::new(None).generator().username(input).await.unwrap();
/// println!("{}", username);
/// Ok(())
/// }
/// ```
pub async fn username(&self, input: UsernameGeneratorRequest) -> Result<String> {
username(input).await
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bitwarden/src/tool/generators/username.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub struct UsernameGeneratorRequest {
}

pub(super) async fn username(input: UsernameGeneratorRequest) -> Result<String> {
use rand::thread_rng;
use UsernameGeneratorType::*;
let mut rng = rand::thread_rng();

match input.r#type {
Word {
Expand All @@ -98,10 +98,10 @@ pub(super) async fn username(input: UsernameGeneratorRequest) -> Result<String>
} => {
let capitalize = capitalize.unwrap_or(true);
let include_number = include_number.unwrap_or(true);
Ok(username_word(&mut rng, capitalize, include_number))
Ok(username_word(&mut thread_rng(), capitalize, include_number))
}
Subaddress { r#type, email } => Ok(username_subaddress(&mut rng, r#type, email)),
Catchall { r#type, domain } => Ok(username_catchall(&mut rng, r#type, domain)),
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 } => {
use crate::tool::generators::username_forwarders::*;
use ForwarderServiceType::*;
Expand Down

0 comments on commit 9d8ddde

Please sign in to comment.