gix_credentials/protocol/
mod.rs1use bstr::BString;
2
3use crate::helper;
4
5#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Outcome {
8 pub identity: gix_sec::identity::Account,
10 pub next: helper::NextAction,
12}
13
14pub type Result = std::result::Result<Option<Outcome>, Error>;
16
17#[derive(Debug, thiserror::Error)]
19#[allow(missing_docs)]
20pub enum Error {
21 #[error(transparent)]
22 UrlParse(#[from] gix_url::parse::Error),
23 #[error("The 'url' field must be set when performing a 'get/fill' action")]
24 UrlMissing,
25 #[error(transparent)]
26 ContextDecode(#[from] context::decode::Error),
27 #[error(transparent)]
28 InvokeHelper(#[from] helper::Error),
29 #[error("Could not obtain identity for context: {}", { let mut buf = Vec::<u8>::new(); context.write_to(&mut buf).ok(); String::from_utf8_lossy(&buf).into_owned() })]
30 IdentityMissing { context: Context },
31 #[error("The handler asked to stop trying to obtain credentials")]
32 Quit,
33 #[error("Couldn't obtain {prompt}")]
34 Prompt { prompt: String, source: gix_prompt::Error },
35}
36
37#[derive(Debug, Default, Clone, Eq, PartialEq)]
39pub struct Context {
40 pub protocol: Option<String>,
42 pub host: Option<String>,
44 pub path: Option<BString>,
47 pub username: Option<String>,
49 pub password: Option<String>,
51 pub url: Option<BString>,
55 pub quit: Option<bool>,
57}
58
59#[allow(clippy::result_large_err)]
61pub fn helper_outcome_to_result(outcome: Option<helper::Outcome>, action: helper::Action) -> Result {
62 fn redact(mut ctx: Context) -> Context {
63 if let Some(pw) = ctx.password.as_mut() {
64 *pw = "<redacted>".into();
65 }
66 ctx
67 }
68 match (action, outcome) {
69 (helper::Action::Get(ctx), None) => Err(Error::IdentityMissing { context: redact(ctx) }),
70 (helper::Action::Get(ctx), Some(mut outcome)) => match outcome.consume_identity() {
71 Some(identity) => Ok(Some(Outcome {
72 identity,
73 next: outcome.next,
74 })),
75 None => Err(if outcome.quit {
76 Error::Quit
77 } else {
78 Error::IdentityMissing { context: redact(ctx) }
79 }),
80 },
81 (helper::Action::Store(_) | helper::Action::Erase(_), _ignore) => Ok(None),
82 }
83}
84
85pub mod context;