Initial google home trait macro
This commit is contained in:
parent
fb7af4a8b1
commit
d84ff8ec8e
99
Cargo.lock
generated
99
Cargo.lock
generated
|
@ -73,9 +73,9 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
|
|||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.72"
|
||||
version = "0.1.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09"
|
||||
checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -105,7 +105,7 @@ dependencies = [
|
|||
"futures",
|
||||
"google-home",
|
||||
"hostname",
|
||||
"indexmap 2.0.0",
|
||||
"indexmap 2.2.6",
|
||||
"mlua",
|
||||
"once_cell",
|
||||
"paste",
|
||||
|
@ -136,9 +136,13 @@ version = "0.1.0"
|
|||
name = "automation_macro"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"automation_cast",
|
||||
"itertools 0.12.1",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 2.0.60",
|
||||
]
|
||||
|
||||
|
@ -370,12 +374,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.16"
|
||||
version = "0.8.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
|
@ -642,7 +643,9 @@ dependencies = [
|
|||
"anyhow",
|
||||
"async-trait",
|
||||
"automation_cast",
|
||||
"automation_macro",
|
||||
"futures",
|
||||
"json_value_merge",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
|
@ -676,9 +679,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
|||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.14.0"
|
||||
version = "0.14.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
|
||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"allocator-api2",
|
||||
|
@ -871,12 +874,12 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.0.0"
|
||||
version = "2.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d"
|
||||
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown 0.14.0",
|
||||
"hashbrown 0.14.5",
|
||||
"serde",
|
||||
]
|
||||
|
||||
|
@ -919,6 +922,15 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "json_value_merge"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b0a3aadd8aaadfe2be6fd22bbdb5dbc63494ce22b7c124211f684fd757b3215"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
@ -1166,6 +1178,29 @@ version = "0.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets 0.52.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.14"
|
||||
|
@ -1332,6 +1367,15 @@ dependencies = [
|
|||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd"
|
||||
dependencies = [
|
||||
"bitflags 2.5.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.9.3"
|
||||
|
@ -1599,9 +1643,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.198"
|
||||
version = "1.0.202"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc"
|
||||
checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
@ -1618,9 +1662,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.198"
|
||||
version = "1.0.202"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9"
|
||||
checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1629,9 +1673,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.104"
|
||||
version = "1.0.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c"
|
||||
checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
|
@ -1681,7 +1725,7 @@ dependencies = [
|
|||
"chrono",
|
||||
"hex",
|
||||
"indexmap 1.9.3",
|
||||
"indexmap 2.0.0",
|
||||
"indexmap 2.2.6",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_with_macros",
|
||||
|
@ -1706,7 +1750,7 @@ version = "0.9.27"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c"
|
||||
dependencies = [
|
||||
"indexmap 2.0.0",
|
||||
"indexmap 2.2.6",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
|
@ -1722,6 +1766,15 @@ dependencies = [
|
|||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.8"
|
||||
|
@ -1890,7 +1943,9 @@ dependencies = [
|
|||
"libc",
|
||||
"mio",
|
||||
"num_cpus",
|
||||
"parking_lot",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2 0.5.3",
|
||||
"tokio-macros",
|
||||
"tracing",
|
||||
|
@ -1976,7 +2031,7 @@ dependencies = [
|
|||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-util",
|
||||
"hashbrown 0.14.0",
|
||||
"hashbrown 0.14.5",
|
||||
"pin-project-lite",
|
||||
"slab",
|
||||
"tokio",
|
||||
|
|
|
@ -7,7 +7,14 @@ edition = "2021"
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
automation_cast = { path = "../automation_cast" }
|
||||
async-trait = "0.1.80"
|
||||
itertools = "0.12.1"
|
||||
proc-macro2 = "1.0.81"
|
||||
quote = "1.0.36"
|
||||
serde = { version = "1.0.202", features = ["derive"] }
|
||||
syn = { version = "2.0.60", features = ["extra-traits", "full"] }
|
||||
serde_json = "1.0.118"
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0.202", features = ["derive"] }
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
#![feature(let_chains)]
|
||||
#![feature(iter_intersperse)]
|
||||
mod lua_device;
|
||||
mod lua_device_config;
|
||||
|
||||
use lua_device::impl_lua_device_macro;
|
||||
use lua_device_config::impl_lua_device_config_macro;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::parse::Parse;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::token::Brace;
|
||||
use syn::{
|
||||
braced, parse_macro_input, DeriveInput, GenericArgument, Ident, LitStr, Path, PathArguments,
|
||||
PathSegment, ReturnType, Signature, Token, Type, TypePath,
|
||||
};
|
||||
|
||||
#[proc_macro_derive(LuaDevice, attributes(config))]
|
||||
pub fn lua_device_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
|
@ -18,3 +28,561 @@ pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::T
|
|||
|
||||
impl_lua_device_config_macro(&ast).into()
|
||||
}
|
||||
|
||||
mod kw {
|
||||
use syn::custom_keyword;
|
||||
|
||||
custom_keyword!(required);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FieldAttribute {
|
||||
ident: Ident,
|
||||
_colon_token: Token![:],
|
||||
ty: Type,
|
||||
}
|
||||
|
||||
impl Parse for FieldAttribute {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
ident: input.parse()?,
|
||||
_colon_token: input.parse()?,
|
||||
ty: input.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FieldState {
|
||||
sign: Signature,
|
||||
}
|
||||
|
||||
impl Parse for FieldState {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
sign: input.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FieldExecute {
|
||||
name: LitStr,
|
||||
_fat_arrow_token: Token![=>],
|
||||
sign: Signature,
|
||||
}
|
||||
|
||||
impl Parse for FieldExecute {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
name: input.parse()?,
|
||||
_fat_arrow_token: input.parse()?,
|
||||
sign: input.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Field {
|
||||
Attribute(FieldAttribute),
|
||||
State(FieldState),
|
||||
Execute(FieldExecute),
|
||||
}
|
||||
|
||||
impl Parse for Field {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
if input.peek(Ident) {
|
||||
Ok(Field::Attribute(input.parse()?))
|
||||
} else if input.peek(LitStr) {
|
||||
Ok(Field::Execute(input.parse()?))
|
||||
} else {
|
||||
Ok(Field::State(input.parse()?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Trait {
|
||||
name: LitStr,
|
||||
_fat_arrow_token: Token![=>],
|
||||
_trait_token: Token![trait],
|
||||
ident: Ident,
|
||||
_brace_token: Brace,
|
||||
fields: Punctuated<Field, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for Trait {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let content;
|
||||
Ok(Self {
|
||||
name: input.parse()?,
|
||||
_fat_arrow_token: input.parse()?,
|
||||
_trait_token: input.parse()?,
|
||||
ident: input.parse()?,
|
||||
_brace_token: braced!(content in input),
|
||||
fields: content.parse_terminated(Field::parse, Token![,])?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Input {
|
||||
ty: TypePath,
|
||||
_comma: Token![,],
|
||||
traits: Punctuated<Trait, Token![,]>,
|
||||
}
|
||||
|
||||
// TODO: Error on duplicate name?
|
||||
impl Parse for Input {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
ty: input.parse()?,
|
||||
_comma: input.parse()?,
|
||||
traits: input.parse_terminated(Trait::parse, Token![,])?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_type_path(ty: &syn::Type) -> Option<&Path> {
|
||||
match *ty {
|
||||
Type::Path(ref typepath) if typepath.qself.is_none() => Some(&typepath.path),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_segment<'a>(path: &'a Path, options: &[&str]) -> Option<&'a PathSegment> {
|
||||
let idents_of_path = path
|
||||
.segments
|
||||
.iter()
|
||||
.map(|segment| segment.ident.to_string())
|
||||
.intersperse('|'.into())
|
||||
.collect::<String>();
|
||||
|
||||
options
|
||||
.iter()
|
||||
.find(|s| &idents_of_path == *s)
|
||||
.and_then(|_| path.segments.last())
|
||||
}
|
||||
|
||||
// Based on: https://stackoverflow.com/a/56264023
|
||||
fn extract_type_from_option(ty: &syn::Type) -> Option<&syn::Type> {
|
||||
extract_type_path(ty)
|
||||
.and_then(|path| {
|
||||
extract_segment(path, &["Option", "std|option|Option", "core|option|Option"])
|
||||
})
|
||||
.and_then(|path_seg| {
|
||||
let type_params = &path_seg.arguments;
|
||||
// It should have only on angle-bracketed param ("<String>"):
|
||||
match *type_params {
|
||||
PathArguments::AngleBracketed(ref params) => params.args.first(),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.and_then(|generic_arg| match *generic_arg {
|
||||
GenericArgument::Type(ref ty) => Some(ty),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_type_from_result(ty: &syn::Type) -> Option<&syn::Type> {
|
||||
extract_type_path(ty)
|
||||
.and_then(|path| {
|
||||
extract_segment(path, &["Result", "std|result|Result", "core|result|Result"])
|
||||
})
|
||||
.and_then(|path_seg| {
|
||||
let type_params = &path_seg.arguments;
|
||||
// It should have only on angle-bracketed param ("<String>"):
|
||||
match *type_params {
|
||||
PathArguments::AngleBracketed(ref params) => params.args.first(),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.and_then(|generic_arg| match *generic_arg {
|
||||
GenericArgument::Type(ref ty) => Some(ty),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_attributes_struct_ident(t: &Trait) -> Ident {
|
||||
syn::Ident::new(&format!("{}Attributes", t.ident), t.ident.span())
|
||||
}
|
||||
|
||||
fn get_attributes_struct(t: &Trait) -> proc_macro2::TokenStream {
|
||||
let fields = t.fields.iter().filter_map(|f| match f {
|
||||
Field::Attribute(attr) => {
|
||||
let ident = &attr.ident;
|
||||
let ty = &attr.ty;
|
||||
|
||||
// TODO: Extract into function
|
||||
if let Some(ty) = extract_type_from_option(ty) {
|
||||
Some(quote! {
|
||||
#[serde(skip_serializing_if = "core::option::Option::is_none")]
|
||||
#ident: ::core::option::Option<#ty>
|
||||
})
|
||||
} else {
|
||||
Some(quote! {
|
||||
#ident: #ty
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
|
||||
let name = get_attributes_struct_ident(t);
|
||||
quote! {
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct #name {
|
||||
#(#fields,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_state_struct_ident(t: &Trait) -> Ident {
|
||||
syn::Ident::new(&format!("{}State", t.ident), t.ident.span())
|
||||
}
|
||||
|
||||
fn get_state_struct(t: &Trait) -> proc_macro2::TokenStream {
|
||||
let fields = t.fields.iter().filter_map(|f| match f {
|
||||
Field::State(state) => {
|
||||
let ident = &state.sign.ident;
|
||||
|
||||
let ReturnType::Type(_, ty) = &state.sign.output else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let ty = extract_type_from_result(ty).unwrap_or(ty);
|
||||
|
||||
if let Some(ty) = extract_type_from_option(ty) {
|
||||
Some(quote! {
|
||||
#[serde(skip_serializing_if = "core::option::Option::is_none")]
|
||||
#ident: ::core::option::Option<#ty>
|
||||
})
|
||||
} else {
|
||||
Some(quote! {#ident: #ty})
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
|
||||
let name = get_state_struct_ident(t);
|
||||
quote! {
|
||||
#[derive(Debug, Default, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct #name {
|
||||
#(#fields,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_command_enum(traits: &Punctuated<Trait, Token![,]>) -> proc_macro2::TokenStream {
|
||||
let items = traits.iter().flat_map(|t| {
|
||||
t.fields.iter().filter_map(|f| match f {
|
||||
Field::Execute(execute) => {
|
||||
let name = execute.name.value();
|
||||
let ident = Ident::new(
|
||||
name.split_at(name.rfind('.').map(|v| v + 1).unwrap_or(0)).1,
|
||||
execute.name.span(),
|
||||
);
|
||||
|
||||
let parameters = execute.sign.inputs.iter().skip(1);
|
||||
|
||||
Some(quote! {
|
||||
#[serde(rename = #name, rename_all = "camelCase")]
|
||||
#ident {
|
||||
#(#parameters,)*
|
||||
}
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
|
||||
quote! {
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
#[serde(tag = "command", content = "params", rename_all = "camelCase")]
|
||||
pub enum Command {
|
||||
#(#items,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_trait_enum(traits: &Punctuated<Trait, Token![,]>) -> proc_macro2::TokenStream {
|
||||
let items = traits.iter().map(|t| {
|
||||
let name = &t.name;
|
||||
let ident = &t.ident;
|
||||
quote! {
|
||||
#[serde(rename = #name)]
|
||||
#ident
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
pub enum Trait {
|
||||
#(#items,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_trait(t: &Trait) -> proc_macro2::TokenStream {
|
||||
let fields = t.fields.iter().map(|f| match f {
|
||||
Field::Attribute(attr) => {
|
||||
let name = &attr.ident;
|
||||
let ty = &attr.ty;
|
||||
|
||||
// If the default type is marked as optional, respond None by default
|
||||
if let Some(ty) = extract_type_from_option(ty) {
|
||||
quote! {
|
||||
fn #name(&self) -> Option<#ty> {
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
fn #name(&self) -> #ty;
|
||||
}
|
||||
}
|
||||
}
|
||||
Field::State(state) => {
|
||||
let sign = &state.sign;
|
||||
|
||||
let ReturnType::Type(_, ty) = &state.sign.output else {
|
||||
todo!("Handle weird function return types");
|
||||
};
|
||||
|
||||
let inner = extract_type_from_result(ty);
|
||||
// If the default type is marked as optional, respond None by default
|
||||
if extract_type_from_option(inner.unwrap_or(ty)).is_some() {
|
||||
if inner.is_some() {
|
||||
quote! {
|
||||
#sign {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#sign {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#sign;
|
||||
}
|
||||
}
|
||||
}
|
||||
Field::Execute(execute) => {
|
||||
let sign = &execute.sign;
|
||||
quote! {
|
||||
#sign;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let ident = &t.ident;
|
||||
|
||||
let attr_ident = get_attributes_struct_ident(t);
|
||||
let attr = t.fields.iter().filter_map(|f| match f {
|
||||
Field::Attribute(attr) => {
|
||||
let name = &attr.ident;
|
||||
|
||||
Some(quote! {
|
||||
#name: self.#name()
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
|
||||
let state_ident = get_state_struct_ident(t);
|
||||
let state = t.fields.iter().filter_map(|f| match f {
|
||||
Field::State(state) => {
|
||||
let ident = &state.sign.ident;
|
||||
let f_ident = &state.sign.ident;
|
||||
|
||||
let asyncness = if state.sign.asyncness.is_some() {
|
||||
quote! {.await}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let errors = if let ReturnType::Type(_, ty) = &state.sign.output
|
||||
&& extract_type_from_result(ty).is_some()
|
||||
{
|
||||
quote! {?}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
Some(quote! {
|
||||
#ident: self.#f_ident() #asyncness #errors,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
|
||||
quote! {
|
||||
#[async_trait::async_trait]
|
||||
pub trait #ident: Sync + Send {
|
||||
#(#fields)*
|
||||
|
||||
fn get_attributes(&self) -> #attr_ident {
|
||||
#attr_ident { #(#attr,)* }
|
||||
}
|
||||
|
||||
async fn get_state(&self) -> Result<#state_ident, Box<dyn ::std::error::Error>> {
|
||||
Ok(#state_ident { #(#state)* })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn google_home_traits(item: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(item as Input);
|
||||
let traits = input.traits;
|
||||
|
||||
let structs = traits.iter().map(|t| {
|
||||
let attr = get_attributes_struct(t);
|
||||
let state = get_state_struct(t);
|
||||
let tra = get_trait(t);
|
||||
|
||||
quote! {
|
||||
#attr
|
||||
#state
|
||||
#tra
|
||||
}
|
||||
});
|
||||
|
||||
let command_enum = get_command_enum(&traits);
|
||||
let trait_enum = get_trait_enum(&traits);
|
||||
|
||||
let sync = traits.iter().map(|t| {
|
||||
let ident = &t.ident;
|
||||
|
||||
quote! {
|
||||
if let Some(t) = self.cast() as Option<&dyn #ident> {
|
||||
traits.push(Trait::#ident);
|
||||
let value = serde_json::to_value(t.get_attributes())?;
|
||||
json_value_merge::Merge::merge(&mut attrs, &value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let query = traits.iter().map(|t| {
|
||||
let ident = &t.ident;
|
||||
|
||||
quote! {
|
||||
if let Some(t) = self.cast() as Option<&dyn #ident> {
|
||||
let value = serde_json::to_value(t.get_state().await?)?;
|
||||
json_value_merge::Merge::merge(&mut state, &value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let execute = traits.iter().flat_map(|t| {
|
||||
t.fields.iter().filter_map(|f| match f {
|
||||
Field::Execute(execute) => {
|
||||
let ident = &t.ident;
|
||||
let name = execute.name.value();
|
||||
let command_name = Ident::new(
|
||||
name.split_at(name.rfind('.').map(|v| v + 1).unwrap_or(0)).1,
|
||||
execute.name.span(),
|
||||
);
|
||||
let f_name = &&execute.sign.ident;
|
||||
let parameters = execute
|
||||
.sign
|
||||
.inputs
|
||||
.iter()
|
||||
.filter_map(|p| {
|
||||
if let syn::FnArg::Typed(p) = p {
|
||||
Some(&p.pat)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let asyncness = if execute.sign.asyncness.is_some() {
|
||||
quote! {.await}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let errors = if let ReturnType::Type(_, ty) = &execute.sign.output
|
||||
&& extract_type_from_result(ty).is_some()
|
||||
{
|
||||
quote! {?}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
Some(quote! {
|
||||
Command::#command_name {#(#parameters,)*} => {
|
||||
if let Some(t) = self.cast() as Option<&dyn #ident> {
|
||||
t.#f_name(#(#parameters,)*) #asyncness #errors;
|
||||
serde_json::to_value(t.get_state().await?)?
|
||||
} else {
|
||||
todo!("Device does not support action, return proper error");
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
|
||||
let ty = input.ty;
|
||||
|
||||
let fulfillment = Ident::new(
|
||||
&format!("{}Fulfillment", ty.path.segments.last().unwrap().ident),
|
||||
ty.path.segments.last().unwrap().ident.span(),
|
||||
);
|
||||
|
||||
quote! {
|
||||
// TODO: This is always the same, so should not be part of the macro, but instead something
|
||||
// else
|
||||
#[async_trait::async_trait]
|
||||
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>>;
|
||||
}
|
||||
|
||||
#(#structs)*
|
||||
|
||||
#command_enum
|
||||
#trait_enum
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<D> #fulfillment for D where D: #ty
|
||||
{
|
||||
async fn sync(&self) -> Result<(Vec<Trait>, serde_json::Value), Box<dyn ::std::error::Error>> {
|
||||
let mut traits = Vec::new();
|
||||
let mut attrs = serde_json::Value::Null;
|
||||
|
||||
#(#sync)*
|
||||
|
||||
Ok((traits, attrs))
|
||||
}
|
||||
|
||||
async fn query(&self) -> Result<serde_json::Value, Box<dyn ::std::error::Error>> {
|
||||
let mut state = serde_json::Value::Null;
|
||||
|
||||
#(#query)*
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
async fn execute(&self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
|
||||
let value = match command {
|
||||
#(#execute)*
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
|
|
@ -7,10 +7,12 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
automation_cast = { path = "../automation_cast/" }
|
||||
automation_macro = { path = "../automation_macro/" }
|
||||
serde = { version = "1.0.149", features = ["derive"] }
|
||||
serde_json = "1.0.89"
|
||||
thiserror = "1.0.37"
|
||||
tokio = { version = "1", features = ["sync"] }
|
||||
tokio = { version = "1", features = ["sync", "full"] }
|
||||
async-trait = "0.1.61"
|
||||
futures = "0.3.25"
|
||||
anyhow = "1.0.75"
|
||||
json_value_merge = "2.0.0"
|
||||
|
|
86
google-home/src/bin/expand.rs
Normal file
86
google-home/src/bin/expand.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use std::error::Error;
|
||||
|
||||
use automation_cast::Cast;
|
||||
use automation_macro::google_home_traits;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::AvailableSpeeds;
|
||||
|
||||
trait GoogleHomeDevice: GoogleHomeDeviceFulfillment {}
|
||||
google_home_traits! {
|
||||
GoogleHomeDevice,
|
||||
"action.devices.traits.OnOff" => trait OnOff {
|
||||
command_only_on_off: Option<bool>,
|
||||
query_only_on_off: Option<bool>,
|
||||
async fn on(&self) -> Result<bool, ErrorCode>,
|
||||
"action.devices.commands.OnOff" => async fn set_on(&self, on: bool) -> Result<(), ErrorCode>,
|
||||
},
|
||||
"action.devices.traits.Scene" => trait Scene {
|
||||
scene_reversible: Option<bool>,
|
||||
|
||||
"action.devices.commands.ActivateScene" => async fn set_active(&self, activate: bool) -> Result<(), ErrorCode>,
|
||||
},
|
||||
"action.devices.traits.FanSpeed" => trait FanSpeed {
|
||||
reversible: Option<bool>,
|
||||
command_only_fan_speed: Option<bool>,
|
||||
available_fan_speeds: AvailableSpeeds,
|
||||
|
||||
fn current_fan_speed_setting(&self) -> Result<String, ErrorCode>,
|
||||
fn current_fan_speed_percent(&self) -> Result<String, ErrorCode>,
|
||||
|
||||
// TODO: Figure out some syntax for optional command?
|
||||
// Probably better to just force the user to always implement commands?
|
||||
"action.devices.commands.SetFanSpeed" => fn set_fan_speed(&self, fan_speed: String),
|
||||
},
|
||||
"action.devices.traits.HumiditySetting" => trait HumiditySetting {
|
||||
query_only_humidity_setting: Option<bool>,
|
||||
|
||||
fn humidity_ambient_percent(&self) -> Result<Option<isize>, ErrorCode>,
|
||||
}
|
||||
}
|
||||
|
||||
struct Device {}
|
||||
impl GoogleHomeDevice for Device {}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl OnOff for Device {
|
||||
fn command_only_on_off(&self) -> Option<bool> {
|
||||
Some(true)
|
||||
}
|
||||
|
||||
async fn on(&self) -> Result<bool, ErrorCode> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn set_on(&self, _on: bool) -> Result<(), ErrorCode> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl HumiditySetting for Device {
|
||||
fn query_only_humidity_setting(&self) -> Option<bool> {
|
||||
Some(true)
|
||||
}
|
||||
|
||||
fn humidity_ambient_percent(&self) -> Result<Option<isize>, ErrorCode> {
|
||||
Ok(Some(44))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let device: Box<dyn GoogleHomeDevice> = Box::new(Device {});
|
||||
|
||||
let (traits, sync) = device.sync().await?;
|
||||
let query = device.query().await?;
|
||||
|
||||
println!("{traits:?}");
|
||||
println!("{sync}");
|
||||
println!("{query}");
|
||||
|
||||
let state = device.execute(Command::OnOff { on: true }).await?;
|
||||
|
||||
println!("{state}");
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user