re_log_encoding/
protobuf_conversions.rs

1impl From<re_protos::log_msg::v0::Compression> for crate::Compression {
2    fn from(value: re_protos::log_msg::v0::Compression) -> Self {
3        match value {
4            re_protos::log_msg::v0::Compression::None => Self::Off,
5            re_protos::log_msg::v0::Compression::Lz4 => Self::LZ4,
6        }
7    }
8}
9
10impl From<crate::Compression> for re_protos::log_msg::v0::Compression {
11    fn from(value: crate::Compression) -> Self {
12        match value {
13            crate::Compression::Off => Self::None,
14            crate::Compression::LZ4 => Self::Lz4,
15        }
16    }
17}
18
19#[cfg(feature = "decoder")]
20pub fn log_msg_from_proto(
21    message: re_protos::log_msg::v0::LogMsg,
22) -> Result<re_log_types::LogMsg, crate::decoder::DecodeError> {
23    use crate::codec::{arrow::decode_arrow, CodecError};
24    use crate::decoder::DecodeError;
25    use re_protos::{
26        log_msg::v0::{log_msg::Msg, Encoding},
27        missing_field,
28    };
29
30    match message.msg {
31        Some(Msg::SetStoreInfo(set_store_info)) => Ok(re_log_types::LogMsg::SetStoreInfo(
32            set_store_info.try_into()?,
33        )),
34        Some(Msg::ArrowMsg(arrow_msg)) => {
35            if arrow_msg.encoding() != Encoding::ArrowIpc {
36                return Err(DecodeError::Codec(CodecError::UnsupportedEncoding));
37            }
38
39            let batch = decode_arrow(
40                &arrow_msg.payload,
41                arrow_msg.uncompressed_size as usize,
42                arrow_msg.compression().into(),
43            )?;
44
45            let store_id: re_log_types::StoreId = arrow_msg
46                .store_id
47                .ok_or_else(|| missing_field!(re_protos::log_msg::v0::ArrowMsg, "store_id"))?
48                .into();
49
50            let chunk = re_chunk::Chunk::from_record_batch(batch)?;
51
52            Ok(re_log_types::LogMsg::ArrowMsg(
53                store_id,
54                chunk.to_arrow_msg()?,
55            ))
56        }
57        Some(Msg::BlueprintActivationCommand(blueprint_activation_command)) => {
58            Ok(re_log_types::LogMsg::BlueprintActivationCommand(
59                blueprint_activation_command.try_into()?,
60            ))
61        }
62        None => Err(missing_field!(re_protos::log_msg::v0::LogMsg, "msg").into()),
63    }
64}
65
66#[cfg(feature = "encoder")]
67pub fn log_msg_to_proto(
68    message: re_log_types::LogMsg,
69    compression: crate::Compression,
70) -> Result<re_protos::log_msg::v0::LogMsg, crate::encoder::EncodeError> {
71    use crate::codec::arrow::encode_arrow;
72    use re_protos::log_msg::v0::{
73        ArrowMsg, BlueprintActivationCommand, LogMsg as ProtoLogMsg, SetStoreInfo,
74    };
75
76    let proto_msg = match message {
77        re_log_types::LogMsg::SetStoreInfo(set_store_info) => {
78            let set_store_info: SetStoreInfo = set_store_info.into();
79            ProtoLogMsg {
80                msg: Some(re_protos::log_msg::v0::log_msg::Msg::SetStoreInfo(
81                    set_store_info,
82                )),
83            }
84        }
85        re_log_types::LogMsg::ArrowMsg(store_id, arrow_msg) => {
86            let payload = encode_arrow(&arrow_msg.batch, compression)?;
87            let arrow_msg = ArrowMsg {
88                store_id: Some(store_id.into()),
89                compression: match compression {
90                    crate::Compression::Off => re_protos::log_msg::v0::Compression::None as i32,
91                    crate::Compression::LZ4 => re_protos::log_msg::v0::Compression::Lz4 as i32,
92                },
93                uncompressed_size: payload.uncompressed_size as i32,
94                encoding: re_protos::log_msg::v0::Encoding::ArrowIpc as i32,
95                payload: payload.data,
96            };
97            ProtoLogMsg {
98                msg: Some(re_protos::log_msg::v0::log_msg::Msg::ArrowMsg(arrow_msg)),
99            }
100        }
101        re_log_types::LogMsg::BlueprintActivationCommand(blueprint_activation_command) => {
102            let blueprint_activation_command: BlueprintActivationCommand =
103                blueprint_activation_command.into();
104            ProtoLogMsg {
105                msg: Some(
106                    re_protos::log_msg::v0::log_msg::Msg::BlueprintActivationCommand(
107                        blueprint_activation_command,
108                    ),
109                ),
110            }
111        }
112    };
113
114    Ok(proto_msg)
115}