Skip to main content

MessageKind

Struct MessageKind 

Source
pub struct MessageKind {
    pub version: u8,
    pub request_id: Option<u64>,
    pub trade_index: Option<i64>,
    pub id: Option<Uuid>,
    pub action: Action,
    pub payload: Option<Payload>,
}
Expand description

Body shared by every Message variant.

All Mostro protocol messages share this envelope: a protocol version, an optional client-chosen request id for correlation, a trade index used to enforce strictly increasing sequences per user, an optional order/dispute id, an Action and an optional Payload.

Fields§

§version: u8

Mostro protocol version. Set to PROTOCOL_VER by MessageKind::new.

§request_id: Option<u64>

Client-chosen correlation id, echoed back on responses so the client can match them to in-flight requests.

§trade_index: Option<i64>

Trade index attached to this message. Must be strictly greater than the last trade index Mostro has seen for the sender.

§id: Option<Uuid>

Optional target identifier (usually the id of an crate::order::Order or crate::dispute::Dispute).

§action: Action

Verb of the message.

§payload: Option<Payload>

Payload attached to the action. The allowed shape for a given action is enforced by MessageKind::verify.

Implementations§

Source§

impl MessageKind

Source

pub fn new( id: Option<Uuid>, request_id: Option<u64>, trade_index: Option<i64>, action: Action, payload: Option<Payload>, ) -> Self

Build a new MessageKind stamped with the current protocol version (PROTOCOL_VER).

Source

pub fn from_json(json: &str) -> Result<Self, ServiceError>

Parse a MessageKind from its JSON representation.

Source

pub fn as_json(&self) -> Result<String, ServiceError>

Serialize the MessageKind to a JSON string.

Source

pub fn get_action(&self) -> Action

Return a clone of the Action carried by this message.

Source

pub fn get_next_trade_key(&self) -> Result<Option<(String, u32)>, ServiceError>

Extract the (next_trade_pubkey, next_trade_index) pair from a Payload::NextTrade payload.

Returns Ok(None) when there is no payload at all and ServiceError::InvalidPayload when the payload is present but of a different variant.

Source

pub fn get_rating(&self) -> Result<u8, ServiceError>

Extract the rating value from a Payload::RatingUser payload, validating it against MIN_RATING..=MAX_RATING.

Returns ServiceError::InvalidRating when the payload shape is wrong and ServiceError::InvalidRatingValue when the value is out of range.

Source

pub fn verify(&self) -> bool

Check that the payload, id and trade index are consistent with the action carried by this message.

Returns true when the combination is well-formed and false otherwise; Mostro uses this method to reject malformed requests before processing them.

Source

pub fn get_order(&self) -> Option<&SmallOrder>

Return the SmallOrder carried by a Action::NewOrder message.

Yields None if the action is not NewOrder or the payload is of a different variant.

Source

pub fn get_payment_request(&self) -> Option<String>

Return the Lightning payment request embedded in a message.

Valid only for Action::TakeSell, Action::AddInvoice and Action::NewOrder. For NewOrder, the invoice is read from the SmallOrder::buyer_invoice field. Returns None otherwise.

Source

pub fn get_amount(&self) -> Option<i64>

Return the amount override embedded in a Action::TakeSell or Action::TakeBuy message, either from a Payload::Amount or from the third element of a Payload::PaymentRequest.

Source

pub fn get_payload(&self) -> Option<&Payload>

Borrow the optional payload.

Source

pub fn has_trade_index(&self) -> (bool, i64)

Return (true, index) when the message carries a trade index, (false, 0) otherwise.

Source

pub fn trade_index(&self) -> i64

Return the trade index carried by the message, or 0 when absent.

Trait Implementations§

Source§

impl Clone for MessageKind

Source§

fn clone(&self) -> MessageKind

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MessageKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for MessageKind

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for MessageKind

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,