From 3b2df5022cd0a4fba58ec562d903c9c182351fe5 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 25 Oct 2024 10:45:17 +0200 Subject: [PATCH] examples: don't force io::Error from errors --- examples/client.rs | 10 ++++------ examples/server.rs | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index 1c8f9df..90dc3e6 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -1,3 +1,4 @@ +use std::error::Error as StdError; use std::fs::File; use std::io; use std::io::BufReader; @@ -32,7 +33,7 @@ struct Options { } #[tokio::main] -async fn main() -> io::Result<()> { +async fn main() -> Result<(), Box> { let options: Options = argh::from_env(); let addr = (options.host.as_str(), options.port) @@ -46,7 +47,7 @@ async fn main() -> io::Result<()> { if let Some(cafile) = &options.cafile { let mut pem = BufReader::new(File::open(cafile)?); for cert in rustls_pemfile::certs(&mut pem) { - root_cert_store.add(cert?).unwrap(); + root_cert_store.add(cert?)?; } } else { root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); @@ -61,10 +62,7 @@ async fn main() -> io::Result<()> { let (mut stdin, mut stdout) = (tokio_stdin(), tokio_stdout()); - let domain = ServerName::try_from(domain.as_str()) - .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid dnsname"))? - .to_owned(); - + let domain = ServerName::try_from(domain.as_str())?.to_owned(); let mut stream = connector.connect(domain, stream).await?; stream.write_all(content.as_bytes()).await?; diff --git a/examples/server.rs b/examples/server.rs index 138bdb2..57a2cff 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -3,6 +3,7 @@ use std::io::{self, BufReader, ErrorKind}; use std::net::ToSocketAddrs; use std::path::{Path, PathBuf}; use std::sync::Arc; +use std::error::Error as StdError; use argh::FromArgs; use rustls::pki_types::{CertificateDer, PrivateKeyDer}; @@ -45,7 +46,7 @@ fn load_key(path: &Path) -> io::Result> { } #[tokio::main] -async fn main() -> io::Result<()> { +async fn main() -> Result<(), Box> { let options: Options = argh::from_env(); let addr = options @@ -59,8 +60,7 @@ async fn main() -> io::Result<()> { let config = rustls::ServerConfig::builder() .with_no_client_auth() - .with_single_cert(certs, key) - .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; + .with_single_cert(certs, key)?; let acceptor = TlsAcceptor::from(Arc::new(config)); let listener = TcpListener::bind(&addr).await?;