Skip to content

Commit

Permalink
fix: clippy and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Jan 28, 2025
1 parent efe17dc commit 4857a42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
//! register JSON-RPC methods and their handlers.
//!
//! ```no_run
//! use ajj::{Router, HandlerCtx, ResponsePayload};
//! use ajj::{Router, HandlerCtx, ResponsePayload, Params};
//!
//! # fn main() {
//! // Provide methods called "double" and "add" to the router.
//! let router = Router::<u64>::new()
//! .route("double", |params: u64| async move {
//! .route("double", |Params(params): Params<u64>| async move {
//! Ok::<_, ()>(params * 2)
//! })
//! .route("add", |params: u64, state: u64| async move {
Expand Down
6 changes: 4 additions & 2 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ use tracing::{debug_span, trace};
/// These methods can
///
/// ```
/// use ajj::{Router};
/// use ajj::{Router, Params};
///
/// # fn main() {
/// // Provide methods called "double" and "add" to the router.
/// let router = Router::<u64>::new()
/// .route("double", |params: u64| async move {
/// // when double is called, the provided number is doubled.
/// // see the Handler docs for more information on the hint type
/// .route("double", |Params(params): Params<u64>| async move {
/// Ok::<_, ()>(params * 2)
/// })
/// .route("add", |params: u64, state: u64| async move {
Expand Down
17 changes: 9 additions & 8 deletions src/routes/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ macro_rules! convert_result {
/// trait "Handler argument type inference" section for more information.
#[derive(Debug)]
#[repr(transparent)]
pub struct State<S>(S);
pub struct State<S>(pub S);

/// Hint type for differentiating certain handler impls. See the [`Handler`]
/// trait "Handler argument type inference" section for more information.
#[derive(Debug)]
#[repr(transparent)]
pub struct Params<T>(T);
pub struct Params<T>(pub T);

/// Marker type used for differentiating certain handler impls.
#[allow(missing_debug_implementations, unreachable_pub)]
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct PhantomParams<T>(PhantomData<T>);
/// When the following conditions are true, the compiler may fail to infer
/// whether a handler argument is `params` or `state`:
///
/// 1. The handler takes EITHER `params` or `state`
/// 1. The handler takes EITHER `params` or `state`.
/// 2. The `S` type of the router is a valid `params` type (i.e. it impls
/// [`serde::de::DeserializeOwned`] and satisfies the other requirements of
/// [`RpcRecv`]).
Expand All @@ -70,16 +70,17 @@ pub struct PhantomParams<T>(PhantomData<T>);
/// wrapper struct to indicate that the argument is `state`, or the [`Params`]
/// wrapper struct to indicate that the argument is `params`.
///
/// ```no_run
/// ```compile_fail
/// use ajj::{Router, Params, State};
///
/// async fn ok() -> Result<(), ()> { Ok(()) }
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Router::new::<u8>()
/// // this will fail to infer the `Handler` impl as the argument could be
/// // either `params` or `state`
/// // The error will look like this:
/// // cannot infer type for type parameter `T` declared on the method `route`
/// .route("foo", |something: u8| ok())
/// // this will succeed as the `State` wrapper indicates that the argument
/// // is `state`
Expand Down Expand Up @@ -939,7 +940,7 @@ mod test {
fn combination_inference() {
let router: crate::Router<()> = crate::Router::<NewType>::new()
//responses
.route("respnse", || ok())
.route("respnse", ok)
.route("respnse, ctx", |_: HandlerCtx| resp_ok())
.route("respnse, params", |_: u16| resp_ok())
.route("respnse, ctx, params", |_: HandlerCtx, _: u16| resp_ok())
Expand All @@ -950,7 +951,7 @@ mod test {
|_: HandlerCtx, _: u8, _: NewType| resp_ok(),
)
// results
.route("result", || ok())
.route("result", ok)
.route("result, ctx", |_: HandlerCtx| ok())
.route("result, params", |_: u16| ok())
.route("result, ctx, params", |_: HandlerCtx, _: u16| ok())
Expand Down

0 comments on commit 4857a42

Please sign in to comment.