1use std::collections::HashMap;
2use std::ops::Deref;
3
4use mlua::{FromLua, Lua, ObjectLike, Result, String as LuaString, Table, Value};
5
6use crate::{listener::Listener, Server, StickTable};
7
8#[derive(Clone)]
11pub struct Proxy(Table);
12
13#[derive(Debug, PartialEq, Eq)]
14pub enum ProxyCapability {
15 Frontend,
16 Backend,
17 Proxy,
18 Ruleset,
19}
20
21#[derive(Debug, PartialEq, Eq)]
22pub enum ProxyMode {
23 Tcp,
24 Http,
25 Health,
26 Unknown,
27}
28
29impl Proxy {
30 #[inline]
32 pub fn get_name(&self) -> Result<String> {
33 self.0.call_method("get_name", ())
34 }
35
36 #[inline]
38 pub fn get_uuid(&self) -> Result<String> {
39 self.0.call_method("get_uuid", ())
40 }
41
42 #[inline]
45 pub fn get_servers(&self) -> Result<HashMap<String, Server>> {
46 self.0.get("servers")
47 }
48
49 #[inline]
51 pub fn get_stktable(&self) -> Result<Option<StickTable>> {
52 self.0.get("stktable")
53 }
54
55 #[inline]
58 pub fn get_listeners(&self) -> Result<HashMap<String, Listener>> {
59 self.0.get("listeners")
60 }
61
62 #[inline]
65 pub fn pause(&self) -> Result<()> {
66 self.0.call_method("pause", ())
67 }
68
69 #[inline]
72 pub fn resume(&self) -> Result<()> {
73 self.0.call_method("resume", ())
74 }
75
76 #[inline]
79 pub fn stop(&self) -> Result<()> {
80 self.0.call_method("stop", ())
81 }
82
83 #[inline]
86 pub fn shut_bcksess(&self) -> Result<()> {
87 self.0.call_method("shut_bcksess", ())
88 }
89
90 #[inline]
92 pub fn get_cap(&self) -> Result<ProxyCapability> {
93 let cap: LuaString = self.0.call_method("get_cap", ())?;
94 match cap.to_str()?.deref() {
95 "frontend" => Ok(ProxyCapability::Frontend),
96 "backend" => Ok(ProxyCapability::Backend),
97 "proxy" => Ok(ProxyCapability::Proxy),
98 _ => Ok(ProxyCapability::Ruleset),
99 }
100 }
101
102 #[inline]
104 pub fn get_mode(&self) -> Result<ProxyMode> {
105 let mode: LuaString = self.0.call_method("get_mode", ())?;
106 match mode.to_str()?.deref() {
107 "tcp" => Ok(ProxyMode::Tcp),
108 "http" => Ok(ProxyMode::Http),
109 "health" => Ok(ProxyMode::Health),
110 _ => Ok(ProxyMode::Unknown),
111 }
112 }
113
114 #[inline]
117 pub fn get_srv_act(&self) -> Result<usize> {
118 self.0.call_method("get_srv_act", ())
119 }
120
121 #[inline]
123 pub fn get_srv_bck(&self) -> Result<usize> {
124 self.0.call_method("get_srv_bck", ())
125 }
126
127 #[inline]
130 pub fn get_stats(&self) -> Result<Table> {
131 self.0.call_method("get_stats", ())
132 }
133}
134
135impl FromLua for Proxy {
136 #[inline]
137 fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
138 let class = Table::from_lua(value, lua)?;
139 Ok(Proxy(class))
140 }
141}
142
143impl Deref for Proxy {
144 type Target = Table;
145
146 #[inline]
147 fn deref(&self) -> &Self::Target {
148 &self.0
149 }
150}