forked from eclipse-zenoh/zenoh-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.rs
More file actions
236 lines (215 loc) · 8.31 KB
/
bytes.rs
File metadata and controls
236 lines (215 loc) · 8.31 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::borrow::Cow;
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use std::io::Read;
use pyo3::{
exceptions::{PyTypeError, PyValueError},
prelude::*,
types::{PyByteArray, PyBytes, PyString},
};
use crate::{
macros::{downcast_or_new, wrapper},
utils::{IntoPyResult, MapInto},
};
wrapper!(zenoh::bytes::ZBytes: Clone, Default);
downcast_or_new!(ZBytes);
#[pymethods]
impl ZBytes {
#[new]
fn new(obj: Option<&Bound<PyAny>>) -> PyResult<Self> {
let Some(obj) = obj else {
return Ok(Self::default());
};
if let Ok(bytes) = obj.downcast::<PyByteArray>() {
Ok(Self(bytes.to_vec().into()))
} else if let Ok(bytes) = obj.downcast::<PyBytes>() {
Ok(Self(bytes.as_bytes().into()))
} else if let Ok(string) = obj.downcast::<PyString>() {
Ok(Self(string.to_string().into()))
} else {
Err(PyTypeError::new_err(format!(
"expected bytes/str type, found '{}'",
obj.get_type().name().unwrap()
)))
}
}
fn to_bytes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
// Not using `ZBytes::to_bytes`
PyBytes::new_bound_with(py, self.0.len(), |bytes| {
self.0.reader().read_exact(bytes).into_pyres()
})
}
fn to_string(&self) -> PyResult<Cow<str>> {
self.0
.try_to_string()
.map_err(|_| PyValueError::new_err("not an UTF8 error"))
}
fn __len__(&self) -> usize {
self.0.len()
}
fn __bool__(&self) -> bool {
!self.0.is_empty()
}
fn __bytes__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
self.to_bytes(py)
}
fn __str__(&self) -> PyResult<Cow<str>> {
self.to_string()
}
fn __eq__(&self, other: &Bound<PyAny>) -> PyResult<bool> {
Ok(self.0 == Self::from_py(other)?.0)
}
fn __hash__(&self, py: Python) -> PyResult<isize> {
self.__bytes__(py)?.hash()
}
fn __repr__(&self) -> String {
format!("{:?}", self.0)
}
}
wrapper!(zenoh::bytes::Encoding: Clone, Default);
downcast_or_new!(Encoding => Option<String>);
#[pymethods]
impl Encoding {
#[new]
fn new(s: Option<String>) -> Self {
s.map_into().map(Self).unwrap_or_default()
}
fn with_schema(&self, schema: String) -> Self {
Self(self.0.clone().with_schema(schema))
}
// Cannot use `#[pyo3(from_py_with = "...")]`, see https://github.com/PyO3/pyo3/issues/4113
fn __eq__(&self, other: &Bound<PyAny>) -> PyResult<bool> {
Ok(self.0 == Self::from_py(other)?.0)
}
fn __hash__(&self, py: Python) -> PyResult<isize> {
PyString::new_bound(py, &self.__str__()).hash()
}
fn __repr__(&self) -> String {
format!("{:?}", self.0)
}
fn __str__(&self) -> String {
format!("{}", self.0)
}
#[classattr]
const ZENOH_BYTES: Self = Self(zenoh::bytes::Encoding::ZENOH_BYTES);
#[classattr]
const ZENOH_STRING: Self = Self(zenoh::bytes::Encoding::ZENOH_STRING);
#[classattr]
const ZENOH_SERIALIZED: Self = Self(zenoh::bytes::Encoding::ZENOH_SERIALIZED);
#[classattr]
const APPLICATION_OCTET_STREAM: Self = Self(zenoh::bytes::Encoding::APPLICATION_OCTET_STREAM);
#[classattr]
const TEXT_PLAIN: Self = Self(zenoh::bytes::Encoding::TEXT_PLAIN);
#[classattr]
const APPLICATION_JSON: Self = Self(zenoh::bytes::Encoding::APPLICATION_JSON);
#[classattr]
const TEXT_JSON: Self = Self(zenoh::bytes::Encoding::TEXT_JSON);
#[classattr]
const APPLICATION_CDR: Self = Self(zenoh::bytes::Encoding::APPLICATION_CDR);
#[classattr]
const APPLICATION_CBOR: Self = Self(zenoh::bytes::Encoding::APPLICATION_CBOR);
#[classattr]
const APPLICATION_YAML: Self = Self(zenoh::bytes::Encoding::APPLICATION_YAML);
#[classattr]
const TEXT_YAML: Self = Self(zenoh::bytes::Encoding::TEXT_YAML);
#[classattr]
const TEXT_JSON5: Self = Self(zenoh::bytes::Encoding::TEXT_JSON5);
#[classattr]
const APPLICATION_PYTHON_SERIALIZED_OBJECT: Self =
Self(zenoh::bytes::Encoding::APPLICATION_PYTHON_SERIALIZED_OBJECT);
#[classattr]
const APPLICATION_PROTOBUF: Self = Self(zenoh::bytes::Encoding::APPLICATION_PROTOBUF);
#[classattr]
const APPLICATION_JAVA_SERIALIZED_OBJECT: Self =
Self(zenoh::bytes::Encoding::APPLICATION_JAVA_SERIALIZED_OBJECT);
#[classattr]
const APPLICATION_OPENMETRICS_TEXT: Self =
Self(zenoh::bytes::Encoding::APPLICATION_OPENMETRICS_TEXT);
#[classattr]
const IMAGE_PNG: Self = Self(zenoh::bytes::Encoding::IMAGE_PNG);
#[classattr]
const IMAGE_JPEG: Self = Self(zenoh::bytes::Encoding::IMAGE_JPEG);
#[classattr]
const IMAGE_GIF: Self = Self(zenoh::bytes::Encoding::IMAGE_GIF);
#[classattr]
const IMAGE_BMP: Self = Self(zenoh::bytes::Encoding::IMAGE_BMP);
#[classattr]
const IMAGE_WEBP: Self = Self(zenoh::bytes::Encoding::IMAGE_WEBP);
#[classattr]
const APPLICATION_XML: Self = Self(zenoh::bytes::Encoding::APPLICATION_XML);
#[classattr]
const APPLICATION_X_WWW_FORM_URLENCODED: Self =
Self(zenoh::bytes::Encoding::APPLICATION_X_WWW_FORM_URLENCODED);
#[classattr]
const TEXT_HTML: Self = Self(zenoh::bytes::Encoding::TEXT_HTML);
#[classattr]
const TEXT_XML: Self = Self(zenoh::bytes::Encoding::TEXT_XML);
#[classattr]
const TEXT_CSS: Self = Self(zenoh::bytes::Encoding::TEXT_CSS);
#[classattr]
const TEXT_JAVASCRIPT: Self = Self(zenoh::bytes::Encoding::TEXT_JAVASCRIPT);
#[classattr]
const TEXT_MARKDOWN: Self = Self(zenoh::bytes::Encoding::TEXT_MARKDOWN);
#[classattr]
const TEXT_CSV: Self = Self(zenoh::bytes::Encoding::TEXT_CSV);
#[classattr]
const APPLICATION_SQL: Self = Self(zenoh::bytes::Encoding::APPLICATION_SQL);
#[classattr]
const APPLICATION_COAP_PAYLOAD: Self = Self(zenoh::bytes::Encoding::APPLICATION_COAP_PAYLOAD);
#[classattr]
const APPLICATION_JSON_PATCH_JSON: Self =
Self(zenoh::bytes::Encoding::APPLICATION_JSON_PATCH_JSON);
#[classattr]
const APPLICATION_JSON_SEQ: Self = Self(zenoh::bytes::Encoding::APPLICATION_JSON_SEQ);
#[classattr]
const APPLICATION_JSONPATH: Self = Self(zenoh::bytes::Encoding::APPLICATION_JSONPATH);
#[classattr]
const APPLICATION_JWT: Self = Self(zenoh::bytes::Encoding::APPLICATION_JWT);
#[classattr]
const APPLICATION_MP4: Self = Self(zenoh::bytes::Encoding::APPLICATION_MP4);
#[classattr]
const APPLICATION_SOAP_XML: Self = Self(zenoh::bytes::Encoding::APPLICATION_SOAP_XML);
#[classattr]
const APPLICATION_YANG: Self = Self(zenoh::bytes::Encoding::APPLICATION_YANG);
#[classattr]
const AUDIO_AAC: Self = Self(zenoh::bytes::Encoding::AUDIO_AAC);
#[classattr]
const AUDIO_FLAC: Self = Self(zenoh::bytes::Encoding::AUDIO_FLAC);
#[classattr]
const AUDIO_MP4: Self = Self(zenoh::bytes::Encoding::AUDIO_MP4);
#[classattr]
const AUDIO_OGG: Self = Self(zenoh::bytes::Encoding::AUDIO_OGG);
#[classattr]
const AUDIO_VORBIS: Self = Self(zenoh::bytes::Encoding::AUDIO_VORBIS);
#[classattr]
const VIDEO_H261: Self = Self(zenoh::bytes::Encoding::VIDEO_H261);
#[classattr]
const VIDEO_H263: Self = Self(zenoh::bytes::Encoding::VIDEO_H263);
#[classattr]
const VIDEO_H264: Self = Self(zenoh::bytes::Encoding::VIDEO_H264);
#[classattr]
const VIDEO_H265: Self = Self(zenoh::bytes::Encoding::VIDEO_H265);
#[classattr]
const VIDEO_H266: Self = Self(zenoh::bytes::Encoding::VIDEO_H266);
#[classattr]
const VIDEO_MP4: Self = Self(zenoh::bytes::Encoding::VIDEO_MP4);
#[classattr]
const VIDEO_OGG: Self = Self(zenoh::bytes::Encoding::VIDEO_OGG);
#[classattr]
const VIDEO_RAW: Self = Self(zenoh::bytes::Encoding::VIDEO_RAW);
#[classattr]
const VIDEO_VP8: Self = Self(zenoh::bytes::Encoding::VIDEO_VP8);
#[classattr]
const VIDEO_VP9: Self = Self(zenoh::bytes::Encoding::VIDEO_VP9);
}