@ioka-technologies/asyncapi-rust-client-template 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Template-specific helper functions for Rust client template
3
+ *
4
+ * Most common utilities have been moved to the shared @common package.
5
+ * This file now only contains template-specific functions that are unique
6
+ * to the rust-client template.
7
+ */
8
+
9
+ // Import common utilities for use in local functions
10
+ import {
11
+ toRustIdentifier,
12
+ toRustTypeName,
13
+ toRustFieldName,
14
+ toRustEnumVariant,
15
+ toRustEnumVariantWithSerde,
16
+ getMessageTypeName,
17
+ getMessageRustTypeName,
18
+ getPayloadRustTypeName,
19
+ getNatsSubject,
20
+ isDynamicChannel,
21
+ extractChannelVariables,
22
+ getChannelParameters,
23
+ resolveChannelAddress,
24
+ channelHasParameters,
25
+ toPascalCase,
26
+ toKebabCase,
27
+ toSnakeCase,
28
+ isTemplateVariable,
29
+ extractAsyncApiInfo,
30
+ resolveTemplateParameters
31
+ } from '../../../common/src/index.js';
32
+
33
+ // Re-export common utilities for backward compatibility
34
+ export {
35
+ toRustIdentifier,
36
+ toRustTypeName,
37
+ toRustFieldName,
38
+ toRustEnumVariant,
39
+ toRustEnumVariantWithSerde,
40
+ getMessageTypeName,
41
+ getMessageRustTypeName,
42
+ getPayloadRustTypeName,
43
+ getNatsSubject,
44
+ isDynamicChannel,
45
+ extractChannelVariables,
46
+ getChannelParameters,
47
+ resolveChannelAddress,
48
+ channelHasParameters,
49
+ toPascalCase,
50
+ toKebabCase,
51
+ toSnakeCase,
52
+ isTemplateVariable,
53
+ extractAsyncApiInfo,
54
+ resolveTemplateParameters
55
+ };
56
+
57
+ /**
58
+ * Analyzes operations to determine client method patterns
59
+ * For NATS clients, we need to distinguish between:
60
+ * - Request/Reply operations (using NATS request/reply)
61
+ * - Publish operations (fire-and-forget)
62
+ * - Subscribe operations (message handlers)
63
+ *
64
+ * This function is specific to the rust-client template's operation analysis needs.
65
+ *
66
+ * @param {Array} operations - Array of AsyncAPI operations
67
+ * @returns {Array} Array of client method patterns
68
+ */
69
+ export function analyzeClientOperations(operations) {
70
+ const patterns = [];
71
+
72
+ for (const operation of operations) {
73
+ const operationName = operation.id();
74
+ const action = operation.action();
75
+ const messages = operation.messages();
76
+
77
+ if (action === 'send') {
78
+ // Client sends messages - this becomes a client method
79
+ if (operation.reply && operation.reply()) {
80
+ // Request/Reply pattern
81
+ patterns.push({
82
+ type: 'request_reply',
83
+ operation,
84
+ operationName,
85
+ methodName: toRustFieldName(operationName),
86
+ requestMessage: messages[0],
87
+ responseMessage: operation.reply().messages()[0],
88
+ requestType: getPayloadRustTypeName(messages[0]),
89
+ responseType: getPayloadRustTypeName(operation.reply().messages()[0])
90
+ });
91
+ } else {
92
+ // Publish pattern (fire-and-forget)
93
+ patterns.push({
94
+ type: 'publish',
95
+ operation,
96
+ operationName,
97
+ methodName: toRustFieldName(operationName),
98
+ message: messages[0],
99
+ payloadType: getPayloadRustTypeName(messages[0])
100
+ });
101
+ }
102
+ } else if (action === 'receive') {
103
+ // Client receives messages - this becomes a subscription method
104
+ patterns.push({
105
+ type: 'subscribe',
106
+ operation,
107
+ operationName,
108
+ methodName: toRustFieldName(operationName.replace(/^receive/, 'subscribe_to_')),
109
+ message: messages[0],
110
+ payloadType: getPayloadRustTypeName(messages[0])
111
+ });
112
+ }
113
+ }
114
+
115
+ return patterns;
116
+ }
@@ -5,32 +5,20 @@ import {
5
5
  toRustTypeName,
6
6
  toRustFieldName,
7
7
  getPayloadRustTypeName,
8
- analyzeClientOperations,
9
8
  getNatsSubject,
10
9
  isDynamicChannel,
11
10
  extractChannelVariables,
12
11
  getChannelParameters,
13
12
  resolveChannelAddress,
14
- channelHasParameters
13
+ channelHasParameters,
14
+ toPascalCase,
15
+ isTemplateVariable
15
16
  } from '../helpers/index.js';
16
17
 
17
18
  export default function ClientRs({ asyncapi, params }) {
18
19
  const info = asyncapi.info();
19
20
  const title = info.title();
20
21
 
21
- // Helper function to check if a parameter contains unresolved template variables
22
- function isTemplateVariable(value) {
23
- return typeof value === 'string' && value.includes('{{') && value.includes('}}');
24
- }
25
-
26
- // Helper function to convert title to PascalCase for struct names
27
- function toPascalCase(str) {
28
- return str.replace(/[^a-zA-Z0-9]/g, ' ')
29
- .split(' ')
30
- .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
31
- .join('');
32
- }
33
-
34
22
  // Resolve clientName parameter, falling back to extracted values if parameter contains template variables
35
23
  const clientName = (params.clientName && !isTemplateVariable(params.clientName))
36
24
  ? params.clientName
@@ -1,22 +1,18 @@
1
1
  /* eslint-disable no-unused-vars */
2
2
  import { File } from '@asyncapi/generator-react-sdk';
3
+ import {
4
+ toPascalCase,
5
+ isTemplateVariable,
6
+ extractAsyncApiInfo,
7
+ resolveTemplateParameters
8
+ } from '../helpers/index.js';
3
9
 
4
10
  module.exports = function ({ asyncapi, params }) {
5
- // Helper function to convert title to PascalCase
6
- function toPascalCase(str) {
7
- return str.replace(/[^a-zA-Z0-9]/g, ' ')
8
- .split(' ')
9
- .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
10
- .join('');
11
- }
12
-
13
- const title = asyncapi.info().title();
14
- const description = asyncapi.info().description();
15
- const version = asyncapi.info().version();
11
+ const asyncApiInfo = extractAsyncApiInfo(asyncapi);
12
+ const { title, version, description } = asyncApiInfo;
16
13
 
17
- const clientName = (params.clientName && !params.clientName.includes('{{'))
18
- ? params.clientName
19
- : `${toPascalCase(title)}Client`;
14
+ const resolvedParams = resolveTemplateParameters(params, asyncApiInfo);
15
+ const { clientName } = resolvedParams;
20
16
 
21
17
  // Helper function to format description for Rust doc comments
22
18
  function formatDescription(desc) {