1use std::ops::Deref;
2
3use mlua::{FromLua, IntoLua, Lua, ObjectLike, Result, String as LuaString, Table, Value};
4
5#[derive(Clone)]
9pub struct Channel(Table);
10
11impl Channel {
12 #[inline]
16 pub fn append(&self, data: impl AsRef<[u8]>) -> Result<isize> {
17 self.0.call_method("append", LuaString::wrap(data))
18 }
19
20 #[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 #[inline]
36 pub fn forward(&self, length: usize) -> Result<usize> {
37 self.0.call_method("forward", length)
38 }
39
40 #[inline]
42 pub fn input(&self) -> Result<usize> {
43 self.0.call_method("input", ())
44 }
45
46 #[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 #[inline]
61 pub fn is_full(&self) -> Result<bool> {
62 self.0.call_method("is_full", ())
63 }
64
65 #[inline]
67 pub fn is_resp(&self) -> Result<bool> {
68 self.0.call_method("is_resp", ())
69 }
70
71 #[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 #[inline]
86 pub fn may_recv(&self) -> Result<bool> {
87 self.0.call_method("may_recv", ())
88 }
89
90 #[inline]
92 pub fn output(&self) -> Result<usize> {
93 self.0.call_method("output", ())
94 }
95
96 #[inline]
100 pub fn prepend(&self, data: impl AsRef<[u8]>) -> Result<isize> {
101 self.0.call_method("prepend", LuaString::wrap(data))
102 }
103
104 #[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 #[inline]
120 pub fn send(&self, data: impl AsRef<[u8]>) -> Result<isize> {
121 self.0.call_method("send", LuaString::wrap(data))
122 }
123
124 #[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}