Skip to content

Commit

Permalink
Add RUST-ATTRIBUTE to generate derive
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Jul 21, 2024
1 parent 9edc426 commit 69c7706
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum MarkType {
dartSignalBinary,
rustSignal,
rustSignalBinary,
rustAttribute,
}

class MarkedMessage {
Expand Down Expand Up @@ -47,6 +48,12 @@ Future<void> generateMessageCode({
resourcesInFolders,
);

// Analyze marked messages in `.proto` files.
final markedMessagesAll = await analyzeMarkedMessages(
protoPath,
resourcesInFolders,
);

// Include `package` statement in `.proto` files.
// Package name should be the same as the filename
// because Rust filenames are written with package name
Expand Down Expand Up @@ -115,6 +122,12 @@ Future<void> generateMessageCode({
'--prost_out=$rustFullPath',
...(messageConfig.rustSerde ? ['--prost-serde_out=$rustFullPath'] : []),
...resourceNames.map((name) => '$name.proto'),
...markedMessagesAll.values.fold<List<String>>([], (args, messages) {
messages.values.forEach((messages) => args.addAll(messages
.where((message) => message.markType == MarkType.rustAttribute)
.map((message) => message.name)));
return args;
})
]);
if (protocRustResult.exitCode != 0) {
print(protocRustResult.stderr.toString().trim());
Expand Down Expand Up @@ -219,12 +232,6 @@ Future<void> generateMessageCode({
}
}

// Analyze marked messages in `.proto` files.
final markedMessagesAll = await analyzeMarkedMessages(
protoPath,
resourcesInFolders,
);

// Prepare communication channels between Dart and Rust.
for (final entry in markedMessagesAll.entries) {
final subPath = entry.key;
Expand Down Expand Up @@ -761,6 +768,8 @@ Future<Map<String, Map<String, List<MarkedMessage>>>> analyzeMarkedMessages(
);
final content = await protoFile.readAsString();
final regExp = RegExp(r'{[^}]*}');
final attrExp = RegExp(r"(?<=\[RINF:RUST-ATTRIBUTE\().*(?=\)\])");

// Remove all { ... } blocks from the string
final contentWithoutBlocks = content.replaceAll(regExp, ';');
final statements = contentWithoutBlocks.split(";");
Expand Down Expand Up @@ -790,6 +799,17 @@ Future<Map<String, Map<String, List<MarkedMessage>>>> analyzeMarkedMessages(
} else if (statement.contains("[RINF:RUST-SIGNAL-BINARY]")) {
markType = MarkType.rustSignalBinary;
}

var attr = attrExp.stringMatch(statement);
if (attr != null) {
markedMessages[subPath]![filename]!.add(MarkedMessage(
MarkType.rustAttribute,
"--prost_opt=type_attribute=$filename.$messageName=$attr",
-1,
));
continue;
}

if (markType == null) {
// If there's no mark in the message, just ignore it
continue;
Expand Down

0 comments on commit 69c7706

Please sign in to comment.