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
use crate::{events::Events, VpnClient};
use thiserror::Error;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum OpenconnectError {
    #[error("Failed to create new VPN entry point: {0}")]
    EntrypointConfigError(String),

    #[error("Failed to setup shutdown error: {0}")]
    SetupShutdownError(String),

    #[error("Failed to setup cookie manually. Error code: {0}")]
    SetCookieError(i32),

    #[error("Failed to obtain cookie from server. Error code: {0}")]
    ObtainCookieError(i32),

    #[error("Failed to set protocol. Error code: {0}")]
    SetProtocolError(i32),

    #[error("Failed to set reported OS. Error code: {0}")]
    SetReportOSError(i32),

    #[error("Failed to setup command pipe. Error code: {0}")]
    CmdPipeError(i32),

    #[error("Failed to set HTTP proxy. Error code: {0}")]
    SetProxyError(i32),

    #[error("Failed to make CSTP connection. Error code: {0}")]
    MakeCstpError(i32),

    #[error("Failed to disable DTLS. Error code: {0}")]
    DisableDTLSError(i32),

    #[error("Failed to parse URL. Error code: {0}")]
    ParseUrlError(i32),

    #[error("Failed to setup tun device. Error code: {0}")]
    SetupTunDeviceEror(i32),

    #[error("Failed to set client certificate. Error code: {0}")]
    SetClientCertError(i32),

    #[error("Failed to set MCA certificate. Error code: {0}")]
    SetMCACertError(i32),

    #[error("Failed to set MCA private key. Error code: {0}")]
    MainLoopError(i32),

    #[error("Failed to get IP info. Error code: {0}")]
    GetIpInfoError(i32),

    #[error("Other general error: {0}")]
    OtherError(String),
}

pub type OpenconnectResult<T> = std::result::Result<T, OpenconnectError>;

pub trait EmitError<T> {
    fn emit_error(self, client: &VpnClient) -> OpenconnectResult<T>;
}

impl<T> EmitError<T> for OpenconnectResult<T> {
    fn emit_error(self, client: &VpnClient) -> OpenconnectResult<T> {
        self.inspect_err(|e| client.emit_error(e))
    }
}