forest/libp2p_bitswap/
bitswap_pb.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3// Automatically generated rust module for 'bitswap_pb.proto' file
4// Command: `pb-rs -s -D proto/bitswap_ob.proto`, See <https://crates.io/crates/pb-rs>
5
6#![allow(non_snake_case)]
7#![allow(non_upper_case_globals)]
8#![allow(non_camel_case_types)]
9#![allow(unused_imports)]
10#![allow(unknown_lints)]
11#![allow(clippy::all)]
12#![cfg_attr(rustfmt, rustfmt_skip)]
13
14
15use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result};
16use quick_protobuf::sizeofs::*;
17use super::*;
18
19#[allow(clippy::derive_partial_eq_without_eq)]
20#[derive(Debug, Default, PartialEq, Clone)]
21pub struct Message {
22    pub wantlist: Option<mod_Message::Wantlist>,
23    pub blocks: Vec<Vec<u8>>,
24    pub payload: Vec<mod_Message::Block>,
25    pub blockPresences: Vec<mod_Message::BlockPresence>,
26    pub pendingBytes: i32,
27}
28
29impl<'a> MessageRead<'a> for Message {
30    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
31        let mut msg = Self::default();
32        while !r.is_eof() {
33            match r.next_tag(bytes) {
34                Ok(10) => msg.wantlist = Some(r.read_message::<mod_Message::Wantlist>(bytes)?),
35                Ok(18) => msg.blocks.push(r.read_bytes(bytes)?.to_owned()),
36                Ok(26) => msg.payload.push(r.read_message::<mod_Message::Block>(bytes)?),
37                Ok(34) => msg.blockPresences.push(r.read_message::<mod_Message::BlockPresence>(bytes)?),
38                Ok(40) => msg.pendingBytes = r.read_int32(bytes)?,
39                Ok(t) => { r.read_unknown(bytes, t)?; }
40                Err(e) => return Err(e),
41            }
42        }
43        Ok(msg)
44    }
45}
46
47impl MessageWrite for Message {
48    fn get_size(&self) -> usize {
49        0
50        + self.wantlist.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
51        + self.blocks.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
52        + self.payload.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::<usize>()
53        + self.blockPresences.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::<usize>()
54        + if self.pendingBytes == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.pendingBytes) as u64) }
55    }
56
57    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
58        if let Some(ref s) = self.wantlist { w.write_with_tag(10, |w| w.write_message(s))?; }
59        for s in &self.blocks { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
60        for s in &self.payload { w.write_with_tag(26, |w| w.write_message(s))?; }
61        for s in &self.blockPresences { w.write_with_tag(34, |w| w.write_message(s))?; }
62        if self.pendingBytes != 0i32 { w.write_with_tag(40, |w| w.write_int32(*&self.pendingBytes))?; }
63        Ok(())
64    }
65}
66
67pub mod mod_Message {
68
69use super::*;
70
71#[allow(clippy::derive_partial_eq_without_eq)]
72#[derive(Debug, Default, PartialEq, Clone)]
73pub struct Wantlist {
74    pub entries: Vec<mod_Message::mod_Wantlist::Entry>,
75    pub full: bool,
76}
77
78impl<'a> MessageRead<'a> for Wantlist {
79    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
80        let mut msg = Self::default();
81        while !r.is_eof() {
82            match r.next_tag(bytes) {
83                Ok(10) => msg.entries.push(r.read_message::<mod_Message::mod_Wantlist::Entry>(bytes)?),
84                Ok(16) => msg.full = r.read_bool(bytes)?,
85                Ok(t) => { r.read_unknown(bytes, t)?; }
86                Err(e) => return Err(e),
87            }
88        }
89        Ok(msg)
90    }
91}
92
93impl MessageWrite for Wantlist {
94    fn get_size(&self) -> usize {
95        0
96        + self.entries.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::<usize>()
97        + if self.full == false { 0 } else { 1 + sizeof_varint(*(&self.full) as u64) }
98    }
99
100    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
101        for s in &self.entries { w.write_with_tag(10, |w| w.write_message(s))?; }
102        if self.full != false { w.write_with_tag(16, |w| w.write_bool(*&self.full))?; }
103        Ok(())
104    }
105}
106
107pub mod mod_Wantlist {
108
109use super::*;
110
111#[allow(clippy::derive_partial_eq_without_eq)]
112#[derive(Debug, Default, PartialEq, Clone)]
113pub struct Entry {
114    pub block: Vec<u8>,
115    pub priority: i32,
116    pub cancel: bool,
117    pub wantType: mod_Message::mod_Wantlist::WantType,
118    pub sendDontHave: bool,
119}
120
121impl<'a> MessageRead<'a> for Entry {
122    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
123        let mut msg = Self::default();
124        while !r.is_eof() {
125            match r.next_tag(bytes) {
126                Ok(10) => msg.block = r.read_bytes(bytes)?.to_owned(),
127                Ok(16) => msg.priority = r.read_int32(bytes)?,
128                Ok(24) => msg.cancel = r.read_bool(bytes)?,
129                Ok(32) => msg.wantType = r.read_enum(bytes)?,
130                Ok(40) => msg.sendDontHave = r.read_bool(bytes)?,
131                Ok(t) => { r.read_unknown(bytes, t)?; }
132                Err(e) => return Err(e),
133            }
134        }
135        Ok(msg)
136    }
137}
138
139impl MessageWrite for Entry {
140    fn get_size(&self) -> usize {
141        0
142        + if self.block.is_empty() { 0 } else { 1 + sizeof_len((&self.block).len()) }
143        + if self.priority == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.priority) as u64) }
144        + if self.cancel == false { 0 } else { 1 + sizeof_varint(*(&self.cancel) as u64) }
145        + if self.wantType == bitswap_pb::mod_Message::mod_Wantlist::WantType::Block { 0 } else { 1 + sizeof_varint(*(&self.wantType) as u64) }
146        + if self.sendDontHave == false { 0 } else { 1 + sizeof_varint(*(&self.sendDontHave) as u64) }
147    }
148
149    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
150        if !self.block.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.block))?; }
151        if self.priority != 0i32 { w.write_with_tag(16, |w| w.write_int32(*&self.priority))?; }
152        if self.cancel != false { w.write_with_tag(24, |w| w.write_bool(*&self.cancel))?; }
153        if self.wantType != bitswap_pb::mod_Message::mod_Wantlist::WantType::Block { w.write_with_tag(32, |w| w.write_enum(*&self.wantType as i32))?; }
154        if self.sendDontHave != false { w.write_with_tag(40, |w| w.write_bool(*&self.sendDontHave))?; }
155        Ok(())
156    }
157}
158
159#[derive(Debug, PartialEq, Eq, Clone, Copy)]
160pub enum WantType {
161    Block = 0,
162    Have = 1,
163}
164
165impl Default for WantType {
166    fn default() -> Self {
167        WantType::Block
168    }
169}
170
171impl From<i32> for WantType {
172    fn from(i: i32) -> Self {
173        match i {
174            0 => WantType::Block,
175            1 => WantType::Have,
176            _ => Self::default(),
177        }
178    }
179}
180
181impl<'a> From<&'a str> for WantType {
182    fn from(s: &'a str) -> Self {
183        match s {
184            "Block" => WantType::Block,
185            "Have" => WantType::Have,
186            _ => Self::default(),
187        }
188    }
189}
190
191}
192
193#[allow(clippy::derive_partial_eq_without_eq)]
194#[derive(Debug, Default, PartialEq, Clone)]
195pub struct Block {
196    pub prefix: Vec<u8>,
197    pub data: Vec<u8>,
198}
199
200impl<'a> MessageRead<'a> for Block {
201    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
202        let mut msg = Self::default();
203        while !r.is_eof() {
204            match r.next_tag(bytes) {
205                Ok(10) => msg.prefix = r.read_bytes(bytes)?.to_owned(),
206                Ok(18) => msg.data = r.read_bytes(bytes)?.to_owned(),
207                Ok(t) => { r.read_unknown(bytes, t)?; }
208                Err(e) => return Err(e),
209            }
210        }
211        Ok(msg)
212    }
213}
214
215impl MessageWrite for Block {
216    fn get_size(&self) -> usize {
217        0
218        + if self.prefix.is_empty() { 0 } else { 1 + sizeof_len((&self.prefix).len()) }
219        + if self.data.is_empty() { 0 } else { 1 + sizeof_len((&self.data).len()) }
220    }
221
222    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
223        if !self.prefix.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.prefix))?; }
224        if !self.data.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.data))?; }
225        Ok(())
226    }
227}
228
229#[allow(clippy::derive_partial_eq_without_eq)]
230#[derive(Debug, Default, PartialEq, Clone)]
231pub struct BlockPresence {
232    pub cid: Vec<u8>,
233    pub type_pb: mod_Message::BlockPresenceType,
234}
235
236impl<'a> MessageRead<'a> for BlockPresence {
237    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
238        let mut msg = Self::default();
239        while !r.is_eof() {
240            match r.next_tag(bytes) {
241                Ok(10) => msg.cid = r.read_bytes(bytes)?.to_owned(),
242                Ok(16) => msg.type_pb = r.read_enum(bytes)?,
243                Ok(t) => { r.read_unknown(bytes, t)?; }
244                Err(e) => return Err(e),
245            }
246        }
247        Ok(msg)
248    }
249}
250
251impl MessageWrite for BlockPresence {
252    fn get_size(&self) -> usize {
253        0
254        + if self.cid.is_empty() { 0 } else { 1 + sizeof_len((&self.cid).len()) }
255        + if self.type_pb == bitswap_pb::mod_Message::BlockPresenceType::Have { 0 } else { 1 + sizeof_varint(*(&self.type_pb) as u64) }
256    }
257
258    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
259        if !self.cid.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.cid))?; }
260        if self.type_pb != bitswap_pb::mod_Message::BlockPresenceType::Have { w.write_with_tag(16, |w| w.write_enum(*&self.type_pb as i32))?; }
261        Ok(())
262    }
263}
264
265#[derive(Debug, PartialEq, Eq, Clone, Copy)]
266pub enum BlockPresenceType {
267    Have = 0,
268    DontHave = 1,
269}
270
271impl Default for BlockPresenceType {
272    fn default() -> Self {
273        BlockPresenceType::Have
274    }
275}
276
277impl From<i32> for BlockPresenceType {
278    fn from(i: i32) -> Self {
279        match i {
280            0 => BlockPresenceType::Have,
281            1 => BlockPresenceType::DontHave,
282            _ => Self::default(),
283        }
284    }
285}
286
287impl<'a> From<&'a str> for BlockPresenceType {
288    fn from(s: &'a str) -> Self {
289        match s {
290            "Have" => BlockPresenceType::Have,
291            "DontHave" => BlockPresenceType::DontHave,
292            _ => Self::default(),
293        }
294    }
295}
296
297}
298