use std::collections::HashMap;
use std::ops::Deref;
use mlua::{FromLua, Lua, Result, String as LuaString, Table, TableExt, Value};
use crate::{listener::Listener, Server, StickTable};
#[derive(Clone)]
pub struct Proxy<'lua> {
class: Table<'lua>,
}
#[derive(Debug, PartialEq, Eq)]
pub enum ProxyCapability {
Frontend,
Backend,
Proxy,
Ruleset,
}
#[derive(Debug, PartialEq, Eq)]
pub enum ProxyMode {
Tcp,
Http,
Health,
Unknown,
}
impl<'lua> Proxy<'lua> {
#[inline]
pub fn get_name(&self) -> Result<String> {
self.class.call_method("get_name", ())
}
#[inline]
pub fn get_uuid(&self) -> Result<String> {
self.class.call_method("get_uuid", ())
}
#[inline]
pub fn get_servers(&self) -> Result<HashMap<String, Server<'lua>>> {
self.class.get("servers")
}
#[inline]
pub fn get_stktable(&self) -> Result<Option<StickTable<'lua>>> {
self.class.get("stktable")
}
#[inline]
pub fn get_listeners(&self) -> Result<HashMap<String, Listener<'lua>>> {
self.class.get("listeners")
}
#[inline]
pub fn pause(&self) -> Result<()> {
self.class.call_method("pause", ())
}
#[inline]
pub fn resume(&self) -> Result<()> {
self.class.call_method("resume", ())
}
#[inline]
pub fn stop(&self) -> Result<()> {
self.class.call_method("stop", ())
}
#[inline]
pub fn shut_bcksess(&self) -> Result<()> {
self.class.call_method("shut_bcksess", ())
}
#[inline]
pub fn get_cap(&self) -> Result<ProxyCapability> {
let cap: LuaString = self.class.call_method::<_, LuaString>("get_cap", ())?;
match cap.to_str()? {
"frontend" => Ok(ProxyCapability::Frontend),
"backend" => Ok(ProxyCapability::Backend),
"proxy" => Ok(ProxyCapability::Proxy),
_ => Ok(ProxyCapability::Ruleset),
}
}
#[inline]
pub fn get_mode(&self) -> Result<ProxyMode> {
let mode: LuaString = self.class.call_method("get_mode", ())?;
match mode.to_str()? {
"tcp" => Ok(ProxyMode::Tcp),
"http" => Ok(ProxyMode::Http),
"health" => Ok(ProxyMode::Health),
_ => Ok(ProxyMode::Unknown),
}
}
#[inline]
pub fn get_srv_act(&self) -> Result<usize> {
self.class.call_method("get_srv_act", ())
}
#[inline]
pub fn get_srv_bck(&self) -> Result<usize> {
self.class.call_method("get_srv_bck", ())
}
#[inline]
pub fn get_stats(&self) -> Result<Table<'lua>> {
self.class.call_method("get_stats", ())
}
}
impl<'lua> FromLua<'lua> for Proxy<'lua> {
#[inline]
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
let class = Table::from_lua(value, lua)?;
Ok(Proxy { class })
}
}
impl<'lua> Deref for Proxy<'lua> {
type Target = Table<'lua>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.class
}
}