blockfrost / blockfrost-rust

Rust SDK for Blockfrost.io
Apache License 2.0
16 stars 17 forks source link

Pi/endpoint macro #4

Closed Quantumplation closed 3 years ago

Quantumplation commented 3 years ago

A macro_rules macro to make defining endpoints easier;

The syntax is:

endpoints! {
  /// Description
  function_name(param: type) -> Return => "route";
    ("link_to_docs"),
  ...
}

In theory supports multiple parameters, different types, etc.

Macros can be a bit dense, so I tried to document how it works; If it causes an issue in the future, tag me and I'll be happy to help fix it!

Quantumplation commented 3 years ago

Resolves #3

marcospb19 commented 3 years ago

Amazing, I loved the macro docs, merged!

marcospb19 commented 3 years ago

If proc-macros were to be easier to implement, we could use them to generate the doc URL in compile time.

Instead of:

impl BlockFrostApi {
    endpoints! {
        /// Return the content of a requested block for a specific slot in an epoch.
        blocks_by_epoch_and_slot(epoch_number: Integer, slot_number: Integer) -> Block => "/blocks/epoch/{epoch_number}/slot/{slot_number}";
        ("https://docs.blockfrost.io/#tag/Cardano-Blocks/paths/~1blocks~1epoch~1{epoch_number}~1slot~1{slot_number}/get"),

        /// Return the list of blocks following a specific block.
        blocks_next(hash_or_number: String) -> Vec<Block>  "/blocks/{hash_or_number}/next";
            ("https://docs.blockfrost.io/#tag/Cardano-Blocks/paths/~1blocks~1{hash_or_number}~1next/get"),
    }
}

Use this:

#[endpoints]
impl BlockFrostApi {
    /// Return the content of a requested block for a specific slot in an epoch.
    blocks_by_epoch_and_slot(epoch_number: Integer, slot_number: Integer) -> Block { "/blocks/epoch/{epoch_number}/slot/{slot_number}" }

    /// Return the list of blocks following a specific block.
    blocks_next(hash_or_number: String) -> Vec<Block> { "/blocks/{hash_or_number}/next" }
}

It would just need to replace "/" by "~1".

Maybe in the future, who knows.