50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
use std::{convert::Infallible, str::FromStr};
|
|
|
|
use python_utils::{FromPyFromStr, ToStrToPy};
|
|
|
|
pub mod error_state;
|
|
pub mod unexpected_state;
|
|
|
|
pub use error_state::ErrorState;
|
|
pub use unexpected_state::UnexpectedState;
|
|
|
|
#[derive(Debug, Clone, derive_more::Display, FromPyFromStr, ToStrToPy)]
|
|
pub enum HomeAssistantState<State> {
|
|
Ok(State),
|
|
Err(ErrorState),
|
|
Unexpected(UnexpectedState),
|
|
}
|
|
|
|
impl<State> From<String> for HomeAssistantState<State>
|
|
where
|
|
State: FromStr,
|
|
{
|
|
fn from(s: String) -> Self {
|
|
if let Ok(ok) = State::from_str(&s) {
|
|
return HomeAssistantState::Ok(ok);
|
|
}
|
|
|
|
if let Ok(error) = ErrorState::from_str(&s) {
|
|
return HomeAssistantState::Err(error);
|
|
}
|
|
|
|
HomeAssistantState::Unexpected(UnexpectedState(s.into()))
|
|
}
|
|
}
|
|
|
|
impl<State: FromStr> FromStr for HomeAssistantState<State> {
|
|
type Err = Infallible;
|
|
|
|
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
|
|
if let Ok(ok) = State::from_str(s) {
|
|
return Ok(HomeAssistantState::Ok(ok));
|
|
}
|
|
|
|
if let Ok(error) = ErrorState::from_str(s) {
|
|
return Ok(HomeAssistantState::Err(error));
|
|
}
|
|
|
|
Ok(HomeAssistantState::Unexpected(UnexpectedState(s.into())))
|
|
}
|
|
}
|