use core::fmt;
use std::io;
use crate::{TransportDiagnostic, TransportFailurePhase};
#[derive(Debug)]
pub struct TransportError {
diagnostic: TransportDiagnostic,
source: Option<io::Error>,
}
impl TransportError {
pub const fn new(diagnostic: TransportDiagnostic) -> Self {
Self {
diagnostic,
source: None,
}
}
pub fn from_io(phase: TransportFailurePhase, source: io::Error) -> Self {
Self {
diagnostic: TransportDiagnostic::from_io(phase, &source),
source: Some(source),
}
}
pub const fn diagnostic(&self) -> TransportDiagnostic {
self.diagnostic
}
}
impl fmt::Display for TransportError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.source.as_ref() {
Some(source) => source.fmt(formatter),
None => formatter.write_str("transport progression failed"),
}
}
}
impl core::error::Error for TransportError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
self.source
.as_ref()
.map(|source| source as &(dyn core::error::Error + 'static))
}
}