summaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2022-04-20 15:04:41 +0200
committerJustus Winter <justus@sequoia-pgp.org>2022-04-21 14:13:44 +0200
commitec5755545f7ce2e886ee5f1346df08015d229e31 (patch)
treea624d43b422d26f2793f6abb4180f40b4f7949e5 /ipc
parent9bd8116d2c90408d3381953733725da2746849bc (diff)
ipc: New function assuan::escape.
Diffstat (limited to 'ipc')
-rw-r--r--ipc/src/assuan/mod.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/ipc/src/assuan/mod.rs b/ipc/src/assuan/mod.rs
index 5d6fd3d7..4b1df128 100644
--- a/ipc/src/assuan/mod.rs
+++ b/ipc/src/assuan/mod.rs
@@ -74,6 +74,21 @@ enum WriteState {
}
assert_send_and_sync!(WriteState);
+/// Percent-escapes the given string.
+pub fn escape<S: AsRef<str>>(s: S) -> String {
+ let mut r = String::with_capacity(s.as_ref().len());
+ for c in s.as_ref().chars() {
+ match c {
+ '%' => r.push_str("%25"),
+ ' ' => r.push('+'),
+ n if n.is_ascii() && (n as u8) < 32 =>
+ r.push_str(&format!("%{:02X}", n as u8)),
+ _ => r.push(c),
+ }
+ }
+ r
+}
+
impl Client {
/// Connects to the server.
pub async fn connect<P>(path: P) -> Result<Client> where P: AsRef<Path> {
@@ -103,7 +118,7 @@ impl Client {
/// [`Connection::cancel()`]: Client::cancel()
///
/// Note: `command` is passed as-is. Control characters, like
- /// `%`, must be %-escaped.
+ /// `%`, must be %-escaped using [`escape`].
pub fn send<'a, C: 'a>(&'a mut self, command: C) -> Result<()>
where C: AsRef<[u8]>
{