auth_git2/
prompter.rs

1use std::path::Path;
2
3/// Trait for customizing user prompts.
4///
5/// You can provide an implementor of this trait to customize the way a user is prompted for credentials and passphrases.
6pub trait Prompter: Send {
7	/// Promp the user for a username and password.
8	///
9	/// If the prompt fails or the user fails to provide the requested information, this function should return `None`.
10	fn prompt_username_password(&mut self, url: &str, git_config: &git2::Config) -> Option<(String, String)>;
11
12	/// Promp the user for a password when the username is already known.
13	///
14	/// If the prompt fails or the user fails to provide the requested information, this function should return `None`.
15	fn prompt_password(&mut self, username: &str, url: &str, git_config: &git2::Config) -> Option<String>;
16
17	/// Promp the user for the passphrase of an encrypted SSH key.
18	///
19	/// If the prompt fails or the user fails to provide the requested information, this function should return `None`.
20	fn prompt_ssh_key_passphrase(&mut self, private_key_path: &Path, git_config: &git2::Config) -> Option<String>;
21}
22
23/// Wrap a clonable [`Prompter`] in a `Box<dyn MakePrompter>`.
24pub(crate) fn wrap_prompter<P>(prompter: P) -> Box<dyn ClonePrompter>
25where
26	P: Prompter + Clone + 'static,
27{
28	Box::new(prompter)
29}
30
31/// Trait to allow making clones of a `Box<dyn Prompter + Send>`.
32pub(crate) trait ClonePrompter: Prompter {
33	/// Clone the `Box<dyn ClonePrompter>`.
34	fn dyn_clone(&self) -> Box<dyn ClonePrompter>;
35
36	/// Get `self` as plain `Prompter`.
37	fn as_prompter(&self) -> &dyn Prompter;
38
39	/// Get `self` as plain `Prompter`.
40	fn as_prompter_mut(&mut self) -> &mut dyn Prompter;
41}
42
43/// Implement `ClonePrompter` for clonable Prompters.
44impl<P> ClonePrompter for P
45where
46	P: Prompter + Clone + 'static,
47{
48	fn dyn_clone(&self) -> Box<dyn ClonePrompter> {
49		Box::new(self.clone())
50	}
51
52	fn as_prompter(&self) -> &dyn Prompter {
53		self
54	}
55
56	fn as_prompter_mut(&mut self) -> &mut dyn Prompter {
57		self
58	}
59}
60
61impl Clone for Box<dyn ClonePrompter> {
62	fn clone(&self) -> Self {
63		self.dyn_clone()
64	}
65}