use std::{io, net::SocketAddr, result, time::SystemTimeError};
use openssl::error::ErrorStack;
use thiserror::Error;
use tokio::net::TcpStream;
use tokio_openssl::HandshakeError;
use crate::tls::ValidationError;
pub(super) type Result<T> = result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("no server certificate presented")]
NoServerCertificate,
#[error("no client certificate presented")]
NoClientCertificate,
#[error("remote node has wrong ID")]
WrongId,
#[error("need either both or none of cert, secret_key in network config")]
InvalidConfig,
#[error("own certificate invalid")]
OwnCertificateInvalid(#[source] ValidationError),
#[error("failed to create listener on {1}")]
ListenerCreation(#[source] io::Error, SocketAddr),
#[error("failed to get listener addr")]
ListenerAddr(#[source] io::Error),
#[error("failed to convert listener to tokio")]
ListenerConversion(#[source] io::Error),
#[error("failed to resolve network address")]
ResolveAddr(#[source] io::Error),
#[error("failed to send message")]
MessageNotSent(#[source] io::Error),
#[error("failed to create acceptor")]
AcceptorCreation(#[source] ErrorStack),
#[error("failed to configure connector")]
ConnectorConfiguration(#[source] ErrorStack),
#[error("failed to generate cert")]
CertificateGeneration(#[source] ErrorStack),
#[error("handshake error: {0}")]
Handshake(#[from] HandshakeError<TcpStream>),
#[error("TLS validation error: {0}")]
TlsValidation(#[from] ValidationError),
#[error("system time error: {0}")]
SystemTime(#[from] SystemTimeError),
#[error("could not interact with systemd: {0}")]
SystemD(io::Error),
#[error(transparent)]
Anyhow(#[from] anyhow::Error),
#[error("failed to create outgoing connection as server has stopped")]
ServerStopped,
}