From 6abe278aa33464465acd4c836a09bcbcae88e80e Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 14 Jul 2026 20:29:32 -0400 Subject: [PATCH] chore(home-assistant): streamline Python conversion for `UnexpectedState` --- home-assistant/src/state/unexpected_state.rs | 35 ++++---------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/home-assistant/src/state/unexpected_state.rs b/home-assistant/src/state/unexpected_state.rs index 6c6986b..51959ae 100644 --- a/home-assistant/src/state/unexpected_state.rs +++ b/home-assistant/src/state/unexpected_state.rs @@ -1,35 +1,14 @@ -use std::sync::Arc; +use std::{convert::Infallible, str::FromStr, sync::Arc}; -use pyo3::{exceptions::PyException, prelude::*}; -use snafu::{ResultExt, Snafu}; +use python_utils::{FromPyFromStr, ToStrToPy}; -#[derive(Debug, Clone, derive_more::Display)] +#[derive(Debug, Clone, derive_more::Display, FromPyFromStr, ToStrToPy)] pub struct UnexpectedState(pub Arc); -#[derive(Debug, Snafu)] -pub enum ExtractUnexpectedStateError { - /// couldn't extract the object as a string - ExtractStringError { source: PyErr }, -} +impl FromStr for UnexpectedState { + type Err = Infallible; -impl From for PyErr { - fn from(error: ExtractUnexpectedStateError) -> Self { - match &error { - ExtractUnexpectedStateError::ExtractStringError { .. } => { - PyException::new_err(error.to_string()) - } - } - } -} - -// TODO: replace with a derive(PyFromStr) (analogous to serde_with::DeserializeFromStr) once I make one -impl<'a, 'py> FromPyObject<'a, 'py> for UnexpectedState { - type Error = ExtractUnexpectedStateError; - - fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result { - let s = ob.extract::().context(ExtractStringSnafu)?; - let s = s.into(); - - Ok(UnexpectedState(s)) + fn from_str(s: &str) -> Result { + Ok(Self(s.into())) } }