diff --git a/otdrs.pyi b/otdrs.pyi index 142d7dc..557f614 100644 --- a/otdrs.pyi +++ b/otdrs.pyi @@ -1,3 +1,6 @@ +import enum + + class BlockInfo: """Details about a specific SOR block""" @@ -296,6 +299,18 @@ class SORFile: Returns an enum. You should generally treat a missing checksum as non-fatal.""" ... + def to_json(self) -> dict: + """ + Serialize SORFile class to json + """ + ... + + def to_cbor(self) -> bytes: + """ + Serialize SORFile class to CBOR + """ + ... + def parse_file(path: str) -> SORFile: """Load a SOR from the given path and parse it""" diff --git a/src/python.rs b/src/python.rs index 840de1c..7e4866e 100644 --- a/src/python.rs +++ b/src/python.rs @@ -62,6 +62,19 @@ impl SORFile { Ok(result.into()) } + #[pyo3(name = "to_json")] + fn to_json_py(&self, py: Python<'_>) -> PyResult { + let json_str = serde_json::to_string(&self) + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; + let json_module = py.import("json")?; + json_module.call_method1("loads", (json_str,))?.extract() + } + + #[pyo3(name = "to_cbor")] + fn to_cbor_py(&self) -> PyResult> { + serde_cbor::to_vec(&self).map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + } /// This module is implemented in Rust.