haproxy_api/
txn.rs

1use std::ops::Deref;
2
3use mlua::{FromLua, IntoLua, Lua, ObjectLike, Result, Table, 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 {
10    class: Table,
11    pub c: Converters,
12    pub f: Fetches,
13    pub(crate) r#priv: Value,
14}
15
16impl Txn {
17    /// Returns an HTTP class object.
18    #[inline]
19    pub fn http(&self) -> Result<Http> {
20        self.class.get("http")
21    }
22
23    /// Returns the request HTTPMessage object.
24    pub fn http_req(&self) -> Result<HttpMessage> {
25        self.class.get("http_req")
26    }
27
28    /// Returns the response HTTPMessage object.
29    pub fn http_res(&self) -> Result<HttpMessage> {
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(&self, msg: impl AsRef<str>) -> Result<()> {
43        self.class.call_method("deflog", msg.as_ref())
44    }
45
46    /// Returns data stored in the current transaction (with the `set_priv()`) function.
47    #[inline]
48    pub fn get_priv<R: FromLua>(&self) -> Result<R> {
49        self.class.call_method("get_priv", ())
50    }
51
52    /// Stores any data in the current HAProxy transaction.
53    /// This action replaces the old stored data.
54    #[inline]
55    pub fn set_priv(&self, val: impl IntoLua) -> Result<()> {
56        self.class.call_method("set_priv", val)
57    }
58
59    /// Returns data stored in the variable `name`.
60    #[inline]
61    pub fn get_var<R: FromLua>(&self, name: &str) -> Result<R> {
62        self.class.call_method("get_var", name)
63    }
64
65    /// Store variable `name` in an HAProxy converting the type.
66    #[inline]
67    pub fn set_var(&self, name: &str, val: impl IntoLua) -> Result<()> {
68        self.class.call_method("set_var", (name, val))
69    }
70
71    /// Store variable `name` in an HAProxy if the variable already exists.
72    #[inline]
73    pub fn set_var_if_exists(&self, name: &str, val: impl IntoLua) -> Result<()> {
74        self.class.call_method("set_var", (name, val, true))
75    }
76
77    /// Unsets the variable `name`.
78    #[inline]
79    pub fn unset_var(&self, name: &str) -> Result<()> {
80        self.class.call_method("unset_var", name)
81    }
82
83    /// Changes the log level of the current request.
84    /// The `level` must be an integer between 0 and 7.
85    #[inline]
86    pub fn set_loglevel(&self, level: LogLevel) -> Result<()> {
87        self.class.call_method("set_loglevel", level)
88    }
89}
90
91impl FromLua for Txn {
92    #[inline]
93    fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
94        let class = Table::from_lua(value, lua)?;
95        Ok(Txn {
96            c: class.get("c")?,
97            f: class.get("f")?,
98            class,
99            r#priv: Value::Nil,
100        })
101    }
102}
103
104impl Deref for Txn {
105    type Target = Table;
106
107    #[inline]
108    fn deref(&self) -> &Self::Target {
109        &self.class
110    }
111}