[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"7wXaLs1uq4":3},"# protobuf\n`protobuf` is an implementation of Google's Protocol Buffers in Lean 4, supporting `proto2`, `proto3`, and `edition`.\n\nThe goal of this package is to be the standard choice among all Lean 4 protobuf implementations. So far (1/7/2026), this packages has been fully featured in terms of **all core protobuf features** a user would expect.\n\n# Usage\n\nThere are 5 methods to use this library:\n\n1. Load a standalone .proto file.\n2. Load a folder containing .proto files.\n3. As a protoc plugin.\n4. Use the internal notation.\n5. Use the encoding/decoding utilities directly.\n\n**The first 3 methods require the `protoc` command.\nThe last tested `protoc` version is `libprotoc 30.2`.**\n\nDownstream users of this package can expect the first 3 methods to be always reliable and production ready. The first two methods are highly recommended for production use.\n\n## Standalone .proto file\n\nSay you have a file `proto/A.proto` relative to **package root**:\n\n```protobuf\nsyntax = \"proto3\";\n\npackage test.a;\n\nmessage A {\n    optional int32 t = 1;\n}\n\nmessage Q {\n    oneof q {\n        A a = 1;\n        Q b = 2;\n    }\n    map\u003Cint32, int32> s = 4;\n}\n```\n\nIn any Lean source file:\n\n```lean\nimport Protobuf\n\nopen Protobuf Encoding Notation\n\n#load_proto_file \"proto/A.proto\"\n\n#check test.a.A.t\n\ninstance : Repr ByteArray where\n  reprPrec x p := s!\"{reprPrec x.data p}\"\n\n#eval test.a.Q.encode { q := test.a.Q.q_Type.a { t := some 1 } }\n```\n\n## A folder of .proto files\n\n```lean\nimport Protobuf\n\nopen Protobuf Encoding Notation\n\n#load_proto_dir \"folder\"\n...\n```\n\n## As a protoc plugin\n\n**Warning: Currently (v4.26.0) Lean 4 compiler does not prune the `meta` imports, causing executables to be exceedingly huge (180 MiB).**\n\nFirst prepare a folder to contain the plugin, say `\u003Cplugin_folder>`.\n\n```bash\nclone https://github.com/Lean-zh/protobuf.git\ncd protobuf\nlake build Plugin\ncp ./.lake/build/bin/protoc-gen-lean4 \u003Cplugin_folder>\n```\n\nThen create a Lean 4 project, with name `Foo`.\n\n```bash\ncd \u003Croot_of_Foo>\nmkdir Foo/Proto\nprotoc --plugin=protoc-gen-lean4=\u003Cplugin_folder>/protoc-gen-lean4 --lean4_out=./Foo/Proto --lean4_opt=lean4_prefix=Foo.Proto -I \u003Cproto_files_search_path> \u003Cproto_file>\n```\n\n## Internal notation\n\n**NOTE: the internal notation is protobuf-version-neutral, that is, you have to specify very specific behaviors of the encoding.**\n\nOne example is, in any lean source file:\n\n```lean\nimport Protobuf\n\nopen Protobuf Encoding Notation\n\nmessage A {\n  repeated int32 a = 1 [packed = true];\n}\n\n#eval A.encode { a := #[1, 2, 3] }\n```\n\nWith this you can define messages in a very convenient and compact way, and it does not require the `protoc` command to be present.\n\n## Using encoding/decoding API\nPlease read the source code under the folder `Encoding` to learn their usage.\n\nThis usage is highly unrecommended and should only serve for debugging purposes.\n\n# Missing features\n\nWork in progress:\n\n1. Reflection API: e.g. function `descriptor : MsgType -> Descriptor`. The option `no_standard_descriptor_accessor` is currently ignored.\n2. Json mapping: designing, likely to be an add-on after we have reflection API.\n3. Service/RPC: we will need to think through frameworking issues first. Currently services are ignored.\n\n## Less likely to have\nSome of them may never be supported:\n\n### -Proto1 behaviors\n`proto1` has been too old and is not worth an implementation.\nFor example, option `message_set_wire_format` is forbidden.\n\n\n### -SGROUP/EGROUP in serialization \nThe delimited serialization of message is not allowed, though they can be deserialized from wire format. The `edition` `features` enabling this are forbidden.\n\nNote: It is indiscernible to an end user whether the wire format of a submessage is delimited or nested, since all protobuf implementations are expected to parse both, and so is this package.\n\nThus the absence of this feature does not affect protobuf functionality in an observable way.\n### -Group fields\nOnly `proto2` allows group fields, for example:\n```protobuf\nrepeated group Result = 1 { fields... }\n```\n\nThis is forbidden. Use nested messages instead.\n\n### -Options of extension fields\nFor example:\n```protobuf\nextend A {\n  optional int32 a = 42 [default = 1];\n}\n```\nThe `default` option (and other options) of `extend`ed field `a` has no effects.\n",1781732210720]