feat: implement StringLiteral macro
This commit is contained in:
@@ -4,39 +4,14 @@ use std::str::FromStr;
|
||||
use pyo3::FromPyObject;
|
||||
use python_utils::{FromPyFromStr, ToStrToPy};
|
||||
use snafu::Snafu;
|
||||
use string_literal::StringLiteral;
|
||||
|
||||
use crate::{entity_id::EntityId, state_object::StateObject};
|
||||
|
||||
// TODO: replace with a derive(PyFromStrLiteral) / #[literal = "state_changed"] once I learn how to make something like that and see about serde or strum integration or inspiration
|
||||
#[derive(Debug, Clone, FromPyFromStr, ToStrToPy)]
|
||||
#[derive(Debug, Clone, StringLiteral, FromPyFromStr, ToStrToPy)]
|
||||
#[string_literal(value = "state_changed")]
|
||||
pub struct Type;
|
||||
|
||||
/// expected a string of value "state_changed", but got {actual}
|
||||
#[derive(Debug, Snafu)]
|
||||
pub struct ParseTypeError {
|
||||
actual: String,
|
||||
}
|
||||
|
||||
impl FromStr for Type {
|
||||
type Err = ParseTypeError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s == "state_changed" {
|
||||
Ok(Self)
|
||||
} else {
|
||||
Err(ParseTypeError {
|
||||
actual: s.to_owned(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Type {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "state_changed")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, FromPyObject)]
|
||||
#[pyo3(from_item_all)]
|
||||
pub struct Data<
|
||||
|
||||
Reference in New Issue
Block a user