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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use pyo3::{exceptions::PyTypeError, prelude::*, types::*};
use ricq::msg::PushElem;
use ricq_core::msg::elem;
#[derive(FromPyObject)]
#[non_exhaustive]
pub enum Element {
#[doc(hidden)]
At(At),
#[doc(hidden)]
Face(Face),
}
impl PushElem for Element {
fn push_to(elem: Self, vec: &mut Vec<ricq::msg::MessageElem>) {
match elem {
Element::At(at) => PushElem::push_to(elem::At::from(at), vec),
Element::Face(face) => PushElem::push_to(elem::Face::from(face), vec),
}
}
}
#[doc(hidden)]
#[derive(FromPyObject)]
pub enum ElementOrText {
Element(Element),
Text(String),
}
impl PushElem for ElementOrText {
fn push_to(elem: Self, vec: &mut Vec<ricq::msg::MessageElem>) {
match elem {
ElementOrText::Element(elem) => PushElem::push_to(elem, vec),
ElementOrText::Text(text) => PushElem::push_to(elem::Text::new(text), vec),
}
}
}
#[pyclass(subclass)]
pub struct ElementBase {}
#[pymethods]
impl ElementBase {
#[new]
fn new() -> PyResult<Self> {
Err(PyTypeError::new_err("ElementBase is abstract"))
}
}
#[pyclass]
#[derive(Clone)]
pub struct At {
#[pyo3(get)]
pub target: i64,
}
#[pymethods]
impl At {
#[new]
pub fn new(target: i64) -> Self {
Self { target }
}
}
impl From<At> for elem::At {
fn from(at: At) -> Self {
Self::new(at.target)
}
}
#[pyclass]
#[derive(Clone)]
pub struct Face {
elem: elem::Face,
}
#[pymethods]
impl Face {
#[new]
#[args(args = "*", kwargs = "**")]
pub fn new(args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult<Self> {
match args.len() {
0 => {
let id = kwargs
.and_then(|kwargs| kwargs.get_item("id"))
.map(|id| id.extract())
.transpose()?;
if let Some(id) = id {
return Ok(Self {
elem: elem::Face::new(id),
});
}
let name = kwargs
.and_then(|kwargs| kwargs.get_item("name"))
.map(|name| name.extract())
.transpose()?;
if let Some(name) = name {
let elem = elem::Face::new_from_name(name)
.ok_or_else(|| PyTypeError::new_err("invalid face name"))?;
return Ok(Self { elem });
}
Err(PyTypeError::new_err(
"missing required argument 'id' or 'name'",
))
}
1 => {
let id_or_name = args.get_item(0)?;
if id_or_name.is_instance_of::<PyString>()? {
let name = id_or_name.extract()?;
let elem = elem::Face::new_from_name(name)
.ok_or_else(|| PyTypeError::new_err("invalid face name"))?;
Ok(Self { elem })
} else {
let id = id_or_name.extract()?;
Ok(Self {
elem: elem::Face::new(id),
})
}
}
_ => Err(PyTypeError::new_err("expected at most 1 arguments")),
}
}
#[getter]
pub fn id(&self) -> i32 {
self.elem.index
}
#[getter]
pub fn name(&self) -> String {
self.elem.name.to_owned()
}
}
impl From<Face> for elem::Face {
fn from(face: Face) -> Self {
face.elem
}
}