use crate::{
ids::{MessageId, ProgramId},
message::{Payload, ReplyDetails, Value},
};
use core::convert::TryFrom;
use gear_core_errors::ReplyCode;
use scale_info::{
scale::{Decode, Encode},
TypeInfo,
};
use super::{MessageDetails, StoredMessage};
#[derive(Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)]
pub struct UserMessage {
id: MessageId,
source: ProgramId,
destination: ProgramId,
payload: Payload,
#[codec(compact)]
value: Value,
details: Option<ReplyDetails>,
}
impl UserMessage {
pub fn new(
id: MessageId,
source: ProgramId,
destination: ProgramId,
payload: Payload,
value: Value,
details: Option<ReplyDetails>,
) -> Self {
Self {
id,
source,
destination,
payload,
value,
details,
}
}
pub fn id(&self) -> MessageId {
self.id
}
pub fn source(&self) -> ProgramId {
self.source
}
pub fn destination(&self) -> ProgramId {
self.destination
}
pub fn payload_bytes(&self) -> &[u8] {
self.payload.inner()
}
pub fn value(&self) -> Value {
self.value
}
pub fn details(&self) -> Option<ReplyDetails> {
self.details
}
pub fn reply_code(&self) -> Option<ReplyCode> {
self.details.map(|d| d.to_reply_code())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct FromStoredMessageError;
impl TryFrom<StoredMessage> for UserMessage {
type Error = FromStoredMessageError;
fn try_from(stored: StoredMessage) -> Result<Self, Self::Error> {
let some_details = stored.details.is_some();
let details = stored.details.and_then(|d| d.to_reply_details());
if details.is_none() && some_details {
return Err(FromStoredMessageError);
}
Ok(Self {
id: stored.id,
source: stored.source,
destination: stored.destination,
payload: stored.payload,
value: stored.value,
details,
})
}
}
impl From<UserMessage> for StoredMessage {
fn from(user: UserMessage) -> Self {
let details = user.details.map(MessageDetails::Reply);
StoredMessage {
id: user.id,
source: user.source,
destination: user.destination,
payload: user.payload,
value: user.value,
details,
}
}
}
#[derive(Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)]
pub struct UserStoredMessage {
id: MessageId,
source: ProgramId,
destination: ProgramId,
payload: Payload,
#[codec(compact)]
value: Value,
}
impl UserStoredMessage {
pub fn new(
id: MessageId,
source: ProgramId,
destination: ProgramId,
payload: Payload,
value: Value,
) -> Self {
Self {
id,
source,
destination,
payload,
value,
}
}
pub fn id(&self) -> MessageId {
self.id
}
pub fn source(&self) -> ProgramId {
self.source
}
pub fn destination(&self) -> ProgramId {
self.destination
}
pub fn payload_bytes(&self) -> &[u8] {
self.payload.inner()
}
pub fn value(&self) -> Value {
self.value
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserStoredMessageConvertError;
impl TryFrom<StoredMessage> for UserStoredMessage {
type Error = UserStoredMessageConvertError;
fn try_from(stored: StoredMessage) -> Result<Self, Self::Error> {
if stored.details().is_some() {
return Err(UserStoredMessageConvertError);
}
Ok(Self {
id: stored.id,
source: stored.source,
destination: stored.destination,
payload: stored.payload,
value: stored.value,
})
}
}
impl TryFrom<UserMessage> for UserStoredMessage {
type Error = UserStoredMessageConvertError;
fn try_from(user: UserMessage) -> Result<Self, Self::Error> {
if user.details().is_some() {
return Err(UserStoredMessageConvertError);
}
Ok(Self {
id: user.id,
source: user.source,
destination: user.destination,
payload: user.payload,
value: user.value,
})
}
}
impl From<UserStoredMessage> for StoredMessage {
fn from(user_stored: UserStoredMessage) -> Self {
StoredMessage {
id: user_stored.id,
source: user_stored.source,
destination: user_stored.destination,
payload: user_stored.payload,
value: user_stored.value,
details: None,
}
}
}