Skip to content

Commit 3a2c49e

Browse files
improve documentation
1 parent caeb9a0 commit 3a2c49e

1 file changed

Lines changed: 76 additions & 12 deletions

File tree

src/lib.rs

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
//! println!("Your password is {}", password);
1414
//! ```
1515
//!
16-
//! Finally, in unit tests, you might want to pass a `Cursor`, which implements `BufRead`. In that
17-
//! case, you can use `read_password_from_bufread` and `prompt_password_from_bufread`:
18-
//! ```
19-
//! use std::io::Cursor;
16+
//! For testing, you can use `read_password_with_config` and `prompt_password_with_config` with a temporary file:
17+
//! ```no_run
18+
//! use tempfile::NamedTempFile;
19+
//! use std::io::Write;
2020
//!
21-
//! let mut mock_input = Cursor::new("my-password\n".as_bytes().to_owned());
22-
//! let password = rpassword::read_password_from_bufread(&mut mock_input).unwrap();
23-
//! println!("Your password is {}", password);
21+
//! let mut temp_file = NamedTempFile::new().unwrap();
22+
//! temp_file.write_all(b"my-password\n").unwrap();
23+
//! let path = temp_file.path().to_str().unwrap().to_string();
24+
//!
25+
//! let config = rpassword::ConfigBuilder::new()
26+
//! .input_path(path)
27+
//! .build();
2428
//!
25-
//! let mut mock_input = Cursor::new("my-password\n".as_bytes().to_owned());
26-
//! let mut mock_output = Cursor::new(Vec::new());
27-
//! let password = rpassword::prompt_password_from_bufread(&mut mock_input, &mut mock_output, "Your password: ").unwrap();
29+
//! let password = rpassword::read_password_with_config(config).unwrap();
2830
//! println!("Your password is {}", password);
2931
//! ```
3032
@@ -35,6 +37,35 @@ use std::fmt::Debug;
3537
use std::io::{BufRead, Write};
3638

3739
/// Controls visual feedback when the user types a password.
40+
///
41+
/// # Examples
42+
///
43+
/// ## Using `PasswordFeedback::Mask` to show asterisks (`*`) while typing:
44+
/// ```
45+
/// use rpassword::{ConfigBuilder, PasswordFeedback};
46+
///
47+
/// let config = ConfigBuilder::new()
48+
/// .password_feedback(PasswordFeedback::Mask('*'))
49+
/// .build();
50+
/// ```
51+
///
52+
/// ## Using `PasswordFeedback::PartialMask` to show the first 3 characters in plaintext, then asterisks (`*`):
53+
/// ```
54+
/// use rpassword::{ConfigBuilder, PasswordFeedback};
55+
///
56+
/// let config = ConfigBuilder::new()
57+
/// .password_feedback(PasswordFeedback::PartialMask('*', 3))
58+
/// .build();
59+
/// ```
60+
///
61+
/// ## Using `PasswordFeedback::Hide` (default behavior):
62+
/// ```
63+
/// use rpassword::{ConfigBuilder, PasswordFeedback};
64+
///
65+
/// let config = ConfigBuilder::new()
66+
/// .password_feedback(PasswordFeedback::Hide)
67+
/// .build();
68+
/// ```
3869
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3970
#[non_exhaustive]
4071
pub enum PasswordFeedback {
@@ -53,11 +84,44 @@ pub enum PasswordFeedback {
5384
/// Configuration for prompting and reading a password.
5485
#[derive(Debug, Clone, PartialEq, Eq, Default)]
5586
pub struct Config {
56-
pub feedback: PasswordFeedback,
87+
pub(crate) feedback: PasswordFeedback,
5788
pub(crate) input_path: Option<String>,
5889
}
5990

6091
/// A builder for creating a [`Config`].
92+
///
93+
/// This struct provides a convenient way to configure the behavior of password reading,
94+
/// such as setting visual feedback and specifying an input path.
95+
///
96+
/// # Examples
97+
///
98+
/// ## Basic Usage
99+
/// ```
100+
/// use rpassword::{ConfigBuilder, PasswordFeedback};
101+
///
102+
/// let config = ConfigBuilder::new()
103+
/// .password_feedback(PasswordFeedback::Mask('*'))
104+
/// .build();
105+
/// ```
106+
///
107+
/// ## Setting a Custom Input Path
108+
/// ```
109+
/// use rpassword::ConfigBuilder;
110+
///
111+
/// let config = ConfigBuilder::new()
112+
/// .input_path("/dev/tty".to_string())
113+
/// .build();
114+
/// ```
115+
///
116+
/// ## Combining Feedback and Input Path
117+
/// ```
118+
/// use rpassword::{ConfigBuilder, PasswordFeedback};
119+
///
120+
/// let config = ConfigBuilder::new()
121+
/// .password_feedback(PasswordFeedback::PartialMask('*', 3))
122+
/// .input_path("/dev/tty".to_string())
123+
/// .build();
124+
/// ```
61125
#[derive(Debug, Clone, Default)]
62126
pub struct ConfigBuilder {
63127
feedback: PasswordFeedback,
@@ -1126,7 +1190,7 @@ mod tests {
11261190
mod windows {
11271191
use windows_sys::Win32::Foundation::{ERROR_FILE_NOT_FOUND};
11281192
use crate::{read_password_with_config, ConfigBuilder};
1129-
use std::io::{Read, Write};
1193+
use std::io::{Write};
11301194

11311195
#[test]
11321196
fn test_read_password_with_config() {

0 commit comments

Comments
 (0)