| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:record_use/record_use.dart' as record_use; | |
| /// Extracts the list of native C/Rust symbol names used by the application from | |
| /// [recordedUses]. | |
| typedef SymbolsResolver = List<String> Function( | |
| record_use.Recordings recordedUses, | |
| ); | |
| /// Standard [SymbolsResolver] factories for `ffigen` and Diplomat bindings. | |
| abstract final class SymbolsResolvers { | |
| /// Resolves native symbol names for `ffigen`-generated bindings using the | |
| /// `recordUseMapping` map generated by `ffigen`. | |
| static SymbolsResolver fromRecordUseMapping( | |
| record_use.Library bindingsLibrary, | |
| Map<String, String> recordUseMapping, | |
| ) => | |
| (recordedUses) => [ | |
| for (final definition in recordedUses.calls.keys) | |
| if (definition.library == bindingsLibrary) | |
| ?recordUseMapping[definition.name], | |
| ]..sort(); | |
| /// Resolves native symbol names for bindings (such as Diplomat's `icu4x` | |
| /// generator) where `@RecordUse()` is attached to private wrapper functions | |
| /// named `<prefix><c_symbol_name>` (defaulting to `'_'`). | |
| static SymbolsResolver fromMethodPrefix( | |
| record_use.Library bindingsLibrary, { | |
| String prefix = '_', | |
| }) => | |
| (recordedUses) => | |
| recordedUses.calls.keys | |
| .where((id) => id.library == bindingsLibrary) | |
| .map((id) => id.name) | |
| .where((name) => name.startsWith(prefix)) | |
| .map((name) => name.substring(prefix.length)) | |
| .toSet() | |
| .toList() | |
| ..sort(); | |
| } |