steam_vent_proto_common/
lib.rs

1pub use protobuf;
2use protobuf::Enum;
3use std::fmt::Debug;
4use std::io::{Read, Write};
5
6/// Constant used to verify that all proto crates use the same version of the codegen.
7pub const VERSION_0_5_0: () = ();
8
9pub trait RpcService {
10    const SERVICE_NAME: &'static str;
11}
12
13pub trait RpcMethod: protobuf::Message + RpcMessage {
14    const METHOD_NAME: &'static str;
15    type Response: RpcMessage;
16}
17
18pub trait RpcMessage: Debug + Sized {
19    fn parse(_reader: &mut dyn Read) -> protobuf::Result<Self>;
20
21    fn write(&self, _writer: &mut dyn Write) -> protobuf::Result<()>;
22
23    fn encode_size(&self) -> usize;
24}
25
26impl RpcMessage for () {
27    fn parse(_reader: &mut dyn Read) -> protobuf::Result<Self> {
28        Ok(())
29    }
30
31    fn write(&self, _writer: &mut dyn Write) -> protobuf::Result<()> {
32        Ok(())
33    }
34
35    fn encode_size(&self) -> usize {
36        0
37    }
38}
39
40pub trait RpcMessageWithKind: RpcMessage {
41    type KindEnum: MsgKindEnum;
42    const KIND: Self::KindEnum;
43}
44
45#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
46pub struct MsgKind(pub i32);
47
48impl MsgKind {
49    pub fn value(&self) -> i32 {
50        self.0
51    }
52}
53
54impl From<MsgKind> for i32 {
55    fn from(value: MsgKind) -> Self {
56        value.0
57    }
58}
59
60pub const PROTO_MASK: u32 = 0x80000000;
61
62pub trait MsgKindEnum: Enum + Debug {
63    fn enum_value(&self) -> i32 {
64        <Self as Enum>::value(self)
65    }
66
67    fn encode_kind(&self, is_protobuf: bool) -> u32 {
68        if is_protobuf {
69            self.enum_value() as u32 | PROTO_MASK
70        } else {
71            self.enum_value() as u32
72        }
73    }
74}
75
76impl<T: MsgKindEnum> From<T> for MsgKind {
77    fn from(value: T) -> Self {
78        MsgKind(value.enum_value())
79    }
80}
81
82impl<T: MsgKindEnum> PartialEq<T> for MsgKind {
83    fn eq(&self, other: &T) -> bool {
84        self.0.eq(&other.enum_value())
85    }
86}
87
88/// Trait for implementing a check for completion of a job that returns a response stream
89pub trait JobMultiple {
90    /// If the job has completed
91    fn completed(&self) -> bool;
92}