Skip to content

Commit 2dfffec

Browse files
authored
refactor: improve and document handler type inference (#18)
* refactor: improve and document handler type inference * fix: clippy and tests * docs: more info in the handler docs * doc: fix em * doc: nits * docs: slight improvements
1 parent a36b7e7 commit 2dfffec

File tree

7 files changed

+476
-54
lines changed

7 files changed

+476
-54
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Simple, modern, ergonomic JSON-RPC 2.0 router built with tower an
55
keywords = ["json-rpc", "jsonrpc", "json"]
66
categories = ["web-programming::http-server", "web-programming::websocket"]
77

8-
version = "0.1.2"
8+
version = "0.2.0"
99
edition = "2021"
1010
rust-version = "1.81"
1111
authors = ["init4", "James Prestwich"]

src/lib.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
//! ```no_run
1313
//! use ajj::{Router, HandlerCtx, ResponsePayload};
1414
//!
15-
//! # fn main() {
15+
//! # fn test_fn() -> Router<()> {
1616
//! // Provide methods called "double" and "add" to the router.
1717
//! let router = Router::<u64>::new()
18-
//! .route("double", |params: u64| async move {
19-
//! Ok::<_, ()>(params * 2)
20-
//! })
2118
//! .route("add", |params: u64, state: u64| async move {
2219
//! Ok::<_, ()>(params + state)
2320
//! })
21+
//! .with_state(3u64)
22+
//! .route("double", |params: u64| async move {
23+
//! Ok::<_, ()>(params * 2)
24+
//! })
2425
//! // Routes get a ctx, which can be used to send notifications.
2526
//! .route("notify", |ctx: HandlerCtx| async move {
2627
//! if ctx.notifications().is_none() {
@@ -43,9 +44,8 @@
4344
//! .route("error_example", || async {
4445
//! // This will appear in the ResponsePayload's `message` field.
4546
//! ResponsePayload::<(), ()>::internal_error_message("this is an error".into())
46-
//! })
47-
//! // The router is provided with state, and is now ready to serve requests.
48-
//! .with_state::<()>(3u64);
47+
//! });
48+
//! # router
4949
//! # }
5050
//! ```
5151
//!
@@ -144,7 +144,9 @@ pub mod pubsub;
144144
pub use pubsub::ReadJsonStream;
145145

146146
mod routes;
147-
pub use routes::{BatchFuture, Handler, HandlerArgs, HandlerCtx, NotifyError, RouteFuture};
147+
pub use routes::{
148+
BatchFuture, Handler, HandlerArgs, HandlerCtx, NotifyError, Params, RouteFuture, State,
149+
};
148150
pub(crate) use routes::{BoxedIntoRoute, ErasedIntoRoute, Method, Route};
149151

150152
mod router;

src/router.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,25 @@ use tracing::{debug_span, trace};
3333
/// The `double` method doubles the provided number, and the `add` method adds
3434
/// the provided number to some state, and returns the sum.
3535
///
36-
/// These methods can
37-
///
3836
/// ```
39-
/// use ajj::{Router};
37+
/// use ajj::Router;
4038
///
4139
/// # fn main() {
4240
/// // Provide methods called "double" and "add" to the router.
4341
/// let router = Router::<u64>::new()
44-
/// .route("double", |params: u64| async move {
45-
/// Ok::<_, ()>(params * 2)
46-
/// })
42+
/// // This route requires state to be provided.
4743
/// .route("add", |params: u64, state: u64| async move {
4844
/// Ok::<_, ()>(params + state)
4945
/// })
50-
/// // The router is provided with state, and is now ready to serve requests.
51-
/// .with_state::<()>(3u64);
46+
/// .with_state::<()>(3u64)
47+
/// // when double is called, the provided number is doubled.
48+
/// // see the Handler docs for more information on the hint type
49+
/// .route("double", |params: u64| async move {
50+
/// Ok::<_, ()>(params * 2)
51+
/// });
5252
/// # }
5353
/// ```
5454
///
55-
///
5655
/// ## Note
5756
///
5857
/// The state `S` is "missing" state. It is state that must be added to the

src/routes/future.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ use tower::util::{BoxCloneSyncService, Oneshot};
1313

1414
use super::Response;
1515

16-
/// A future produced by [`Router::call_with_state`]. This should only be
17-
/// instantiated by that method.
16+
/// A future produced by the [`Router`].
1817
///
19-
/// [`Route`]: crate::routes::Route
18+
/// [`Router`]: crate::Router
2019
#[pin_project]
2120
pub struct RouteFuture {
2221
/// The inner [`Route`] future.
@@ -159,7 +158,9 @@ impl BatchFuture {
159158
}
160159

161160
/// Push a parse result into the batch. Convenience function to simplify
162-
/// [`Router::call_batch_with_state`] logic.
161+
/// [`Router`] logic.
162+
///
163+
/// [`Router`]: crate::Router
163164
pub(crate) fn push_parse_result(&mut self, result: Result<RouteFuture, RequestError>) {
164165
match result {
165166
Ok(fut) => self.push(fut),

0 commit comments

Comments
 (0)