Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: build: parser: Ping360 transducer return type #55

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions build/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct MessageDefinition {
description: Option<String>,
payload: Vec<Payload>,
category: MessageDefinitionCategory,
returns_message: Option<String>,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -194,12 +195,22 @@ impl MessageDefinition {
.map(|element| Payload::from_json(element))
.collect(),
category,
returns_message: value
.get("returns_message")
.and_then(|v| v.as_str().map(str::to_string)),
}
}

pub fn emit_fn(&self) -> TokenStream {
let struct_name = quote::format_ident!("{}Struct", self.name.to_case(Case::Pascal));
let pascal_message_name = ident!(self.name.to_case(Case::Pascal));
let return_struct_name = match self.returns_message {
Some(ref message) => Some(quote::format_ident!(
"{}Struct",
message.to_case(Case::Pascal)
)),
None => None,
};

let function_name = quote::format_ident!("{}", self.name.to_case(Case::Snake));
let function_description = self
Expand Down Expand Up @@ -236,6 +247,7 @@ impl MessageDefinition {

let function_token = match self.category {
MessageDefinitionCategory::Set | MessageDefinitionCategory::Control => {
let mut return_type = quote! { () };
let result = if self.category == MessageDefinitionCategory::Set {
quote! {
let receiver = self.subscribe();
Expand All @@ -245,18 +257,30 @@ impl MessageDefinition {
self.wait_for_ack(receiver, #struct_name::id()).await
}
} else {
quote! {
self.get_common().send_message(package).await?;
// For messages from Config category that returns a structure
if let Some(value) = return_struct_name {
return_type = quote! { #value };
quote! {
let receiver = self.subscribe();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be outside the if/else since both scopes use it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside a quote, can't be changed.


self.get_common().send_message(package).await?;

Ok(())
self.wait_for_message(receiver).await
}
} else {
quote! {
self.get_common().send_message(package).await?;

Ok(())
}
}
};

quote! {
#[doc = #function_description]
#[doc = "# Arguments"]
#(#function_parameters_description)*
pub async fn #function_name(&self, #(#function_parameters),*) -> Result<(), PingError> {
pub async fn #function_name(&self, #(#function_parameters),*) -> Result<#return_type, PingError> {
let request = Messages::#pascal_message_name(#struct_name {
#(#function_assignments,)*
});
Expand Down
2 changes: 1 addition & 1 deletion build/ping-protocol
Loading