rmlToRevFiles function

RmlProjectCompileResult rmlToRevFiles(
  1. String rml,
  2. List<RmlFileInput> files
)

Compiles rml against files: text files are served as they are, and a file with null bytes is present but compiles without contents. A file= the list does not hold is an error naming the asset and path. When markupAvailable is false, it returns a failure without calling native.

Implementation

RmlProjectCompileResult rmlToRevFiles(String rml, List<RmlFileInput> files) {
  if (!markupAvailable) {
    return const RmlProjectCompileResult.failure([
      RmlCompileError(line: 0, message: 'markup is not available'),
    ]);
  }
  if (rml.isEmpty) {
    return const RmlProjectCompileResult.failure([
      RmlCompileError(line: 0, message: 'no markup to compile'),
    ]);
  }
  final rmlBytes = utf8.encode(rml);
  final bundle = encodeRmlFileInputs(files);
  final source = calloc<Uint8>(rmlBytes.length);
  final filesPtr = calloc<Uint8>(bundle.length);
  final response = calloc<_RmlProjectCompileResponseStruct>();
  try {
    source.asTypedList(rmlBytes.length).setAll(0, rmlBytes);
    filesPtr.asTypedList(bundle.length).setAll(0, bundle);
    _rmlToRevFiles(source, rmlBytes.length, filesPtr, bundle.length, response);
    final references =
        response.ref.refs == nullptr || response.ref.refsSize <= 0
        ? const <RmlFileReference>[]
        : RmlFileReference.listFromJson(
            utf8.decode(
              response.ref.refs.asTypedList(response.ref.refsSize),
              allowMalformed: true,
            ),
          );
    if (response.ref.data != nullptr && response.ref.size > 0) {
      // A view over native memory; copy it out before freeing.
      return RmlProjectCompileResult.success(
        Uint8List.fromList(response.ref.data.asTypedList(response.ref.size)),
        fileReferences: references,
      );
    }
    if (response.ref.error != nullptr && response.ref.errorSize > 0) {
      return RmlProjectCompileResult.failure(
        RmlCompileError.listFromJson(
          utf8.decode(
            response.ref.error.asTypedList(response.ref.errorSize),
            allowMalformed: true,
          ),
        ),
        fileReferences: references,
      );
    }
    return RmlProjectCompileResult.failure(const [
      RmlCompileError(line: 0, message: 'markup compile failed'),
    ], fileReferences: references);
  } finally {
    _freeRmlProjectCompile(response);
    calloc.free(response);
    calloc.free(source);
    calloc.free(filesPtr);
  }
}