Started actually using the google home trait macro

This commit is contained in:
2024-07-06 00:34:15 +02:00
parent d84ff8ec8e
commit 9aa16e3ef8
18 changed files with 94 additions and 229 deletions

View File

@@ -299,7 +299,7 @@ fn get_command_enum(traits: &Punctuated<Trait, Token![,]>) -> proc_macro2::Token
});
quote! {
#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(tag = "command", content = "params", rename_all = "camelCase")]
pub enum Command {
#(#items,)*
@@ -520,7 +520,7 @@ pub fn google_home_traits(item: TokenStream) -> TokenStream {
Some(quote! {
Command::#command_name {#(#parameters,)*} => {
if let Some(t) = self.cast() as Option<&dyn #ident> {
if let Some(t) = self.cast_mut() as Option<&mut dyn #ident> {
t.#f_name(#(#parameters,)*) #asyncness #errors;
serde_json::to_value(t.get_state().await?)?
} else {
@@ -547,7 +547,7 @@ pub fn google_home_traits(item: TokenStream) -> TokenStream {
pub trait #fulfillment: Sync + Send {
async fn sync(&self) -> Result<(Vec<Trait>, serde_json::Value), Box<dyn ::std::error::Error>>;
async fn query(&self) -> Result<serde_json::Value, Box<dyn ::std::error::Error>>;
async fn execute(&self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>>;
async fn execute(&mut self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>>;
}
#(#structs)*
@@ -575,7 +575,7 @@ pub fn google_home_traits(item: TokenStream) -> TokenStream {
Ok(state)
}
async fn execute(&self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
async fn execute(&mut self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
let value = match command {
#(#execute)*
};