bitwarden_generators/
generator_client.rs

1use bitwarden_core::Client;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{
6    passphrase::passphrase, password::password, username::username, PassphraseError,
7    PassphraseGeneratorRequest, PasswordError, PasswordGeneratorRequest, UsernameError,
8    UsernameGeneratorRequest,
9};
10
11#[allow(missing_docs)]
12#[cfg_attr(feature = "wasm", wasm_bindgen)]
13pub struct GeneratorClient {
14    client: Client,
15}
16
17#[cfg_attr(feature = "wasm", wasm_bindgen)]
18impl GeneratorClient {
19    fn new(client: Client) -> Self {
20        Self { client }
21    }
22
23    /// Generates a random password.
24    ///
25    /// The character sets and password length can be customized using the `input` parameter.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use bitwarden_core::Client;
31    /// use bitwarden_generators::{GeneratorClientsExt, PassphraseError, PasswordGeneratorRequest};
32    ///
33    /// async fn test() -> Result<(), PassphraseError> {
34    ///     let input = PasswordGeneratorRequest {
35    ///         lowercase: true,
36    ///         uppercase: true,
37    ///         numbers: true,
38    ///         length: 20,
39    ///         ..Default::default()
40    ///     };
41    ///     let password = Client::new(None).generator().password(input).unwrap();
42    ///     println!("{}", password);
43    ///     Ok(())
44    /// }
45    /// ```
46    pub fn password(&self, input: PasswordGeneratorRequest) -> Result<String, PasswordError> {
47        password(input)
48    }
49
50    /// Generates a random passphrase.
51    /// A passphrase is a combination of random words separated by a character.
52    /// An example of passphrase is `correct horse battery staple`.
53    ///
54    /// The number of words and their case, the word separator, and the inclusion of
55    /// a number in the passphrase can be customized using the `input` parameter.
56    ///
57    /// # Examples
58    ///
59    /// ```
60    /// use bitwarden_core::Client;
61    /// use bitwarden_generators::{GeneratorClientsExt, PassphraseError, PassphraseGeneratorRequest};
62    ///
63    /// async fn test() -> Result<(), PassphraseError> {
64    ///     let input = PassphraseGeneratorRequest {
65    ///         num_words: 4,
66    ///         ..Default::default()
67    ///     };
68    ///     let passphrase = Client::new(None).generator().passphrase(input).unwrap();
69    ///     println!("{}", passphrase);
70    ///     Ok(())
71    /// }
72    /// ```
73    pub fn passphrase(&self, input: PassphraseGeneratorRequest) -> Result<String, PassphraseError> {
74        passphrase(input)
75    }
76}
77
78impl GeneratorClient {
79    /// Generates a random username.
80    /// There are different username generation strategies, which can be customized using the
81    /// `input` parameter.
82    ///
83    /// Note that most generation strategies will be executed on the client side, but `Forwarded`
84    /// will use third-party services, which may require a specific setup or API key.
85    ///
86    /// ```
87    /// use bitwarden_core::Client;
88    /// use bitwarden_generators::{GeneratorClientsExt, UsernameError, UsernameGeneratorRequest};
89    ///
90    /// async fn test() -> Result<(), UsernameError> {
91    ///     let input = UsernameGeneratorRequest::Word {
92    ///         capitalize: true,
93    ///         include_number: true,
94    ///     };
95    ///     let username = Client::new(None).generator().username(input).await.unwrap();
96    ///     println!("{}", username);
97    ///     Ok(())
98    /// }
99    /// ```
100    pub async fn username(&self, input: UsernameGeneratorRequest) -> Result<String, UsernameError> {
101        username(input, self.client.internal.get_http_client()).await
102    }
103}
104
105#[allow(missing_docs)]
106pub trait GeneratorClientsExt {
107    fn generator(&self) -> GeneratorClient;
108}
109
110impl GeneratorClientsExt for Client {
111    fn generator(&self) -> GeneratorClient {
112        GeneratorClient::new(self.clone())
113    }
114}