use crate::{
buffer::Payload,
ids::{ActorId, MessageId},
message::{
Dispatch, DispatchKind, GasLimit, Message, Packet, StoredDispatch, StoredMessage, Value,
},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HandleMessage {
id: MessageId,
destination: ActorId,
payload: Payload,
gas_limit: Option<GasLimit>,
value: Value,
}
impl HandleMessage {
pub fn from_packet(id: MessageId, packet: HandlePacket) -> Self {
Self {
id,
destination: packet.destination,
payload: packet.payload,
gas_limit: packet.gas_limit,
value: packet.value,
}
}
pub fn into_message(self, source: ActorId) -> Message {
Message::new(
self.id,
source,
self.destination,
self.payload,
self.gas_limit,
self.value,
None,
)
}
pub fn into_stored(self, source: ActorId) -> StoredMessage {
self.into_message(source).into()
}
pub fn into_dispatch(self, source: ActorId) -> Dispatch {
Dispatch::new(DispatchKind::Handle, self.into_message(source))
}
pub fn into_stored_dispatch(self, source: ActorId) -> StoredDispatch {
self.into_dispatch(source).into()
}
pub fn id(&self) -> MessageId {
self.id
}
pub fn destination(&self) -> ActorId {
self.destination
}
pub fn payload_bytes(&self) -> &[u8] {
&self.payload
}
pub fn gas_limit(&self) -> Option<GasLimit> {
self.gas_limit
}
pub fn value(&self) -> Value {
self.value
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(any(feature = "mock", test), derive(Default))]
pub struct HandlePacket {
destination: ActorId,
payload: Payload,
gas_limit: Option<GasLimit>,
value: Value,
}
impl HandlePacket {
pub fn new(destination: ActorId, payload: Payload, value: Value) -> Self {
Self {
destination,
payload,
gas_limit: None,
value,
}
}
pub fn new_with_gas(
destination: ActorId,
payload: Payload,
gas_limit: GasLimit,
value: Value,
) -> Self {
Self {
destination,
payload,
gas_limit: Some(gas_limit),
value,
}
}
pub fn maybe_with_gas(
destination: ActorId,
payload: Payload,
gas_limit: Option<GasLimit>,
value: Value,
) -> Self {
match gas_limit {
None => Self::new(destination, payload, value),
Some(gas_limit) => Self::new_with_gas(destination, payload, gas_limit, value),
}
}
pub(super) fn try_prepend(&mut self, mut data: Payload) -> Result<(), Payload> {
if data.try_extend_from_slice(self.payload_bytes()).is_err() {
Err(data)
} else {
self.payload = data;
Ok(())
}
}
pub fn destination(&self) -> ActorId {
self.destination
}
}
impl Packet for HandlePacket {
fn payload_bytes(&self) -> &[u8] {
&self.payload
}
fn payload_len(&self) -> u32 {
self.payload.len_u32()
}
fn gas_limit(&self) -> Option<GasLimit> {
self.gas_limit
}
fn value(&self) -> Value {
self.value
}
fn kind() -> DispatchKind {
DispatchKind::Handle
}
}