| 1 | use bstr::BString; |
| 2 | |
| 3 | /// Indicates key or values contain errors that can't be encoded. |
| 4 | #[derive(Debug, thiserror::Error)] |
| 5 | #[allow(missing_docs)] |
| 6 | pub enum Error { |
| 7 | #[error("{key:?}={value:?} must not contain null bytes or newlines neither in key nor in value.")] |
| 8 | Encoding { key: String, value: BString }, |
| 9 | } |
| 10 | |
| 11 | mod access { |
| 12 | use bstr::BString; |
| 13 | |
| 14 | use crate::protocol::Context; |
| 15 | |
| 16 | impl Context { |
| 17 | /// Clear all fields that are considered secret. |
| 18 | pub fn clear_secrets(&mut self) { |
| 19 | let Context { |
| 20 | protocol: _, |
| 21 | host: _, |
| 22 | path: _, |
| 23 | username: _, |
| 24 | password, |
| 25 | oauth_refresh_token, |
| 26 | password_expiry_utc: _, |
| 27 | url: _, |
| 28 | quit: _, |
| 29 | } = self; |
| 30 | |
| 31 | *password = None; |
| 32 | *oauth_refresh_token = None; |
| 33 | } |
| 34 | /// Replace existing secrets with the word `<redacted>`. |
| 35 | pub fn redacted(mut self) -> Self { |
| 36 | let Context { |
| 37 | protocol: _, |
| 38 | host: _, |
| 39 | path: _, |
| 40 | username: _, |
| 41 | password, |
| 42 | oauth_refresh_token, |
| 43 | password_expiry_utc: _, |
| 44 | url: _, |
| 45 | quit: _, |
| 46 | } = &mut self; |
| 47 | for secret in [password, oauth_refresh_token].into_iter().flatten() { |
| 48 | *secret = "<redacted>".into(); |
| 49 | } |
| 50 | self |
| 51 | } |
| 52 | |
| 53 | /// Convert all relevant fields into a URL for consumption. |
| 54 | pub fn to_url(&self) -> Option<BString> { |
| 55 | use bstr::{ByteSlice, ByteVec}; |
| 56 | let mut buf: BString = self.protocol.clone()?.into(); |
| 57 | buf.push_str(b"://"); |
| 58 | if let Some(user) = &self.username { |
| 59 | buf.push_str(user); |
| 60 | buf.push(b'@'); |
| 61 | } |
| 62 | if let Some(host) = &self.host { |
| 63 | buf.push_str(host); |
| 64 | } |
| 65 | if let Some(path) = &self.path { |
| 66 | if !path.starts_with_str("/") { |
| 67 | buf.push(b'/'); |
| 68 | } |
| 69 | buf.push_str(path); |
| 70 | } |
| 71 | buf.into() |
| 72 | } |
| 73 | /// Compute a prompt to obtain the given value. |
| 74 | pub fn to_prompt(&self, field: &str) -> String { |
| 75 | match self.to_url() { |
| 76 | Some(url) => format!("{field} for {url}: "), |
| 77 | None => format!("{field}: "), |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | mod mutate { |
| 84 | use bstr::ByteSlice; |
| 85 | |
| 86 | use crate::{protocol, protocol::Context}; |
| 87 | |
| 88 | /// In-place mutation |
| 89 | impl Context { |
| 90 | /// Destructure the url at our `url` field into parts like protocol, host, username and path and store |
| 91 | /// them in our respective fields. If `use_http_path` is set, http paths are significant even though |
| 92 | /// normally this isn't the case. |
| 93 | #[allow(clippy::result_large_err)] |
| 94 | pub fn destructure_url_in_place(&mut self, use_http_path: bool) -> Result<&mut Self, protocol::Error> { |
| 95 | if self.url.is_none() { |
| 96 | self.url = Some(self.to_url().ok_or(protocol::Error::UrlMissing)?); |
| 97 | } |
| 98 | |
| 99 | let url = gix_url::parse(self.url.as_ref().expect("URL is present after check above").as_ref())?; |
| 100 | self.protocol = Some(url.scheme.as_str().into()); |
| 101 | self.username = url.user().map(ToOwned::to_owned); |
| 102 | self.password = url.password().map(ToOwned::to_owned); |
| 103 | self.host = url.host().map(ToOwned::to_owned).map(|mut host| { |
| 104 | let port = url.port.filter(|port| { |
| 105 | url.scheme |
| 106 | .default_port() |
| 107 | .is_none_or(|default_port| *port != default_port) |
| 108 | }); |
| 109 | if let Some(port) = port { |
| 110 | use std::fmt::Write; |
| 111 | write!(host, ":{port}").expect("infallible"); |
| 112 | } |
| 113 | host |
| 114 | }); |
| 115 | if !matches!(url.scheme, gix_url::Scheme::Http | gix_url::Scheme::Https) || use_http_path { |
| 116 | let path = url.path.trim_with(|b| b == '/'); |
| 117 | self.path = (!path.is_empty()).then(|| path.into()); |
| 118 | } |
| 119 | Ok(self) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | mod serde; |
| 125 | pub use self::serde::decode; |