
---@alias mqtt_read_func fun(size:integer):string|false, string?

---@class mqtt_packet*
---@field type integer MQTT Package type.
---@field packet_id? integer Packet Identifier。
---@field properties? table MQTT v5 property.
---@field user_properties? table MQTT v5 User attributes.

---@class mqtt_combined_packet*
local mqtt_combined_packet = {}

--- Returns the combined packet length.
---@return integer length
function mqtt_combined_packet:len() end

--- Append package fragment.
---@param part string|mqtt_combined_packet*
function mqtt_combined_packet:append(part) end

---@class mqtt_protocol*
---@field max_variable_length integer Maximum value for variable length fields.
---@field packet_type table<string|integer, integer|string> MQTT Control package type mapping.
---@field connack_rc table<integer, string> CONNACK Reason code description.
---@field packet_mt table Generic package metatable.
---@field connack_packet_mt table CONNACK Package metatable.
---@type mqtt_protocol*
local M = {}

M.max_variable_length = 268435455
M.packet_type = nil
M.connack_rc = nil
M.packet_mt = nil
M.connack_packet_mt = nil

--- encoding uint8.
---@param value integer
---@return string bytes
function M.make_uint8(value) end

--- encoding uint16.
---@param value integer
---@return string bytes
function M.make_uint16(value) end

--- encoding uint32.
---@param value integer
---@return string bytes
function M.make_uint32(value) end

--- Encode MQTT UTF-8 string.
---@param value string
---@return string bytes
function M.make_string(value) end

--- Encode MQTT variable-length integers.
---@param value integer
---@return integer ... bytes
function M.make_var_length(value) end

--- The encoding only allows uint8 with 0 or 1.
---@param value integer
---@return string bytes
function M.make_uint8_0_or_1(value) end

--- Encodes non-zero uint16.
---@param value integer
---@return string bytes
function M.make_uint16_nonzero(value) end

--- Encodes a non-zero variable-length integer.
---@param value integer
---@return integer ... bytes
function M.make_var_length_nonzero(value) end

--- Parse MQTT UTF-8 strings.
---@param read_func mqtt_read_func
---@return string|false value
---@return string? errMsg
function M.parse_string(read_func) end

--- Parse uint8.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_uint8(read_func) end

--- Parsing only allows uint8s of 0 or 1.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_uint8_0_or_1(read_func) end

--- Parse uint16.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_uint16(read_func) end

--- Parse non-zero uint16.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_uint16_nonzero(read_func) end

--- Parse uint32.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_uint32(read_func) end

--- Parse MQTT variable length integers.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_var_length(read_func) end

--- Parses non-zero variable-length integers.
---@param read_func mqtt_read_func
---@return integer|false value
---@return string? errMsg
function M.parse_var_length_nonzero(read_func) end

--- Encoding MQTT fixed headers.
---@param packetType integer MQTT Package type.
---@param flags integer Fixed header flags.
---@param length integer remaining length.
---@return string bytes
function M.make_header(packetType, flags, length) end

--- Check if QoS is valid.
---@param value integer
---@return boolean valid
function M.check_qos(value) end

--- Check if the Packet Identifier is valid.
---@param value integer
---@return boolean valid
function M.check_packet_id(value) end

--- Get the next Packet Identifier.
---@param current? integer Current packet id.
---@return integer nextId Next packet id.
function M.next_packet_id(current) end

--- Determine whether the specified packet requires a Packet Identifier.
---@param args mqtt_packet* package parameters.
---@return boolean required Whether required.
function M.packet_id_required(args) end

--- Merge multiple package fragments.
---@param ... string|mqtt_combined_packet*
---@return mqtt_combined_packet* packet Combo pack.
function M.combine(...) end

--- Render the package as a readable string.
---@param packet mqtt_packet*
---@return string text Readable text.
function M.packet_tostring(packet) end

--- Start parsing the MQTT package.
---@param read_func mqtt_read_func
---@return integer|false packetType
---@return integer|string flagsOrErr
---@return table? input Package input object.
function M.start_parse_packet(read_func) end

--- Parse CONNECT packets.
---@param read_func mqtt_read_func
---@param version? integer Expected protocol version.
---@return mqtt_packet*|false packet
---@return string? errMsg
function M.parse_packet_connect(read_func, version) end

--- Continue parsing CONNECT packets from the input object that has been read in.
---@param input table Package input object.
---@param version? integer Expected protocol version.
---@return mqtt_packet*|false packet
---@return string? errMsg
function M.parse_packet_connect_input(input, version) end

return M
