-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole_command.rs
More file actions
82 lines (70 loc) · 2.22 KB
/
console_command.rs
File metadata and controls
82 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::{FromGil, IntoGil, PyDefault, flat, flat_err_to_py};
use planus::{Builder, ReadAsRoot};
use pyo3::{prelude::*, types::*};
#[pyclass(module = "rlbot_flatbuffers", subclass, get_all)]
pub struct ConsoleCommand {
#[pyo3(set)]
pub command: Py<PyString>,
}
impl crate::PyDefault for ConsoleCommand {
fn py_default(py: Python) -> Py<Self> {
Py::new(
py,
Self {
command: crate::pydefault_string(py),
},
)
.unwrap()
}
}
impl FromGil<&flat::ConsoleCommand> for ConsoleCommand {
#[allow(unused_variables)]
fn from_gil(py: Python, flat_t: &flat::ConsoleCommand) -> Self {
ConsoleCommand {
command: PyString::new(py, &flat_t.command).unbind(),
}
}
}
impl FromGil<&ConsoleCommand> for flat::ConsoleCommand {
#[allow(unused_variables)]
fn from_gil(py: Python, py_type: &ConsoleCommand) -> Self {
Self {
command: py_type.command.to_str(py).unwrap().to_string(),
}
}
}
#[pymethods]
impl ConsoleCommand {
#[new]
#[pyo3(signature = (command=None))]
pub fn new(py: Python, command: Option<Py<PyString>>) -> Self {
Self {
command: command.unwrap_or_else(|| crate::pydefault_string(py)),
}
}
pub fn __str__(&self, py: Python) -> String {
self.__repr__(py)
}
#[allow(unused_variables)]
pub fn __repr__(&self, py: Python) -> String {
format!(
"ConsoleCommand(command={:?})",
self.command.bind(py).to_cow().unwrap(),
)
}
#[classattr]
fn __match_args__() -> (&'static str,) {
("command",)
}
fn pack<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
let mut builder = Builder::with_capacity(u16::MAX as usize);
let flat_t = flat::ConsoleCommand::from_gil(py, self);
PyBytes::new(py, builder.finish(flat_t, None))
}
#[staticmethod]
fn unpack(py: Python, data: &[u8]) -> PyResult<Py<Self>> {
let flat_t_ref = flat::ConsoleCommandRef::read_as_root(data).map_err(flat_err_to_py)?;
let flat_t = flat::ConsoleCommand::try_from(flat_t_ref).map_err(flat_err_to_py)?;
Ok(crate::into_py_from(py, &flat_t))
}
}