assertSupportedDocumentVersion function

DocumentVersionReport assertSupportedDocumentVersion(
  1. dynamic version
)

Implementation

DocumentVersionReport assertSupportedDocumentVersion(dynamic version) {
  if (version is! String) {
    throw DocumentVersionException(
      code: UidlErrorCodes.malformedVersion,
      message: 'Document version "$version" is malformed; expected major.minor like "$uidlSpecVersion"',
      rawVersion: version?.toString() ?? 'null',
    );
  }

  final match = documentVersionPattern.firstMatch(version);
  if (match == null) {
    throw DocumentVersionException(
      code: UidlErrorCodes.malformedVersion,
      message: 'Document version "$version" is malformed; expected major.minor like "$uidlSpecVersion"',
      rawVersion: version,
    );
  }

  final major = int.tryParse(match.group(1) ?? '');
  final minor = int.tryParse(match.group(2) ?? '');
  final supportedMajor = int.parse(uidlSpecVersion.split('.').first);

  if (major == null || major != supportedMajor) {
    throw DocumentVersionException(
      code: UidlErrorCodes.unsupportedVersion,
      message: 'Document version "$version" has major $major; this runtime implements UIDL spec $uidlSpecVersion',
      rawVersion: version,
      major: major,
      minor: minor,
    );
  }

  return DocumentVersionReport(
    isSupported: true,
    major: major,
    minor: minor,
    raw: version,
  );
}