chore: extract python_utils
and home-assistant
to their own crates
This commit is contained in:
7
python-utils/Cargo.toml
Normal file
7
python-utils/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "python-utils"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
pyo3 = { workspace = true }
|
21
python-utils/src/lib.rs
Normal file
21
python-utils/src/lib.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use pyo3::{exceptions::PyTypeError, prelude::*};
|
||||
|
||||
/// Create a GIL-independent reference
|
||||
pub fn detach<T>(bound: &Bound<T>) -> Py<T> {
|
||||
let py = bound.py();
|
||||
bound.as_unbound().clone_ref(py)
|
||||
}
|
||||
|
||||
pub fn validate_type_by_name(bound: &Bound<PyAny>, expected_type_name: &str) -> PyResult<()> {
|
||||
let py_type = bound.get_type();
|
||||
let type_name = py_type.name()?;
|
||||
let type_name = type_name.to_str()?;
|
||||
|
||||
if type_name != expected_type_name {
|
||||
let fully_qualified_type_name = py_type.fully_qualified_name()?;
|
||||
let fully_qualified_type_name = fully_qualified_type_name.to_str()?;
|
||||
return Err(PyTypeError::new_err(format!("expected an instance of {expected_type_name} but got an instance of {fully_qualified_type_name}")));
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
Reference in New Issue
Block a user