use crate::{
buffer::Payload,
ids::{ActorId, MessageId},
message::{
ContextStore, DispatchKind, GasLimit, IncomingDispatch, IncomingMessage, ReplyDetails,
Value, common::MessageDetails,
},
};
use core::ops::Deref;
use gear_core_errors::ReplyCode;
use parity_scale_codec::{Decode, Encode};
use scale_decode::DecodeAsType;
use scale_encode::EncodeAsType;
use scale_info::TypeInfo;
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo,
)]
pub struct StoredMessage {
pub(super) id: MessageId,
pub(super) source: ActorId,
pub(super) destination: ActorId,
pub(super) payload: Payload,
#[codec(compact)]
pub(super) value: Value,
pub(super) details: Option<MessageDetails>,
}
impl StoredMessage {
pub fn new(
id: MessageId,
source: ActorId,
destination: ActorId,
payload: Payload,
value: Value,
details: Option<MessageDetails>,
) -> Self {
Self {
id,
source,
destination,
payload,
value,
details,
}
}
pub fn into_parts(
self,
) -> (
MessageId,
ActorId,
ActorId,
Payload,
Value,
Option<MessageDetails>,
) {
(
self.id,
self.source,
self.destination,
self.payload,
self.value,
self.details,
)
}
pub fn into_incoming(self, gas_limit: GasLimit) -> IncomingMessage {
IncomingMessage::new(
self.id,
self.source,
self.payload,
gas_limit,
self.value,
self.details,
)
}
pub fn id(&self) -> MessageId {
self.id
}
pub fn source(&self) -> ActorId {
self.source
}
pub fn destination(&self) -> ActorId {
self.destination
}
pub fn payload_bytes(&self) -> &[u8] {
&self.payload
}
pub fn value(&self) -> Value {
self.value
}
pub fn details(&self) -> Option<MessageDetails> {
self.details
}
pub fn reply_details(&self) -> Option<ReplyDetails> {
self.details.and_then(|d| d.to_reply_details())
}
pub fn is_error_reply(&self) -> bool {
self.details.map(|d| d.is_error_reply()).unwrap_or(false)
}
pub fn is_reply(&self) -> bool {
self.details.map(|d| d.is_reply_details()).unwrap_or(false)
}
pub fn reply_code(&self) -> Option<ReplyCode> {
self.details
.and_then(|d| d.to_reply_details().map(|d| d.to_reply_code()))
}
}
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo,
)]
pub struct StoredDispatch {
kind: DispatchKind,
message: StoredMessage,
context: Option<ContextStore>,
}
impl From<StoredDispatch> for (DispatchKind, StoredMessage, Option<ContextStore>) {
fn from(dispatch: StoredDispatch) -> (DispatchKind, StoredMessage, Option<ContextStore>) {
(dispatch.kind, dispatch.message, dispatch.context)
}
}
impl StoredDispatch {
pub fn new(kind: DispatchKind, message: StoredMessage, context: Option<ContextStore>) -> Self {
Self {
kind,
message,
context,
}
}
pub fn into_incoming(self, gas_limit: GasLimit) -> IncomingDispatch {
IncomingDispatch::new(
self.kind,
self.message.into_incoming(gas_limit),
self.context,
)
}
pub fn into_parts(self) -> (DispatchKind, StoredMessage, Option<ContextStore>) {
self.into()
}
pub fn kind(&self) -> DispatchKind {
self.kind
}
pub fn message(&self) -> &StoredMessage {
&self.message
}
pub fn context(&self) -> &Option<ContextStore> {
&self.context
}
}
impl Deref for StoredDispatch {
type Target = StoredMessage;
fn deref(&self) -> &Self::Target {
self.message()
}
}
impl From<StoredDelayedDispatch> for StoredDispatch {
fn from(dispatch: StoredDelayedDispatch) -> Self {
StoredDispatch::new(dispatch.kind, dispatch.message, None)
}
}
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Decode, DecodeAsType, Encode, EncodeAsType, TypeInfo,
)]
pub struct StoredDelayedDispatch {
kind: DispatchKind,
message: StoredMessage,
}
impl From<StoredDelayedDispatch> for (DispatchKind, StoredMessage) {
fn from(dispatch: StoredDelayedDispatch) -> (DispatchKind, StoredMessage) {
(dispatch.kind, dispatch.message)
}
}
impl StoredDelayedDispatch {
pub fn new(kind: DispatchKind, message: StoredMessage) -> Self {
Self { kind, message }
}
pub fn into_parts(self) -> (DispatchKind, StoredMessage) {
self.into()
}
pub fn kind(&self) -> DispatchKind {
self.kind
}
pub fn message(&self) -> &StoredMessage {
&self.message
}
}
impl Deref for StoredDelayedDispatch {
type Target = StoredMessage;
fn deref(&self) -> &Self::Target {
self.message()
}
}