haproxy_api/
channel.rs

1use std::ops::Deref;
2
3use mlua::{FromLua, IntoLua, Lua, ObjectLike, Result, String as LuaString, Table, Value};
4
5/// The "Channel" class contains all functions to manipulate channels.
6///
7/// Please refer to HAProxy documentation to get more information.
8#[derive(Clone)]
9pub struct Channel(Table);
10
11impl Channel {
12    /// Copies the string string at the end of incoming data of the channel buffer.
13    ///
14    /// Returns the copied length on success or -1 if data cannot be copied.
15    #[inline]
16    pub fn append(&self, data: impl AsRef<[u8]>) -> Result<isize> {
17        self.0.call_method("append", LuaString::wrap(data))
18    }
19
20    /// Returns `length` bytes of incoming data from the channel buffer, starting at the `offset`.
21    ///
22    /// The data are not removed from the buffer.
23    #[inline]
24    pub fn data(&self, offset: Option<isize>, length: Option<isize>) -> Result<Option<LuaString>> {
25        let offset = offset.unwrap_or(0);
26        match length {
27            Some(length) => self.0.call_method("data", (offset, length)),
28            None => self.0.call_method("data", offset),
29        }
30    }
31
32    /// Forwards `length` bytes of data from the channel buffer.
33    ///
34    /// Returns the amount of data forwarded and must not be called from an action to avoid yielding.
35    #[inline]
36    pub fn forward(&self, length: usize) -> Result<usize> {
37        self.0.call_method("forward", length)
38    }
39
40    /// Returns the length of incoming data in the channel buffer.
41    #[inline]
42    pub fn input(&self) -> Result<usize> {
43        self.0.call_method("input", ())
44    }
45
46    /// Copies the `data` at the `offset` in incoming data of the channel buffer.
47    ///
48    /// Returns the copied length on success or -1 if data cannot be copied.
49    ///
50    /// By default, if no `offset` is provided, the string is copied in front of incoming data.
51    /// A positive `offset` is relative to the beginning of incoming data of the channel buffer while negative offset is relative to their end.
52    #[inline]
53    pub fn insert(&self, data: impl AsRef<[u8]>, offset: Option<isize>) -> Result<isize> {
54        let offset = offset.unwrap_or(0);
55        self.0
56            .call_method("insert", (LuaString::wrap(data), offset))
57    }
58
59    /// Returns true if the channel buffer is full.
60    #[inline]
61    pub fn is_full(&self) -> Result<bool> {
62        self.0.call_method("is_full", ())
63    }
64
65    /// Returns true if the channel is the response one.
66    #[inline]
67    pub fn is_resp(&self) -> Result<bool> {
68        self.0.call_method("is_resp", ())
69    }
70
71    /// Parses `length` bytes of incoming data of the channel buffer, starting at `offset`,
72    /// and returns the first line found, including the `\n`.
73    ///
74    /// The data are not removed from the buffer. If no line is found, all data are returned.
75    #[inline]
76    pub fn line(&self, offset: Option<isize>, length: Option<isize>) -> Result<Option<LuaString>> {
77        let offset = offset.unwrap_or(0);
78        match length {
79            Some(length) => self.0.call_method("line", (offset, length)),
80            None => self.0.call_method("line", offset),
81        }
82    }
83
84    /// Returns true if the channel may still receive data.
85    #[inline]
86    pub fn may_recv(&self) -> Result<bool> {
87        self.0.call_method("may_recv", ())
88    }
89
90    /// Returns the length of outgoing data of the channel buffer.
91    #[inline]
92    pub fn output(&self) -> Result<usize> {
93        self.0.call_method("output", ())
94    }
95
96    /// Copies the `data` in front of incoming data of the channel buffer.
97    ///
98    /// Returns the copied length on success or -1 if data cannot be copied.
99    #[inline]
100    pub fn prepend(&self, data: impl AsRef<[u8]>) -> Result<isize> {
101        self.0.call_method("prepend", LuaString::wrap(data))
102    }
103
104    /// Removes `length` bytes of incoming data of the channel buffer, starting at `offset`.
105    ///
106    /// Returns number of bytes removed on success.
107    #[inline]
108    pub fn remove(&self, offset: Option<isize>, length: Option<usize>) -> Result<isize> {
109        let offset = offset.unwrap_or(0);
110        match length {
111            Some(length) => self.0.call_method("remove", (offset, length)),
112            None => self.0.call_method("remove", offset),
113        }
114    }
115
116    /// Requires immediate send of the `data`.
117    ///
118    /// It means the `data` is copied at the beginning of incoming data of the channel buffer and immediately forwarded.
119    #[inline]
120    pub fn send(&self, data: impl AsRef<[u8]>) -> Result<isize> {
121        self.0.call_method("send", LuaString::wrap(data))
122    }
123
124    /// Replaces `length` bytes of incoming data of the channel buffer, starting at `offset`, by the new `data`.
125    ///
126    /// Returns the copied length on success or -1 if data cannot be copied.
127    #[inline]
128    pub fn set(
129        &self,
130        data: impl AsRef<[u8]>,
131        offset: Option<isize>,
132        length: Option<usize>,
133    ) -> Result<isize> {
134        let data = LuaString::wrap(data);
135        let offset = offset.unwrap_or(0);
136        match length {
137            Some(length) => self.0.call_method("set", (data, offset, length)),
138            None => self.0.call_method("set", (data, offset)),
139        }
140    }
141}
142
143impl FromLua for Channel {
144    #[inline]
145    fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
146        let class = Table::from_lua(value, lua)?;
147        Ok(Channel(class))
148    }
149}
150
151impl IntoLua for Channel {
152    #[inline]
153    fn into_lua(self, _: &Lua) -> Result<Value> {
154        Ok(Value::Table(self.0))
155    }
156}
157
158impl Deref for Channel {
159    type Target = Table;
160
161    #[inline]
162    fn deref(&self) -> &Self::Target {
163        &self.0
164    }
165}