Skip to content

Commit c9d907b

Browse files
Apply rustfmt to command.rs and escape.rs
Signed-off-by: Aalekh Patel <aalekh@protectchildren.ca>
1 parent 3673202 commit c9d907b

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

src/command.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,34 @@ macro_rules! delegate {
5555
/// Primarily a way to allow `std::process::Command` to be turned directly into an `openssh::Command`.
5656
pub trait OverSsh {
5757
/// Given a session, return a command that can be executed over that session.
58-
///
59-
/// **Note**: The command to be executed on the remote machine does not include
58+
///
59+
/// **Note**: The command to be executed on the remote machine does not include
6060
/// any environment variables or the current directory. They must be
6161
/// set explicitly, if desired.
62-
///
62+
///
6363
/// # Example
6464
/// Consider the implementation of `OverSsh` for `std::process::Command`:
65-
///
65+
///
6666
/// ```no_run
6767
/// # #[tokio::main(flavor = "current_thread")]
6868
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
6969
/// use std::process::Command;
7070
/// use openssh::{Session, KnownHosts, OverSsh};
7171
///
7272
/// let session = Session::connect_mux("me@ssh.example.com", KnownHosts::Strict).await?;
73-
/// let ls =
73+
/// let ls =
7474
/// Command::new("ls")
7575
/// .arg("-l")
7676
/// .arg("-a")
7777
/// .arg("-h")
7878
/// .over_session(&session)
7979
/// .output()
8080
/// .await?;
81-
///
81+
///
8282
/// assert!(String::from_utf8(ls.stdout).unwrap().contains("total"));
8383
/// # Ok(())
8484
/// }
85-
///
85+
///
8686
/// ```
8787
fn over_session<'session>(&self, session: &'session Session) -> crate::Command<'session>;
8888
}
@@ -91,26 +91,22 @@ impl OverSsh for std::process::Command {
9191
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
9292
let program_escaped: Cow<'_, OsStr> = escape(self.get_program());
9393
let mut command = session.raw_command(program_escaped);
94-
95-
let args = self
96-
.get_args()
97-
.map(escape);
94+
95+
let args = self.get_args().map(escape);
9896
command.raw_args(args);
9997
command
10098
}
10199
}
102100

103-
104101
impl OverSsh for tokio::process::Command {
105102
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
106103
self.as_std().over_session(session)
107104
}
108105
}
109106

110-
111107
impl<S> OverSsh for &S
112108
where
113-
S: OverSsh
109+
S: OverSsh,
114110
{
115111
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
116112
<S as OverSsh>::over_session(self, session)
@@ -119,7 +115,7 @@ where
119115

120116
impl<S> OverSsh for Box<S>
121117
where
122-
S: OverSsh
118+
S: OverSsh,
123119
{
124120
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
125121
<S as OverSsh>::over_session(self, session)
@@ -128,7 +124,7 @@ where
128124

129125
impl<S> OverSsh for std::rc::Rc<S>
130126
where
131-
S: OverSsh
127+
S: OverSsh,
132128
{
133129
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
134130
<S as OverSsh>::over_session(self, session)
@@ -137,7 +133,7 @@ where
137133

138134
impl<S> OverSsh for std::sync::Arc<S>
139135
where
140-
S: OverSsh
136+
S: OverSsh,
141137
{
142138
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
143139
<S as OverSsh>::over_session(self, session)

src/escape.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
//! Escape characters that may have special meaning in a shell, including spaces.
22
//! This is a modified version of the [`shell-escape::unix`] module of [`shell-escape`] crate.
3-
//!
3+
//!
44
//! [`shell-escape`]: https://crates.io/crates/shell-escape
55
//! [`shell-escape::unix`]: https://docs.rs/shell-escape/latest/src/shell_escape/lib.rs.html#101
66
77
use std::{
8-
borrow::Cow,
8+
borrow::Cow,
99
ffi::{OsStr, OsString},
1010
os::unix::ffi::OsStrExt,
1111
os::unix::ffi::OsStringExt,
1212
};
1313

14-
1514
fn whitelisted(byte: u8) -> bool {
1615
matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_' | b'=' | b'/' | b',' | b'.' | b'+')
1716
}
1817

1918
/// Escape characters that may have special meaning in a shell, including spaces.
20-
///
19+
///
2120
/// **Note**: This function is an adaptation of [`shell-escape::unix::escape`].
2221
/// This function exists only for type compatibility and the implementation is
2322
/// almost exactly the same as [`shell-escape::unix::escape`].
24-
///
23+
///
2524
/// [`shell-escape::unix::escape`]: https://docs.rs/shell-escape/latest/src/shell_escape/lib.rs.html#101
26-
///
25+
///
2726
pub(crate) fn escape(s: &OsStr) -> Cow<'_, OsStr> {
2827
let as_bytes = s.as_bytes();
2928
let all_whitelisted = as_bytes.iter().copied().all(whitelisted);
@@ -51,7 +50,6 @@ pub(crate) fn escape(s: &OsStr) -> Cow<'_, OsStr> {
5150
OsString::from_vec(escaped).into()
5251
}
5352

54-
5553
#[cfg(test)]
5654
mod tests {
5755
use super::*;
@@ -75,36 +73,21 @@ mod tests {
7573
fn test_escape() {
7674
test_escape_case(
7775
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+",
78-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+"
79-
);
80-
test_escape_case(
81-
"--aaa=bbb-ccc",
82-
"--aaa=bbb-ccc"
76+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+",
8377
);
78+
test_escape_case("--aaa=bbb-ccc", "--aaa=bbb-ccc");
8479
test_escape_case(
8580
"linker=gcc -L/foo -Wl,bar",
86-
r#"'linker=gcc -L/foo -Wl,bar'"#
87-
);
88-
test_escape_case(
89-
r#"--features="default""#,
90-
r#"'--features="default"'"#
91-
);
92-
test_escape_case(
93-
r#"'!\$`\\\n "#,
94-
r#"''\'''\!'\$`\\\n '"#
95-
);
96-
test_escape_case(
97-
"",
98-
r#"''"#
99-
);
100-
test_escape_case(
101-
" ",
102-
r#"' '"#
81+
r#"'linker=gcc -L/foo -Wl,bar'"#,
10382
);
83+
test_escape_case(r#"--features="default""#, r#"'--features="default"'"#);
84+
test_escape_case(r#"'!\$`\\\n "#, r#"''\'''\!'\$`\\\n '"#);
85+
test_escape_case("", r#"''"#);
86+
test_escape_case(" ", r#"' '"#);
10487

10588
test_escape_from_bytes(
10689
&[0x66, 0x6f, 0x80, 0x6f],
107-
&[b'\'', 0x66, 0x6f, 0x80, 0x6f, b'\'']
90+
&[b'\'', 0x66, 0x6f, 0x80, 0x6f, b'\''],
10891
);
10992
}
11093
}

0 commit comments

Comments
 (0)