Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions otdrs.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import enum


class BlockInfo:
"""Details about a specific SOR block"""

Expand Down Expand Up @@ -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"""

Expand Down
13 changes: 13 additions & 0 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ impl SORFile {
Ok(result.into())
}

#[pyo3(name = "to_json")]
fn to_json_py(&self, py: Python<'_>) -> PyResult<PyObject> {
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<Vec<u8>> {
serde_cbor::to_vec(&self).map_err(|e| PyRuntimeError::new_err(e.to_string()))
}

}

/// This module is implemented in Rust.
Expand Down