Skip to content

Commit fc66b12

Browse files
authored
feat: add wrap option to macro to optionally generate wrapper types for enum variants (#51)
This adds an optional `wrap` attribute to the macro. When set, it will take the enum fields (can be any kind: unit, named, tuple), turn them into a struct, and modify the enum to have a single field as usually expected by irpc. This reduces the boiler plate a lot for simple protocols. Also it allows you to skip the tedious newtyping if you e.g. have a lot of different requests that all take a single arg of the same type (e.g. a Uuid or a Hash). It is purely optional, and works quite well with editors thanks to the span support (jump to definition jumps to the value of the wrap type, hover over that type gives the full fields, and jump to definition on a field of the generated struct takes you directly to the field on the enum variant even). See the `simple.rs` example in the diff for the full boilerplatefree irpc-iroh galore. I.e. it turns this ```rust #[rpc_requests(message = FooMessage)] #[derive(Debug, Serialize, Deserialize)] pub enum FooProtocol { #[rpc(wrap=GetRequest, tx=oneshot::Sender<Option<String>>)] Get(String), #[rpc(wrap=SetRequest, tx=oneshot::Sender<Option<String>>)] Set { key: String, value: String }, } ``` into this ```rust #[rpc_requests(message = FooMessage)] #[derive(Debug, Serialize, Deserialize)] pub enum FooProtocol { #[rpc(wrap=GetRequest, tx=oneshot::Sender<Option<String>>)] Get(GetRequest), #[rpc(wrap=SetRequest, tx=oneshot::Sender<Option<String>>)] Set(SetRequest) } #[derive(Debug, Serialize, Deserialize)] pub struct GetRequst(String) #[derive(Debug, Serialize, Deserialize)] pub struct SetRequest { key: String, value : String } ```
1 parent b363f00 commit fc66b12

File tree

6 files changed

+439
-49
lines changed

6 files changed

+439
-49
lines changed

Cargo.lock

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)