haproxy_api/
txn.rs

1use std::ops::Deref;
2
3use mlua::{FromLua, IntoLua, Lua, Result, Table, TableExt, Value};
4
5use crate::{Converters, Fetches, Http, HttpMessage, LogLevel};
6
7/// The txn class contain all the functions relative to the http or tcp transaction.
8#[derive(Clone)]
9pub struct Txn<'lua> {
10    class: Table<'lua>,
11    pub c: Converters<'lua>,
12    pub f: Fetches<'lua>,
13    pub(crate) r#priv: Value<'lua>,
14}
15
16impl<'lua> Txn<'lua> {
17    /// Returns an HTTP class object.
18    #[inline]
19    pub fn http(&self) -> Result<Http<'lua>> {
20        self.class.get("http")
21    }
22
23    /// Returns the request HTTPMessage object.
24    pub fn http_req(&self) -> Result<HttpMessage<'lua>> {
25        self.class.get("http_req")
26    }
27
28    /// Returns the response HTTPMessage object.
29    pub fn http_res(&self) -> Result<HttpMessage<'lua>> {
30        self.class.get("http_res")
31    }
32
33    /// Sends a log on the default syslog server if it is configured and on the stderr if it is allowed.
34    #[inline]
35    pub fn log(&self, level: LogLevel, msg: impl AsRef<str>) -> Result<()> {
36        let msg = msg.as_ref();
37        self.class.call_method("log", (level, msg))
38    }
39
40    /// Sends a log line with the default loglevel for the proxy associated with the transaction.
41    #[inline]
42    pub fn deflog<S>(&self, msg: &S) -> Result<()>
43    where
44        S: AsRef<str> + ?Sized,
45    {
46        self.class.call_method("deflog", msg.as_ref())
47    }
48
49    /// Returns data stored in the current transaction (with the `set_priv()`) function.
50    #[inline]
51    pub fn get_priv<R: FromLua<'lua>>(&self) -> Result<R> {
52        self.class.call_method("get_priv", ())
53    }
54
55    /// Stores any data in the current HAProxy transaction.
56    /// This action replaces the old stored data.
57    #[inline]
58    pub fn set_priv<A: IntoLua<'lua>>(&self, val: A) -> Result<()> {
59        self.class.call_method("set_priv", val)
60    }
61
62    /// Returns data stored in the variable `name`.
63    #[inline]
64    pub fn get_var<R: FromLua<'lua>>(&self, name: &str) -> Result<R> {
65        self.class.call_method("get_var", name)
66    }
67
68    /// Store variable `name` in an HAProxy converting the type.
69    #[inline]
70    pub fn set_var<A: IntoLua<'lua>>(&self, name: &str, val: A) -> Result<()> {
71        self.class.call_method("set_var", (name, val))
72    }
73
74    /// Store variable `name` in an HAProxy if the variable already exists.
75    #[inline]
76    pub fn set_var_if_exists<A: IntoLua<'lua>>(&self, name: &str, val: A) -> Result<()> {
77        self.class.call_method("set_var", (name, val, true))
78    }
79
80    /// Unsets the variable `name`.
81    #[inline]
82    pub fn unset_var(&self, name: &str) -> Result<()> {
83        self.class.call_method("unset_var", name)
84    }
85
86    /// Changes the log level of the current request.
87    /// The `level` must be an integer between 0 and 7.
88    #[inline]
89    pub fn set_loglevel(&self, level: LogLevel) -> Result<()> {
90        self.class.call_method("set_loglevel", level)
91    }
92}
93
94impl<'lua> FromLua<'lua> for Txn<'lua> {
95    #[inline]
96    fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
97        let class = Table::from_lua(value, lua)?;
98        Ok(Txn {
99            c: class.get("c")?,
100            f: class.get("f")?,
101            class,
102            r#priv: Value::Nil,
103        })
104    }
105}
106
107impl<'lua> Deref for Txn<'lua> {
108    type Target = Table<'lua>;
109
110    #[inline]
111    fn deref(&self) -> &Self::Target {
112        &self.class
113    }
114}