encodeValue static method

Uint8List encodeValue(
  1. Protocol protocol,
  2. String value
)

Encodes a protocol value to bytes

Implementation

static Uint8List encodeValue(Protocol protocol, String value) {
  // Validate the value before encoding
  MultiAddrValidator.validateValue(protocol, value);

  switch (protocol.name) {
    case 'ip4':
      return _encodeIP4(value);
    case 'ip6':
      return _encodeIP6(value);
    case 'tcp':
    case 'udp':
      return _encodePort(value);
    case 'unix':
      return _encodePath(value);
    default:
      if (protocol.size == 0) { // Protocol has no value component
        return Uint8List(0);
      }
      if (protocol.isVariableSize) { // Protocol has a variable-length string value
        return _encodeString(value);
      }
      // Protocol has a fixed, non-zero size but is not handled by a specific case
      throw ArgumentError('Unsupported protocol for encoding: ${protocol.name}');
  }
}