Expand description

Example module for the interface! macro.

You can compare the source of the generated documentation to inspect the generated API. The most important generated types are Client and Server.

fizyr_rpc::interface! {
    /// RPC interface for the supermarket.
    pub interface Supermarket {
        /// Greet the cashier.
        ///
        /// The cashier will reply with their own greeting.
        service 1 greet_cashier: String -> String,

        /// Purchase tomatoes.
        ///
        /// The response of the cashier depends on the update messages exchanged.
        /// If you run away after they have sent the `price`, or if you pay with a nun-fungable token,
        /// they will respond with [`BuyTomatoesResponse::ICalledSecurity`].
        ///
        /// If you pay with the correct amount, they will respond with [`BuyTomatoesResponse::ThankYouComeAgain`].
        service 2 buy_tomatoes: BuyTomatoesRequest -> BuyTomatoesResponse {
            /// Sent once by the cashier to notify you of the price of the tomatoes.
            response_update 1 price: Price,

            /// Sent by the client to pay for the tomatoes.
            request_update 1 pay: Payment,

            /// Sent by broke or kleptomanic clients that still want tomatoes.
            request_update 2 run_away: (),
        }

        /// Mutter something as you're walking through the supermarket.
        ///
        /// Since no-one will respond, this is a stream rather than a service.
        ///
        /// Popular phrases include:
        ///  * Woah thats cheap!
        ///  * Everything used to be better in the good old days...
        ///  * Why did they move the toilet paper?
        stream 1 mutter: String,
    }
}

/// The initial request to buy tomatoes.
#[derive(Debug)]
pub struct BuyTomatoesRequest {
    /// The tomatoes you want to buy.
    pub amount: usize,
}

/// The price for something.
#[derive(Debug)]
pub struct Price {
    /// The total price in cents.
    pub total_price_cents: usize,
}

/// Payment options for purchasing tomatoes.
#[derive(Debug)]
pub enum Payment {
    /// Payment in money.
    Money {
        /// The amount of money in cents.
        cents: usize
    },

    /// Payment with an NFT.
    NonFungibleToken,
}

/// The response of a cashier when buying tomatoes.
#[derive(Debug)]
pub enum BuyTomatoesResponse {
    /// A final greeting and your receipt.
    ThankYouComeAgain(Receipt),

    /// Security has been called.
    ICalledSecurity,
}

/// A receipt for your purchase.
#[derive(Debug)]
pub struct Receipt {
    /// The number of tomatoes you bought.
    pub amount_of_tomatoes: usize,

    /// The total price you paid for the tomatoes.
    pub total_price_cents: usize,

    /// If the cashier really liked you, they may write their phone number on the receipt with pen.
    pub phone_number: Option<String>,
}

Modules

Structs

  • The initial request to buy tomatoes.
  • RPC client for the Supermarket interface.
  • Introspection for the Supermarket RPC interface.
  • The price for something.
  • A receipt for your purchase.
  • RPC server for the Supermarket interface.

Enums

Traits

  • Trait for formats that are compatible with this interface.