Skip to content

Commit 5457998

Browse files
committed
feat: Add ArgumentSafety and Url::*_as_argument() methods
This adds three methods to `Url`: - `Url::user_as_argument` - `Url::host_as_argument` - `Url::path_as_argument` They return `ArgumentSafety` values that distingiush three cases: 1. There is no wrapped value (`Absent`). 2. The wrapped value is usable and presumably safe (`Usable`). 3. The wrapped value is dangerous and should not be passed as a command-line argument because it could be interpreted as an option due to starting with `-`. The value itself may still be useful and safe to include in error messages. `user_as_argument` and `host_as_argument` are the most useful ones, as they serve as alternatives to `user_argument_safe` and `host_argument_safe` whose return values are unambiguous. The `user_argument_safe` and `host_argument_safe` methods don't distinguish between the absence of a username or host, and the presence of a username or host that is unsafe to pass as an argument. `path_as_argument` included for parity, in case it is useful to write code that handles the three cases similarly. However, there is no underlying ambiguity in the return value of the corresponding `path_argument_safe` method, since unlike `user` and `host`, the `path` is not an option type.
1 parent 29941e6 commit 5457998

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

gix-url/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ pub fn expand_path(user: Option<&expand_path::ForUser>, path: &BStr) -> Result<P
6666
///
6767
/// This type only expresses known *syntactic* risk. It does not cover other risks, such as passing a personal access
6868
/// token as a username rather than a password in an application that logs usernames.
69-
enum ArgumentSafety<'a, T> {
69+
pub enum ArgumentSafety<T> {
7070
/// May be safe. There is nothing to pass, so there is nothing dangerous.
7171
Absent,
7272
/// May be safe. The argument does not begin with a `-` and so will not be confused as an option.
73-
Usable(&'a T),
73+
Usable(T),
7474
/// Dangerous! Begins with `-` and could be treated as an option. Use the value in error messages only.
75-
Dangerous(&'a T),
75+
Dangerous(T),
7676
}
7777

7878
/// A URL with support for specialized git related capabilities.
@@ -200,7 +200,7 @@ impl Url {
200200
///
201201
/// Use this method instead of [Self::user()] if the host is going to be passed to a command-line application.
202202
/// If the unsafe and absent cases need not be distinguished, [Self::user_argument_safe()] may also be used.
203-
pub fn user_as_argument(&self) -> ArgumentSafety<str> {
203+
pub fn user_as_argument(&self) -> ArgumentSafety<&str> {
204204
match self.user() {
205205
Some(user) if looks_like_command_line_option(user.as_bytes()) => ArgumentSafety::Dangerous(user),
206206
Some(user) => ArgumentSafety::Usable(user),
@@ -242,7 +242,7 @@ impl Url {
242242
///
243243
/// Use this method instead of [Self::host()] if the host is going to be passed to a command-line application.
244244
/// If the unsafe and absent cases need not be distinguished, [Self::host_argument_safe()] may also be used.
245-
pub fn host_as_argument(&self) -> ArgumentSafety<str> {
245+
pub fn host_as_argument(&self) -> ArgumentSafety<&str> {
246246
match self.host() {
247247
Some(host) if looks_like_command_line_option(host.as_bytes()) => ArgumentSafety::Dangerous(host),
248248
Some(host) => ArgumentSafety::Usable(host),
@@ -269,7 +269,7 @@ impl Url {
269269
/// passed to a command-line application, unless it is certain that the leading `/` will always be included.
270270
///
271271
/// This method never returns an [ArgumentSafety::Absent].
272-
pub fn path_as_argument(&self) -> ArgumentSafety<BStr> {
272+
pub fn path_as_argument(&self) -> ArgumentSafety<&BStr> {
273273
match self.path_argument_safe() {
274274
Some(path) => ArgumentSafety::Usable(path),
275275
None => ArgumentSafety::Dangerous(self.path.as_ref()),

0 commit comments

Comments
 (0)