irelia_cli/
lib.rs

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
#![warn(clippy::pedantic)]
#![forbid(unsafe_code)]

//! Irelia is an async set of bindings to the LCU API
//!
//! Features are broken down as follows:
//! - `in_game`: Allows connections to the `in_game` API, return types are auto generated
//! - `rest`: Allows connections to the LCU `rest` API, providing basic get/post functionality
//! - `ws`: Allows connections to the LCU websocket API, providing all functionality needed
pub use irelia_encoder;

#[cfg(feature = "in_game")]
pub mod in_game;
#[cfg(feature = "rest")]
pub mod rest;
pub mod utils;
#[cfg(feature = "ws")]
pub mod ws;

/// Errors that can be produced by the LCU API
///
/// This contains errors from `serde_json`, `hyper` and `tungstenite`
#[derive(Debug)]
pub enum Error {
    #[cfg(any(feature = "in_game", feature = "rest"))]
    HyperHttpError(hyper::http::Error),
    #[cfg(any(feature = "in_game", feature = "rest"))]
    HyperClientError(hyper_util::client::legacy::Error),
    #[cfg(any(feature = "in_game", feature = "rest"))]
    HyperError(hyper::Error),
    #[cfg(feature = "ws")]
    WebsocketError(tokio_tungstenite::tungstenite::Error),
    #[cfg(any(feature = "ws", feature = "rest"))]
    StdIo(std::io::Error),
    SerdeJsonError(serde_json::Error),
    LCUProcessNotRunning,
    PortNotFound,
    AuthTokenNotFound,
    LockFileNotFound,
}

#[cfg(any(feature = "in_game", feature = "rest"))]
impl From<hyper::http::Error> for Error {
    fn from(value: hyper::http::Error) -> Self {
        Self::HyperHttpError(value)
    }
}

#[cfg(any(feature = "in_game", feature = "rest"))]
impl From<hyper_util::client::legacy::Error> for Error {
    fn from(value: hyper_util::client::legacy::Error) -> Self {
        Self::HyperClientError(value)
    }
}

#[cfg(any(feature = "in_game", feature = "rest"))]
impl From<hyper::Error> for Error {
    fn from(value: hyper::Error) -> Self {
        Self::HyperError(value)
    }
}

#[cfg(feature = "ws")]
impl From<tokio_tungstenite::tungstenite::Error> for Error {
    fn from(value: tokio_tungstenite::tungstenite::Error) -> Self {
        Self::WebsocketError(value)
    }
}

impl From<serde_json::Error> for Error {
    fn from(value: serde_json::Error) -> Self {
        Self::SerdeJsonError(value)
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::StdIo(value)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let error = match self {
            #[cfg(any(feature = "in_game", feature = "rest"))]
            Error::HyperHttpError(err) => err.to_string(),
            #[cfg(any(feature = "in_game", feature = "rest"))]
            Error::HyperError(err) => err.to_string(),
            #[cfg(any(feature = "in_game", feature = "rest"))]
            Error::HyperClientError(err) => err.to_string(),
            Error::SerdeJsonError(err) => err.to_string(),
            #[cfg(feature = "ws")]
            Error::WebsocketError(err) => err.to_string(),
            Error::StdIo(err) => err.to_string(),
            Error::LCUProcessNotRunning => String::from("LCU Process is not running!"),
            Error::PortNotFound => String::from("Port Not Found!"),
            Error::AuthTokenNotFound => String::from("Auth Token Not Found!"),
            Error::LockFileNotFound => {
                String::from("Unable to the lock file, but the process was running!")
            }
        };
        f.write_str(&error)
    }
}

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

#[cfg(feature = "tauri")]
impl serde::Serialize for Error {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

#[cfg(any(feature = "rest", feature = "in_game"))]
pub use utils::requests::RequestClient;