volans_core/upgrade/
error.rs1use std::fmt;
2
3use volans_stream_select::NegotiationError;
4
5#[derive(Debug)]
6pub enum UpgradeError<E> {
7 Select(NegotiationError),
8 Apply(E),
9}
10
11impl<E> UpgradeError<E> {
12 pub fn map_err<F, T>(self, f: F) -> UpgradeError<T>
13 where
14 F: FnOnce(E) -> T,
15 {
16 match self {
17 UpgradeError::Select(e) => UpgradeError::Select(e),
18 UpgradeError::Apply(e) => UpgradeError::Apply(f(e)),
19 }
20 }
21
22 pub fn into_err<T>(self) -> UpgradeError<T>
23 where
24 T: From<E>,
25 {
26 self.map_err(Into::into)
27 }
28}
29
30impl<E> fmt::Display for UpgradeError<E>
31where
32 E: fmt::Display,
33{
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 match self {
36 UpgradeError::Select(_) => write!(f, "Stream select failed"),
37 UpgradeError::Apply(_) => write!(f, "Handshake failed"),
38 }
39 }
40}
41
42impl<E> std::error::Error for UpgradeError<E>
43where
44 E: std::error::Error + 'static,
45{
46 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47 match self {
48 UpgradeError::Select(e) => Some(e),
49 UpgradeError::Apply(e) => Some(e),
50 }
51 }
52}
53
54impl<E> From<NegotiationError> for UpgradeError<E> {
55 fn from(e: NegotiationError) -> Self {
56 UpgradeError::Select(e)
57 }
58}