From 2eb4d07bedd44b87d2c6745eebbf4741f85e20e7 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Mon, 18 Dec 2023 12:49:30 +0100 Subject: [PATCH] Fix z_pub sample attachment string parsing --- examples/examples/z_pub.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/examples/z_pub.rs b/examples/examples/z_pub.rs index 097b686de9..b892eeafc3 100644 --- a/examples/examples/z_pub.rs +++ b/examples/examples/z_pub.rs @@ -40,7 +40,7 @@ async fn main() { put = put.with_attachment( attachment .split('&') - .map(|pair| pair.as_bytes().split_at(pair.find('=').unwrap_or(0))) + .map(|pair| split_once(pair, '=')) .collect(), ) } @@ -65,6 +65,17 @@ struct Args { common: CommonArgs, } +fn split_once(s: &str, c: char) -> (&[u8], &[u8]) { + let s_bytes = s.as_bytes(); + match s.find(c) { + Some(index) => { + let (l, r) = s_bytes.split_at(index); + (l, &r[1..]) + } + None => (s_bytes, &[]), + } +} + fn parse_args() -> (Config, KeyExpr<'static>, String, Option) { let args = Args::parse(); (args.common.into(), args.key, args.value, args.attach)