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_mut(&mut self) -> &mut dyn Prompter;
38}
39
40/// Implement `ClonePrompter` for clonable Prompters.
41impl<P> ClonePrompter for P
42where
43 P: Prompter + Clone + 'static,
44{
45 fn dyn_clone(&self) -> Box<dyn ClonePrompter> {
46 Box::new(self.clone())
47 }
48
49 fn as_prompter_mut(&mut self) -> &mut dyn Prompter {
50 self
51 }
52}
53
54impl Clone for Box<dyn ClonePrompter> {
55 fn clone(&self) -> Self {
56 self.dyn_clone()
57 }
58}