use hedera_proto::mirror;
use time::OffsetDateTime;
use crate::{
FromProtobuf,
TransactionId,
};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct TopicMessage {
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<serde_with::TimestampNanoSeconds>")
)]
pub consensus_timestamp: OffsetDateTime,
#[cfg_attr(feature = "ffi", serde(with = "serde_with::As::<serde_with::base64::Base64>"))]
pub contents: Vec<u8>,
#[cfg_attr(feature = "ffi", serde(with = "serde_with::As::<serde_with::base64::Base64>"))]
pub running_hash: Vec<u8>,
pub running_hash_version: u64,
pub sequence_number: u64,
pub initial_transaction_id: Option<TransactionId>,
pub chunk_number: u32,
pub chunk_total: u32,
}
impl FromProtobuf<mirror::ConsensusTopicResponse> for TopicMessage {
fn from_protobuf(pb: mirror::ConsensusTopicResponse) -> crate::Result<Self>
where
Self: Sized,
{
let consensus_at = pb_getf!(pb, consensus_timestamp)?.into();
let sequence_number = pb.sequence_number;
let running_hash = pb.running_hash;
let running_hash_version = pb.running_hash_version;
let contents = pb.message;
let (initial_transaction_id, chunk_number, chunk_total) = if let Some(chunk_info) =
pb.chunk_info
{
(chunk_info.initial_transaction_id, chunk_info.number as u32, chunk_info.total as u32)
} else {
(None, 1, 1)
};
let initial_transaction_id = Option::from_protobuf(initial_transaction_id)?;
Ok(Self {
consensus_timestamp: consensus_at,
contents,
running_hash,
running_hash_version,
sequence_number,
initial_transaction_id,
chunk_number,
chunk_total,
})
}
}