Skip to content
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
84 changes: 83 additions & 1 deletion asyncapi-rust-codegen/src/asyncapi_attrs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
//! Utilities for parsing asyncapi attributes

use syn::Attribute;
use syn::{Attribute, Path};

#[derive(Clone, Debug, PartialEq)]
pub enum ResponseTopicMeta {
Reference(Path),
Uri(String),
}

impl Default for ResponseTopicMeta {
fn default() -> Self {
Self::Uri(String::default())
}
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct MqttMessageBindingsMeta {
pub payload_format_indicator: Option<u8>,
pub correlation_data: Option<Path>,
pub content_type: Option<String>,
pub response_topic: Option<ResponseTopicMeta>,
pub binding_version: Option<String>,
}

/// AsyncAPI metadata extracted from attributes
#[derive(Debug, Default, Clone)]
Expand All @@ -13,6 +34,18 @@ pub struct AsyncApiMeta {
/// Override the message name used in `components.messages` and `asyncapi_message_names()`.
/// When absent the Rust variant/type identifier is used.
pub message_name: Option<String>,
pub mqtt: Option<MqttMessageBindingsMeta>,
}

fn parse_response_topic(expr: syn::Expr) -> Option<ResponseTopicMeta> {
match expr {
syn::Expr::Lit(expr_lit) => match expr_lit.lit {
syn::Lit::Str(s) => Some(ResponseTopicMeta::Uri(s.value())),
_ => None,
},
syn::Expr::Path(expr_path) => Some(ResponseTopicMeta::Reference(expr_path.path)),
_ => None,
}
}

/// Extract asyncapi metadata from `#[asyncapi(...)]` attributes
Expand Down Expand Up @@ -48,6 +81,36 @@ pub fn extract_asyncapi_meta(attrs: &[Attribute]) -> AsyncApiMeta {
let value = nested.value()?;
let s: syn::LitStr = value.parse()?;
meta.message_name = Some(s.value());
} else if nested.path.is_ident("mqtt") {
let mut binding = MqttMessageBindingsMeta::default();

nested.parse_nested_meta(|m| {
if m.path.is_ident("payload_format_indicator") {
let v = m.value()?;
let lit: syn::LitInt = v.parse()?;
binding.payload_format_indicator = Some(lit.base10_parse::<u8>()?);
} else if m.path.is_ident("correlation_data") {
let v = m.value()?;
let s: syn::Path = v.parse()?;
binding.correlation_data = Some(s);
} else if m.path.is_ident("content_type") {
let v = m.value()?;
let s: syn::LitStr = v.parse()?;
binding.content_type = Some(s.value());
} else if m.path.is_ident("response_topic") {
let v = m.value()?;
let expr: syn::Expr = v.parse()?;
binding.response_topic = parse_response_topic(expr);
} else if m.path.is_ident("binding_version") {
let v = m.value()?;
let s: syn::LitStr = v.parse()?;
binding.binding_version = Some(s.value());
}

Ok(())
})?;

meta.mqtt = Some(binding);
}
Ok(())
});
Expand Down Expand Up @@ -86,6 +149,25 @@ mod tests {
);
}

#[test]
fn test_extract_multiple_with_mqtt_bindings() {
let attrs: Vec<Attribute> = vec![parse_quote! {
#[asyncapi(mqtt(response_topic = "/a/b/d"))]
}];

let meta = extract_asyncapi_meta(&attrs);
assert_eq!(
meta.mqtt,
Some(MqttMessageBindingsMeta {
response_topic: Some(ResponseTopicMeta::Uri("/a/b/d".to_string())),
payload_format_indicator: None,
content_type: None,
correlation_data: None,
binding_version: None
})
)
}

#[test]
fn test_extract_content_type() {
let attrs: Vec<Attribute> = vec![parse_quote! {
Expand Down
Loading