{"id":18006490,"url":"https://github.com/plietar/rust-protobuf-macros","last_synced_at":"2025-03-26T12:30:38.283Z","repository":{"id":30971559,"uuid":"34529764","full_name":"plietar/rust-protobuf-macros","owner":"plietar","description":"Easy protobuf in rust","archived":false,"fork":false,"pushed_at":"2017-02-09T01:07:45.000Z","size":41,"stargazers_count":30,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-14T19:43:42.513Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/plietar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-24T16:36:15.000Z","updated_at":"2023-06-09T03:12:18.000Z","dependencies_parsed_at":"2022-09-08T19:13:34.693Z","dependency_job_id":null,"html_url":"https://github.com/plietar/rust-protobuf-macros","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plietar%2Frust-protobuf-macros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plietar%2Frust-protobuf-macros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plietar%2Frust-protobuf-macros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plietar%2Frust-protobuf-macros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/plietar","download_url":"https://codeload.github.com/plietar/rust-protobuf-macros/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222144762,"owners_count":16938456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T01:08:35.470Z","updated_at":"2024-10-30T01:08:35.965Z","avatar_url":"https://github.com/plietar.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rust-protobuf-macros\nMacros designed to make protobuf easier to use in rust.\n\nThe macros use [stepancheg/rust-protobuf](https://github.com/stepancheg/rust-protobuf),\nbut provide a simpler syntax to set and get fields from protobuf objects.\n\n## Installation\n### Rust nightly\nAdd to your `Cargo.toml`\n```toml\n[dependencies.protobuf]\ngit = \"https://github.com/stepancheg/rust-protobuf.git\"\n[dependencies.protobuf_macros]\ngit = \"https://github.com/plietar/rust-protobuf-macros.git\"\n```\n\nThen enable it in your crate :\n```rust\n#![feature(plugin)]\n#![plugin(protobuf_macros)]\n```\n\n### Rust stable\nThe plugin can be used on rust stable using [syntex](https://github.com/serde-rs/syntex) :\n\nAdd to your `Cargo.toml` :\n```toml\n[dependencies.protobuf]\ngit = \"https://github.com/stepancheg/rust-protobuf.git\"\n[build-dependencies.protobuf_macros]\ngit = \"https://github.com/plietar/rust-protobuf-macros.git\"\n```\n\nAnd add a build script :\n```rust\nextern crate protobuf_macros;\nuse std::env;\nuse std::path::PathBuf;\n\nfn main() {\n  let out = PathBuf::from(env::var(\"OUT_DIR\").unwrap());\n  protobuf_macros::expand(\"src/lib.in.rs\", \u0026out.join(\"lib.rs\")).unwrap();\n}\n```\n\n## Usage\nThe examples use the following schema :\n```protobuf\nmessage Person {\n  required string name = 1;\n  required int32 id = 2;\n  repeated string email = 3;\n\n  enum PhoneType {\n    MOBILE = 0;\n    HOME = 1;\n    WORK = 2;\n  }\n\n  message PhoneNumber {\n    required string number = 1;\n    optional PhoneType type = 2 [default = HOME];\n  }\n\n  repeated PhoneNumber phone = 4;\n\n  message Job {\n    required string title = 1;\n    required string company = 2;\n  }\n\n  optional Job job = 5;\n}\n\nmessage AddressBook {\n  repeated Person person = 1;\n}\n```\n\n### protobuf_init!\nThe `protobuf_init!` macro is used to fill an existing protobuf object.\n\n```rust\nlet person = protobuf_init!(Person::new(), {\n    name: \"Joe\",\n    id: 42,\n    email =\u003e [\n        \"joe@domain.com\",\n        \"joe@other.com\"\n    ],\n    phone =\u003e [\n        @{\n            number: \"0123456789\",\n            field_type: Person_PhoneType::HOME\n        },\n        @{\n            number: \"9876543210\",\n            field_type: Person_PhoneType::WORK\n        }\n    ]\n\n    job =\u003e {\n        title: \"Boss\",\n        company: \"Big Corp\"\n    }\n});\n```\n\n### protobuf_bind!\nThe `protobuf_bind!` macro is used to extract data from a protobuf object.\nThe variable names on the right are created by the macro using a let statement.\n\n```rust\nprotobuf_bind!(person, {\n    name: person_name,\n    id: person_id,\n    email: emails,\n    phone: phone_numbers,\n    job =\u003e {\n        title: job_title,\n        company: company\n    }\n});\n```\n\nNote that it is not possible to extract the repeated fields. Instead, you will get\na `RepeatedField` object. You can iterate on it, and call `protobuf_bind!` again\non its elements.\n\n```rust\nfor phone in phone_numbers {\n    protobuf_bind!(phone, {\n        number: number,\n        field_type: phone_type,\n    });\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplietar%2Frust-protobuf-macros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplietar%2Frust-protobuf-macros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplietar%2Frust-protobuf-macros/lists"}