protobuf

    The runtime library for protobuf

    idl
    protobuf
    codegen
    Download zip
    Version
    0.1.3
    License
    Apache-2.0 WITH LLVM-exception
    Last updated
    18 hours ago
    Downloads
    1K

    #Protobuf Runtime

    This package contains the MoonBit Protobuf runtime library, providing essential support for Protobuf message serialization and deserialization.

    #Directory Structure

    • src/
      • reader.mbt / writer.mbt: Basic read/write interfaces
      • async_reader.mbt / async_writer.mbt: Asynchronous read/write interfaces
      • json_utils.mbt: JSON utilities
      • proto.mbt: Protobuf traits definitions
      • types.mbt: Type definitions
      • sizeof.mbt: Message size calculation
      • reader_impl.mbt / writer_impl.mbt: Implementation reader/writer
      • reader_test.mbt / writer_test.mbt: Test cases

    AsyncProto

    pub(open) trait AsyncProto : AsyncRead + AsyncWrite {
    }

    AsyncRead

    pub(open) trait AsyncRead {
    async fn read(&AsyncReader) -> Self = _
    async fn read_with_limit(LimitedReader[&AsyncReader]) -> Self
    }

    AsyncReader

    pub(open) trait AsyncReader {
    async fn read(Self, FixedArray[Byte], offset~ : Int, max_length~ : Int) -> Int?
    }

    AsyncWrite

    pub(open) trait AsyncWrite {
    async fn write(Self, &AsyncWriter) -> Unit
    }

    AsyncWriter

    pub(open) trait AsyncWriter {
    async fn write(Self, BytesView) -> Unit
    }

    Proto

    pub(open) trait Proto : Read + Write {
    }

    Read

    pub(open) trait Read {
    fn read(&Reader) -> Self raise = _
    fn read_with_limit(LimitedReader[&Reader]) -> Self raise
    }

    Reader

    pub(open) trait Reader {
    fn read(Self, FixedArray[Byte], offset~ : Int, max_length~ : Int) -> Int? raise
    }

    Trait representing the reader

    Sized

    pub(open) trait Sized {
    fn size_of(Self) -> UInt
    }

    impl Sized for Bool
    impl Sized for Int
    impl Sized for Int64
    impl Sized for UInt
    impl Sized for UInt64
    impl Sized for Float
    impl Sized for Double
    impl Sized for String
    impl Sized for Bytes

    Write

    pub(open) trait Write {
    fn write(Self, &Writer) -> Unit raise
    }

    Writer

    pub(open) trait Writer {
    fn write(Self, BytesView) -> Unit raise
    }

    Trait representing the Writer
    impl Writer for Buffer

    ReaderError

    pub(all) suberror ReaderError {
    EndOfStream
    InvalidString
    InvalidBase64
    InvalidLength
    InvalidPackedLength
    OverlongVarint
    UnknownWireType(UInt)
    } derive(Eq,
    Debug
    )

    impl Show for ReaderError

    BytesReader

    type BytesReader

    impl Show for BytesReader

    BytesReader::from_bytes

    fn BytesReader::from_bytes(data : Bytes) -> BytesReader

    Enum

    Enum
    impl Show for Enum
    impl Sized for Enum

    Len

    pub(all) struct Len(Int) derive(Default, Eq,
    Debug
    )

    length
    impl Show for Len

    LimitedReader

    type LimitedReader[T]

    impl Reader for LimitedReader[T]

    LimitedReader::LimitedReader

    fn[T] LimitedReader::LimitedReader(reader : T, limit? : Int) -> LimitedReader[T]

    Creates a reader wrapper that optionally limits how many bytes a nested length-delimited message can consume.

    LimitedReader::new

    #deprecated("This function is deprecated.")
    fn[T] LimitedReader::new(reader : T, limit? : Int) -> LimitedReader[T]

    SInt

    pub(all) struct SInt(Int) derive(Default, Eq,
    Debug
    )

    sint32
    impl Show for SInt
    impl Sized for SInt

    SInt64

    pub(all) struct SInt64(Int64) derive(Default, Eq,
    Debug
    )

    sint64
    impl Show for SInt64
    impl Sized for SInt64

    WIRE_TYPE_END_GROUP

    let WIRE_TYPE_END_GROUP : UInt

    Protobuf end-group wire type.

    WIRE_TYPE_FIXED32

    let WIRE_TYPE_FIXED32 : UInt

    Protobuf 32-bit fixed-width wire type.

    WIRE_TYPE_FIXED64

    let WIRE_TYPE_FIXED64 : UInt

    Protobuf 64-bit fixed-width wire type.

    WIRE_TYPE_LENGTH_DELIMITED

    let WIRE_TYPE_LENGTH_DELIMITED : UInt

    Protobuf length-delimited wire type.

    WIRE_TYPE_START_GROUP

    let WIRE_TYPE_START_GROUP : UInt

    Protobuf start-group wire type.

    WIRE_TYPE_VARINT

    let WIRE_TYPE_VARINT : UInt

    Protobuf varint wire type.

    async_read_bool

    async fn[T : AsyncReader] async_read_bool(reader : T) -> Bool

    async_read_bytes

    async fn[T : AsyncReader] async_read_bytes(reader : T) -> Bytes

    async_read_delimited_message

    async fn[M : AsyncRead] async_read_delimited_message(reader : LimitedReader[&AsyncReader], field_number : UInt) -> M

    Reads a delimited message payload after its start-group tag has already been consumed, then consumes the matching end-group tag for field_number.

    This helper is used by generated code for proto2 groups and Editions features.message_encoding = DELIMITED.

    async_read_double

    async fn[T : AsyncReader] async_read_double(reader : T) -> Double

    async_read_enum

    async fn[T : AsyncReader] async_read_enum(reader : T) -> Enum

    async_read_fixed32

    async fn[T : AsyncReader] async_read_fixed32(reader : T) -> UInt

    async_read_fixed64

    async fn[T : AsyncReader] async_read_fixed64(reader : T) -> UInt64

    async_read_float

    async fn[T : AsyncReader] async_read_float(reader : T) -> Float

    async_read_int32

    async fn[T : AsyncReader] async_read_int32(reader : T) -> Int

    async_read_int64

    async fn[T : AsyncReader] async_read_int64(reader : T) -> Int64

    async_read_message

    async fn[M : AsyncRead + Default] async_read_message(reader : LimitedReader[&AsyncReader]) -> M

    Reads a length-delimited embedded message after its size prefix.

    async_read_next_message_field

    async fn[R : AsyncReader] async_read_next_message_field(reader : LimitedReader[R]) -> (UInt, UInt)?

    Reads the next field number and wire type within a message body.

    Returns None at the end of a length-delimited message or after consuming the matching end-group tag for a delimited message.

    async_read_packed

    async fn[M : Sized, R : AsyncReader] async_read_packed(reader : R, async_read_fn : async (LimitedReader[R]) -> M, size : UInt?) -> Array[M]

    Reads packed repeated field (Array[M])

    Note: packed field are stored as a variable length chunk of data, while regular repeated fields behaves like an iterator, yielding their tag everytime

    async_read_sfixed32

    async fn[T : AsyncReader] async_read_sfixed32(reader : T) -> Int

    async_read_sfixed64

    async fn[T : AsyncReader] async_read_sfixed64(reader : T) -> Int64

    async_read_sint32

    async fn[T : AsyncReader] async_read_sint32(reader : T) -> SInt

    async_read_sint64

    async fn[T : AsyncReader] async_read_sint64(reader : T) -> SInt64

    async_read_string

    async fn[T : AsyncReader] async_read_string(reader : T) -> String

    async_read_tag

    async fn[T : AsyncReader] async_read_tag(reader : T) -> (UInt, UInt)

    async_read_uint32

    async fn[T : AsyncReader] async_read_uint32(reader : T) -> UInt

    async_read_uint64

    async fn[T : AsyncReader] async_read_uint64(reader : T) -> UInt64

    async_read_unknown

    async fn[T : AsyncReader] async_read_unknown(reader : T, wire_type : UInt) -> Unit

    Skips an unknown non-group field payload by wire type.

    Use async_skip_message_field_by_number while parsing message bodies so unknown group fields can be skipped with their matching end-group field number.

    async_read_varint32

    async fn[T : AsyncReader] async_read_varint32(reader : T) -> UInt

    async_skip_message_field

    async fn[T : AsyncReader] async_skip_message_field(reader : T, wire_type : UInt) -> Unit

    Skips an unknown non-group message field by wire type.

    This legacy helper cannot skip WIRE_TYPE_START_GROUP fields because the matching end-group tag requires the field number.

    async_skip_message_field_by_number

    async fn[T : AsyncReader] async_skip_message_field_by_number(reader : T, field_number : UInt, wire_type : UInt) -> Unit

    Skips an unknown message field by field number and wire type.

    Unlike async_skip_message_field, this handles WIRE_TYPE_START_GROUP fields by reading nested fields until the matching WIRE_TYPE_END_GROUP tag is found.

    async_write_bool

    async fn[T : AsyncWriter] async_write_bool(writer : T, v : Bool) -> Unit

    async_write_bytes

    async fn[T : AsyncWriter] async_write_bytes(writer : T, v : Bytes) -> Unit

    async_write_delimited_message

    async fn[M : AsyncWrite, T : AsyncWriter] async_write_delimited_message(writer : T, field_number : UInt, message : M) -> Unit

    Writes a delimited message field envelope for field_number.

    The encoded form is start-group tag, message payload, then the matching end-group tag. Generated code uses this for proto2 groups and Editions features.message_encoding = DELIMITED.

    async_write_double

    async fn[T : AsyncWriter] async_write_double(writer : T, v : Double) -> Unit

    async_write_enum

    async fn[T : AsyncWriter] async_write_enum(writer : T, v : Enum) -> Unit

    async_write_fixed32

    async fn[T : AsyncWriter] async_write_fixed32(writer : T, v : UInt) -> Unit

    async_write_fixed64

    async fn[T : AsyncWriter] async_write_fixed64(writer : T, v : UInt64) -> Unit

    async_write_float

    async fn[T : AsyncWriter] async_write_float(writer : T, v : Float) -> Unit

    async_write_int32

    async fn[T : AsyncWriter] async_write_int32(writer : T, v : Int) -> Unit

    async_write_int64

    async fn[T : AsyncWriter] async_write_int64(writer : T, v : Int64) -> Unit

    async_write_message

    async fn[M : AsyncWrite + Sized, T : AsyncWriter] async_write_message(writer : T, message : M) -> Unit

    Writes a length-delimited embedded message with a size prefix.

    async_write_sfixed32

    async fn[T : AsyncWriter] async_write_sfixed32(writer : T, v : Int) -> Unit

    async_write_sfixed64

    async fn[T : AsyncWriter] async_write_sfixed64(writer : T, v : Int64) -> Unit

    async_write_sint32

    async fn[T : AsyncWriter] async_write_sint32(writer : T, v : Int) -> Unit

    async_write_sint64

    async fn[T : AsyncWriter] async_write_sint64(writer : T, v : Int64) -> Unit

    async_write_string

    async fn[T : AsyncWriter] async_write_string(writer : T, v : String) -> Unit

    async_write_tag

    async fn[T : AsyncWriter] async_write_tag(writer : T, tag : (UInt, UInt)) -> Unit

    async_write_uint32

    async fn[T : AsyncWriter] async_write_uint32(writer : T, v : UInt) -> Unit

    async_write_uint64

    async fn[T : AsyncWriter] async_write_uint64(writer : T, v : UInt64) -> Unit

    async_write_varint

    async fn[T : AsyncWriter] async_write_varint(writer : T, input : UInt64) -> Unit

    base64_decode

    fn base64_decode(s : String) -> Bytes raise

    base64_encode

    fn base64_encode(data : Bytes) -> String

    read_bool

    fn[T : Reader] read_bool(reader : T) -> Bool raise

    read_bytes

    fn[T : Reader] read_bytes(reader : T) -> Bytes raise

    read_delimited_message

    fn[M : Read] read_delimited_message(reader : LimitedReader[&Reader], field_number : UInt) -> M raise

    Reads a delimited message payload after its start-group tag has already been consumed, then consumes the matching end-group tag for field_number.

    This helper is used by generated code for proto2 groups and Editions features.message_encoding = DELIMITED.

    read_double

    fn[T : Reader] read_double(reader : T) -> Double raise

    read_enum

    fn[T : Reader] read_enum(reader : T) -> Enum raise

    read_fixed32

    fn[T : Reader] read_fixed32(reader : T) -> UInt raise

    read_fixed64

    fn[T : Reader] read_fixed64(reader : T) -> UInt64 raise

    read_float

    fn[T : Reader] read_float(reader : T) -> Float raise

    read_int32

    fn[T : Reader] read_int32(reader : T) -> Int raise

    read_int64

    fn[T : Reader] read_int64(reader : T) -> Int64 raise

    read_message

    fn[M : Read + Default] read_message(reader : LimitedReader[&Reader]) -> M raise

    Reads a length-delimited embedded message after its size prefix.

    read_next_message_field

    fn[R : Reader] read_next_message_field(reader : LimitedReader[R]) -> (UInt, UInt)? raise

    Reads the next field number and wire type within a message body.

    Returns None at the end of a length-delimited message or after consuming the matching end-group tag for a delimited message.

    read_packed

    fn[M : Sized, R : Reader] read_packed(reader : R, read_fn : (LimitedReader[R]) -> M raise, size : UInt?) -> Array[M] raise

    Reads packed repeated field (Array[M])

    Note: packed field are stored as a variable length chunk of data, while regular repeated fields behaves like an iterator, yielding their tag everytime

    read_sfixed32

    fn[T : Reader] read_sfixed32(reader : T) -> Int raise

    read_sfixed64

    fn[T : Reader] read_sfixed64(reader : T) -> Int64 raise

    read_sint32

    fn[T : Reader] read_sint32(reader : T) -> SInt raise

    read_sint64

    fn[T : Reader] read_sint64(reader : T) -> SInt64 raise

    read_string

    fn[T : Reader] read_string(reader : T) -> String raise

    read_tag

    fn[T : Reader] read_tag(reader : T) -> (UInt, UInt) raise

    read_uint32

    fn[T : Reader] read_uint32(reader : T) -> UInt raise

    read_uint64

    fn[T : Reader] read_uint64(reader : T) -> UInt64 raise

    read_unknown

    fn[T : Reader] read_unknown(reader : T, wire_type : UInt) -> Unit raise

    Skips an unknown non-group field payload by wire type.

    Use skip_message_field_by_number while parsing message bodies so unknown group fields can be skipped with their matching end-group field number.

    read_varint32

    fn[T : Reader] read_varint32(reader : T) -> UInt raise

    size_of

    fn[T : Sized] size_of(t : T) -> UInt

    size_of_delimited_field

    fn size_of_delimited_field(field_number : UInt, payload_size : UInt) -> UInt

    Size of a delimited message field envelope around an already-sized payload. field_number is the protobuf field number, not an encoded tag size.

    size_of_field

    fn size_of_field(field_number : UInt, payload_size : UInt) -> UInt

    Size of a non-length-delimited field envelope around an already-sized payload. field_number is the protobuf field number, not an encoded tag size.

    size_of_length_delimited_field

    fn size_of_length_delimited_field(field_number : UInt, payload_size : UInt) -> UInt

    Size of a length-delimited field envelope around an already-sized payload. field_number is the protobuf field number, not an encoded tag size.

    skip_message_field

    fn[T : Reader] skip_message_field(reader : T, wire_type : UInt) -> Unit raise

    Skips an unknown non-group message field by wire type.

    This legacy helper cannot skip WIRE_TYPE_START_GROUP fields because the matching end-group tag requires the field number.

    skip_message_field_by_number

    fn[T : Reader] skip_message_field_by_number(reader : T, field_number : UInt, wire_type : UInt) -> Unit raise

    Skips an unknown message field by field number and wire type.

    Unlike skip_message_field, this handles WIRE_TYPE_START_GROUP fields by reading nested fields until the matching WIRE_TYPE_END_GROUP tag is found.

    write_bool

    fn[T : Writer] write_bool(writer : T, v : Bool) -> Unit raise

    write_bytes

    fn[T : Writer] write_bytes(writer : T, v : Bytes) -> Unit raise

    write_delimited_message

    fn[M : Write, T : Writer] write_delimited_message(writer : T, field_number : UInt, message : M) -> Unit raise

    Writes a delimited message field envelope for field_number.

    The encoded form is start-group tag, message payload, then the matching end-group tag. Generated code uses this for proto2 groups and Editions features.message_encoding = DELIMITED.

    write_double

    fn[T : Writer] write_double(writer : T, v : Double) -> Unit raise

    write_enum

    fn[T : Writer] write_enum(writer : T, v : Enum) -> Unit raise

    write_fixed32

    fn[T : Writer] write_fixed32(writer : T, v : UInt) -> Unit raise

    write_fixed64

    fn[T : Writer] write_fixed64(writer : T, v : UInt64) -> Unit raise

    write_float

    fn[T : Writer] write_float(writer : T, v : Float) -> Unit raise

    write_int32

    fn[T : Writer] write_int32(writer : T, v : Int) -> Unit raise

    write_int64

    fn[T : Writer] write_int64(writer : T, v : Int64) -> Unit raise

    write_message

    fn[M : Write + Sized, T : Writer] write_message(writer : T, message : M) -> Unit raise

    Writes a length-delimited embedded message with a size prefix.

    write_sfixed32

    fn[T : Writer] write_sfixed32(writer : T, v : Int) -> Unit raise

    write_sfixed64

    fn[T : Writer] write_sfixed64(writer : T, v : Int64) -> Unit raise

    write_sint32

    fn[T : Writer] write_sint32(writer : T, v : Int) -> Unit raise

    write_sint64

    fn[T : Writer] write_sint64(writer : T, v : Int64) -> Unit raise

    write_string

    fn[T : Writer] write_string(writer : T, v : String) -> Unit raise

    write_tag

    fn[T : Writer] write_tag(writer : T, tag : (UInt, UInt)) -> Unit raise

    write_uint32

    fn[T : Writer] write_uint32(writer : T, v : UInt) -> Unit raise

    write_uint64

    fn[T : Writer] write_uint64(writer : T, v : UInt64) -> Unit raise

    write_varint

    fn[T : Writer] write_varint(writer : T, input : UInt64) -> Unit raise