Skip to content

Commit f296fc7

Browse files
authored
ice/examples: improve error handling and formatting in ping_pong example (#699)
- Replace verbose match statements with ? operator for HTTP request handling - Fix spacing around else clauses to follow Rust formatting conventions - Simplify error propagation in remoteAuth POST request flow Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
1 parent 0d1d1ee commit f296fc7

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

ice/examples/ping_pong.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,16 @@ async fn main() -> Result<(), Error> {
249249
let (local_ufrag, local_pwd) = ice_agent.get_local_user_credentials().await;
250250

251251
println!("posting remoteAuth with {local_ufrag}:{local_pwd}");
252-
let req = match Request::builder()
252+
let req = Request::builder()
253253
.method(Method::POST)
254254
.uri(format!("http://localhost:{remote_http_port}/remoteAuth"))
255255
.body(Body::from(format!("{local_ufrag}:{local_pwd}")))
256-
{
257-
Ok(req) => req,
258-
Err(err) => return Err(Error::Other(format!("{err}"))),
259-
};
260-
let resp = match client.request(req).await {
261-
Ok(resp) => resp,
262-
Err(err) => return Err(Error::Other(format!("{err}"))),
263-
};
256+
.map_err(|err| Error::Other(format!("{err}")))?;
257+
258+
let resp = client
259+
.request(req)
260+
.await
261+
.map_err(|err| Error::Other(format!("{err}")))?;
264262
println!("Response from remoteAuth: {}", resp.status());
265263

266264
let (remote_ufrag, remote_pwd) = {
@@ -291,11 +289,11 @@ async fn main() -> Result<(), Error> {
291289
println!("add_remote_candidate: {c}");
292290
let c: Arc<dyn Candidate + Send + Sync> = Arc::new(c);
293291
let _ = ice_agent2.add_remote_candidate(&c);
294-
}else{
292+
} else {
295293
println!("unmarshal_candidate error!");
296294
break;
297295
}
298-
}else{
296+
} else {
299297
println!("REMOTE_CAND_CHANNEL done!");
300298
break;
301299
}
@@ -343,7 +341,7 @@ async fn main() -> Result<(), Error> {
343341
if let Err(err) = result {
344342
eprintln!("conn_tx send error: {err}");
345343
break;
346-
}else{
344+
} else {
347345
println!("Sent: '{val}'");
348346
}
349347
}

0 commit comments

Comments
 (0)