1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Types for handling bootstrap domain names.

use std::{convert::TryFrom, fmt};

use serde::{Deserialize, Serialize};

use koibumi_core::net::{ParseSocketAddrExtError, SocketAddrExt};
use koibumi_socks_core::{ParseSocketAddrError, SocketAddr as SocksSocketAddr};

/// A bootstrap socket domain name.
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct Bootstrap(String);

impl fmt::Display for Bootstrap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<&str> for Bootstrap {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl AsRef<str> for Bootstrap {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

/// The error type returned when a conversion from a Bootstrap socket domain name
/// to a SOCKS socket address fails.
///
/// This error is used as the error type for the `TryFrom` implementation
/// for `SocksSocketAddr`.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TryFromBootstrapError {
    /// An error was caught when parsing a SOCKS socket address.
    /// The actual error caught is returned
    /// as a payload of this variant.
    ParseSocketAddrError(ParseSocketAddrError),
}

impl fmt::Display for TryFromBootstrapError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ParseSocketAddrError(err) => err.fmt(f),
        }
    }
}

impl From<ParseSocketAddrError> for TryFromBootstrapError {
    fn from(err: ParseSocketAddrError) -> Self {
        Self::ParseSocketAddrError(err)
    }
}

impl std::error::Error for TryFromBootstrapError {}

impl TryFrom<Bootstrap> for SocksSocketAddr {
    type Error = TryFromBootstrapError;

    fn try_from(bs: Bootstrap) -> Result<Self, <Self as TryFrom<Bootstrap>>::Error> {
        Ok(bs.0.parse::<Self>()?)
    }
}

/// A bootstrap socket address or an extended socket address.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum SocketAddrNode {
    /// A bootstrap socket address.
    Bootstrap(Bootstrap),
    /// An extended socket address.
    Normal(SocketAddrExt),
}

impl fmt::Display for SocketAddrNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bootstrap(addr) => addr.fmt(f),
            Self::Normal(addr) => addr.fmt(f),
        }
    }
}

impl From<Bootstrap> for SocketAddrNode {
    fn from(addr: Bootstrap) -> Self {
        Self::Bootstrap(addr)
    }
}

impl From<SocketAddrExt> for SocketAddrNode {
    fn from(addr: SocketAddrExt) -> Self {
        Self::Normal(addr)
    }
}

/// The error type returned when a conversion from a Bootstrap socket domain name
/// to a SOCKS socket address fails.
///
/// This error is used as the error type for the `TryFrom` implementation
/// for `SocksSocketAddr`.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TryFromSocketAddrNodeError {
    /// An error was caught when parsing a SOCKS socket address.
    /// The actual error caught is returned
    /// as a payload of this variant.
    ParseSocketAddrExtError(ParseSocketAddrExtError),
}

impl fmt::Display for TryFromSocketAddrNodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ParseSocketAddrExtError(err) => err.fmt(f),
        }
    }
}

impl From<ParseSocketAddrExtError> for TryFromSocketAddrNodeError {
    fn from(err: ParseSocketAddrExtError) -> Self {
        Self::ParseSocketAddrExtError(err)
    }
}

impl std::error::Error for TryFromSocketAddrNodeError {}

impl TryFrom<SocketAddrNode> for SocketAddrExt {
    type Error = TryFromSocketAddrNodeError;

    fn try_from(addr: SocketAddrNode) -> Result<Self, <Self as TryFrom<SocketAddrNode>>::Error> {
        match addr {
            SocketAddrNode::Bootstrap(addr) => Ok(addr.0.parse::<Self>()?),
            SocketAddrNode::Normal(addr) => Ok(addr),
        }
    }
}