chore: extract arbitrary into its own crate arbitrary-value

This commit is contained in:
2025-04-21 16:40:27 -04:00
parent 325cb60aa1
commit ea7e9e3c53
8 changed files with 45 additions and 58 deletions

View File

@@ -1,45 +1,16 @@
[package] [workspace]
name = "smart-home-in-rust-with-home-assistant" members = [
version = "0.2.0" "arbitrary-value",
edition = "2021" resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [workspace.dependencies]
[lib] backoff = "0.4.0"
name = "smart_home_in_rust_with_home_assistant"
crate-type = ["cdylib"]
[dependencies]
chrono = "0.4.40" chrono = "0.4.40"
chrono-tz = "0.10.1" chrono-tz = "0.10.1"
derive_more = { version = "2.0.1", features = [ deranged = "0.4.1"
"display", derive_more = "2.0.1"
"from",
"into",
"try_from",
"try_into",
] }
ijson = "0.1.4"
itertools = "0.14.0"
pyo3 = { version = "0.24.0", features = [
"auto-initialize",
"chrono",
"chrono-tz",
] }
pyo3-async-runtimes = { version = "0.24.0", features = ["tokio-runtime"] }
serde_json = "1.0.140"
shadow-rs = { version = "1.0.1", default-features = false }
snafu = "0.8.5" snafu = "0.8.5"
strum = { version = "0.27.1", features = ["derive"] } tokio = "1.32.0"
tokio = { version = "1.32.0", features = [ pyo3 = "0.24.0"
"rt", pyo3-async-runtimes = "0.24.0"
"rt-multi-thread",
"sync",
"time",
] }
tracing = "0.1.37" tracing = "0.1.37"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.17"
ulid = "1.2.0"
[build-dependencies]
shadow-rs = "1.0.1"

View File

@@ -0,0 +1,17 @@
[package]
name = "arbitrary-value"
version = "0.1.0"
edition = "2021"
[features]
pyo3 = ["dep:pyo3"]
[dependencies]
chrono = { workspace = true }
chrono-tz = { workspace = true }
derive_more = { workspace = true }
ijson = "0.1.4"
itertools = "0.14.0"
snafu = { workspace = true }
pyo3 = { workspace = true, optional = true, features = ["chrono", "chrono-tz"] }

View File

@@ -1,6 +1,7 @@
use chrono::DateTime; use chrono::DateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use ijson::{IArray, INumber, IObject, IString, IValue}; use ijson::{IArray, INumber, IObject, IString, IValue};
#[cfg(feature = "pyo3")]
use pyo3::{ use pyo3::{
exceptions::{PyTypeError, PyValueError}, exceptions::{PyTypeError, PyValueError},
prelude::*, prelude::*,
@@ -71,6 +72,7 @@ impl From<Arbitrary> for IValue {
} }
} }
#[cfg(feature = "pyo3")]
impl<'py> FromPyObject<'py> for Arbitrary { impl<'py> FromPyObject<'py> for Arbitrary {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
if let Ok(map_key) = ob.extract::<MapKey>() { if let Ok(map_key) = ob.extract::<MapKey>() {
@@ -91,6 +93,7 @@ impl<'py> FromPyObject<'py> for Arbitrary {
} }
} }
#[cfg(feature = "pyo3")]
impl<'py> IntoPyObject<'py> for Arbitrary { impl<'py> IntoPyObject<'py> for Arbitrary {
type Target = PyAny; type Target = PyAny;

View File

@@ -1,7 +1,7 @@
use pyo3::IntoPyObject;
use snafu::Snafu; use snafu::Snafu;
#[derive(Debug, Clone, derive_more::Into, IntoPyObject)] #[cfg_attr(feature = "pyo3", derive(pyo3::IntoPyObject))]
#[derive(Debug, Clone, derive_more::Into)]
pub struct FiniteF64(f64); pub struct FiniteF64(f64);
#[derive(Debug, Snafu)] #[derive(Debug, Snafu)]

View File

@@ -2,3 +2,5 @@ pub mod arbitrary;
pub mod finite_f64; pub mod finite_f64;
pub mod map; pub mod map;
pub mod map_key; pub mod map_key;
pub use arbitrary::*;

View File

@@ -0,0 +1,7 @@
use std::collections::BTreeMap;
use super::{arbitrary::Arbitrary, map_key::MapKey};
#[cfg_attr(feature = "pyo3", derive(pyo3::FromPyObject, pyo3::IntoPyObject))]
#[derive(Debug, Clone, Default, derive_more::From, derive_more::Into)]
pub struct Map(pub BTreeMap<MapKey, Arbitrary>);

View File

@@ -4,6 +4,7 @@ use chrono::DateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use ijson::IString; use ijson::IString;
use itertools::Itertools; use itertools::Itertools;
#[cfg(feature = "pyo3")]
use pyo3::{ use pyo3::{
exceptions::PyTypeError, exceptions::PyTypeError,
prelude::*, prelude::*,
@@ -41,6 +42,7 @@ impl Display for MapKey {
} }
} }
#[cfg(feature = "pyo3")]
impl<'py> FromPyObject<'py> for MapKey { impl<'py> FromPyObject<'py> for MapKey {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
if let Ok(_none) = ob.downcast::<PyNone>() { if let Ok(_none) = ob.downcast::<PyNone>() {
@@ -62,6 +64,7 @@ impl<'py> FromPyObject<'py> for MapKey {
} }
} }
#[cfg(feature = "pyo3")]
impl<'py> IntoPyObject<'py> for MapKey { impl<'py> IntoPyObject<'py> for MapKey {
type Target = PyAny; type Target = PyAny;

View File

@@ -1,16 +0,0 @@
use std::collections::BTreeMap;
use pyo3::prelude::*;
use super::{arbitrary::Arbitrary, map_key::MapKey};
#[derive(Debug, Clone, Default, derive_more::From, derive_more::Into, IntoPyObject)]
pub struct Map(pub BTreeMap<MapKey, Arbitrary>);
impl<'py> FromPyObject<'py> for Map {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let inner = ob.extract()?;
Ok(Self(inner))
}
}