traq_bot_http/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#![deny(clippy::pedantic, clippy::cargo)]
//! [](https://github.com/H1rono/traq-bot-http-rs/blob/main/LICENSE)
//! [](https://crates.io/crates/traq-bot-http)
//! [](https://github.com/H1rono/traq-bot-http-rs/releases/latest)
//! [](https://crates.io/crates/traq-bot-http)
//!
//! traQ BOTのPOSTリクエストをパースするライブラリです。
//!
//! [examples](https://github.com/H1rono/traq-bot-http-rs/blob/main/examples)
mod error;
mod events;
pub(crate) mod macros;
mod parser;
pub mod payloads;
#[cfg(feature = "tower")]
pub mod handler;
pub use error::{Error, ErrorKind, Result};
pub use events::{Event, EventKind};
/// HTTP POSTリクエストのパーサー
#[must_use]
#[derive(Debug, Clone)]
pub struct RequestParser {
verification_token: String,
}
#[cfg(feature = "tower")]
/// axumライクなhandler APIを提供します。[`handler`]モジュールのドキュメントも併せて読んでください。
///
/// # Example
///
/// ```
/// use tower::service_fn;
/// use traq_bot_http::{payloads, RequestParser};
///
/// async fn on_ping((state, payload): (i32, payloads::PingPayload)) -> Result<(), std::convert::Infallible> {
/// println!("state: {state:?}, ping: {payload:?}");
/// // assert_eq!(state, 0);
/// Ok::<(), std::convert::Infallible>(())
/// }
///
/// let parser = RequestParser::new("traqbotverificationtoken");
/// let handler = parser
/// .into_handler()
/// .on_ping(service_fn(on_ping))
/// .with_state(0i32);
/// # let _ = handler;
/// ```
///
/// # Note
/// この構造体の型パラメータは**unstable**です。`Handler<T>`における`T`は予告なく変化する可能性があります。
///
/// [`handler`]: crate::handler
#[must_use]
#[derive(Debug, Clone)]
pub struct Handler<Service> {
service: Service,
parser: RequestParser,
}
#[cfg(test)]
pub(crate) mod test_utils;