easy_callback 1.0.0

Automatic callback data encoding for teloxide
Documentation

easy_callback

Crates.io Documentation Tests License

Automatic callback data encoding for teloxide.

This crate provides a derive macro for quickly implementing conversions between CallbackQuery and a custom user-defined type, with support for different encoder implementations.


Example

This example demonstrates the library's functionality using the count_bot example.

// ...

// Callback enum encoded using postcard
#[derive(Default, Serialize, Deserialize, PostcardCallback)]
enum Callback {
  Increment(i64),
  Decrement(i64),
  #[default]
  Fallback,
}

// ...

fn keyboard(value: i64) -> InlineKeyboardMarkup {
    // The callback is automatically encoded using postcard
    InlineKeyboardMarkup::new([[
        InlineKeyboardButton::callback("+1", Callback::Increment(value)),
        InlineKeyboardButton::callback("-1", Callback::Decrement(value)),
    ]])
}

// ...

async fn on_callback(bot: Bot, callback_query: CallbackQuery) -> ResponseResult<()> {
  // ...

  // Decode the callback query into the Callback enum
  let callback = Callback::from(callback_query);
  
  // ...
}

// ...

Usage

Add the following to your Cargo.toml, choosing one or more features corresponding to the implementations you want to use:

[dependencies]

easy_callback = { version = "1.0.0", features=["postcard"] }

Available implementations:

Multiple implementations can be enabled simultaneously if required.


License

This project is licensed under either of

at your option.