Skip to content

Commit

Permalink
examples: don't force io::Error from errors
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Oct 25, 2024
1 parent 20c2bb0 commit 3b2df50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 4 additions & 6 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::error::Error as StdError;
use std::fs::File;
use std::io;
use std::io::BufReader;
Expand Down Expand Up @@ -32,7 +33,7 @@ struct Options {
}

#[tokio::main]
async fn main() -> io::Result<()> {
async fn main() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
let options: Options = argh::from_env();

let addr = (options.host.as_str(), options.port)
Expand All @@ -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());
Expand All @@ -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?;

Expand Down
6 changes: 3 additions & 3 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -45,7 +46,7 @@ fn load_key(path: &Path) -> io::Result<PrivateKeyDer<'static>> {
}

#[tokio::main]
async fn main() -> io::Result<()> {
async fn main() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
let options: Options = argh::from_env();

let addr = options
Expand All @@ -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?;
Expand Down

0 comments on commit 3b2df50

Please sign in to comment.