-
Notifications
You must be signed in to change notification settings - Fork 2
/
component.rs
58 lines (49 loc) · 1.32 KB
/
component.rs
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
use pyo3::prelude::*;
use keygen_rs::component::Component as KeygenRsComponent;
use crate::json::JsonValue;
#[pymodule(name = "component")]
pub fn component_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.component", m)
})?;
m.add_class::<Component>()?;
Ok(())
}
#[pyclass(frozen)]
#[derive(Debug, Clone)]
pub struct Component {
inner: KeygenRsComponent,
}
#[pymethods]
impl Component {
#[new]
fn new(id: String, fingerprint: String, name: String) -> Self {
Self {
inner: KeygenRsComponent { id, fingerprint, name }
}
}
#[getter]
fn id(&self) -> PyResult<String> {
Ok(self.inner.id.clone())
}
#[getter]
fn fingerprint(&self) -> PyResult<String> {
Ok(self.inner.fingerprint.clone())
}
#[getter]
fn name(&self) -> PyResult<String> {
Ok(self.inner.name.clone())
}
fn create_object(&self) -> PyResult<JsonValue> {
let object = KeygenRsComponent::create_object(&self.inner);
Ok(JsonValue(object))
}
}
impl From<Component> for KeygenRsComponent {
fn from(c: Component) -> KeygenRsComponent {
c.inner
}
}