resp-async 0.0.7

Asynchronous Redis protocol parser
Documentation
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use std::collections::HashMap;
use std::convert::Infallible;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;

use bytes::{BufMut, Bytes, BytesMut};

use crate::context::{
    ClientId, Cmd, Command, Extensions, LocalAddr, PeerAddr, PubSubHandle, PushHandle,
    RequestContext, State as AppState,
};
use crate::resp::Value;
use crate::response::{IntoResponse, RespError, Response};

/// Extract a typed value from a request context.
pub trait FromRequest<State>: Sized {
    type Rejection: IntoResponse;

    fn from_request(
        ctx: &mut RequestContext,
        state: &Arc<State>,
    ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
}

/// Handler for a RESP command.
pub trait Handler<State>: Send + Sync + 'static {
    fn call(&self, ctx: RequestContext, state: Arc<State>) -> BoxFuture<Response>;
}

type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
type HandlerMarker5<T1, T2, T3, T4, T5> = fn(T1, T2, T3, T4, T5);
type HandlerMarker6<T1, T2, T3, T4, T5, T6> = fn(T1, T2, T3, T4, T5, T6);

pub trait IntoHandler<State, Args>: Send + Sync + 'static {
    fn into_handler(self) -> Arc<dyn Handler<State>>;
}

struct HandlerFn0<F> {
    f: Arc<F>,
}

struct HandlerFn1<F, T1> {
    f: Arc<F>,
    _t1: PhantomData<fn(T1)>,
}

struct HandlerFn2<F, T1, T2> {
    f: Arc<F>,
    _t: PhantomData<fn(T1, T2)>,
}

struct HandlerFn3<F, T1, T2, T3> {
    f: Arc<F>,
    _t: PhantomData<fn(T1, T2, T3)>,
}

struct HandlerFn4<F, T1, T2, T3, T4> {
    f: Arc<F>,
    _t: PhantomData<fn(T1, T2, T3, T4)>,
}

struct HandlerFn5<F, T1, T2, T3, T4, T5> {
    f: Arc<F>,
    _t: PhantomData<HandlerMarker5<T1, T2, T3, T4, T5>>,
}

struct HandlerFn6<F, T1, T2, T3, T4, T5, T6> {
    f: Arc<F>,
    _t: PhantomData<HandlerMarker6<T1, T2, T3, T4, T5, T6>>,
}

macro_rules! impl_handler {
    ($name:ident, $( $ty:ident ),* ) => {
        #[allow(non_snake_case)]
        impl<State, F, Fut, R, $( $ty ),*> Handler<State> for $name<F, $( $ty ),*>
        where
            F: Send + Sync + 'static + Fn($( $ty ),*) -> Fut,
            Fut: Future<Output = R> + Send + 'static,
            R: IntoResponse,
            $( $ty: FromRequest<State> + Send + 'static, )*
            State: Send + Sync + 'static,
        {
    fn call(&self, mut ctx: RequestContext, state: Arc<State>) -> BoxFuture<Response> {
        let f = Arc::clone(&self.f);
        Box::pin(async move {
            log_handler_start(&ctx);
            $(
                let $ty = match $ty::from_request(&mut ctx, &state).await {
                    Ok(value) => value,
                    Err(rejection) => {
                        let response = rejection.into_response();
                        log_handler_result(&ctx, &response);
                        return response;
                    }
                };
            )*

            let response = f($( $ty ),*).await.into_response();
            log_handler_result(&ctx, &response);
            response
        })
    }
        }
    };
}

impl<State, F, Fut, R> Handler<State> for HandlerFn0<F>
where
    F: Send + Sync + 'static + Fn() -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    State: Send + Sync + 'static,
{
    fn call(&self, ctx: RequestContext, _state: Arc<State>) -> BoxFuture<Response> {
        let f = Arc::clone(&self.f);
        Box::pin(async move {
            log_handler_start(&ctx);
            let response = f().await.into_response();
            log_handler_result(&ctx, &response);
            response
        })
    }
}

impl_handler!(HandlerFn1, T1);
impl_handler!(HandlerFn2, T1, T2);
impl_handler!(HandlerFn3, T1, T2, T3);
impl_handler!(HandlerFn4, T1, T2, T3, T4);
impl_handler!(HandlerFn5, T1, T2, T3, T4, T5);
impl_handler!(HandlerFn6, T1, T2, T3, T4, T5, T6);

impl<State, F, Fut, R> IntoHandler<State, ()> for F
where
    F: Send + Sync + 'static + Fn() -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn0 { f: Arc::new(self) })
    }
}

impl<State, F, Fut, R, T1> IntoHandler<State, (T1,)> for F
where
    F: Send + Sync + 'static + Fn(T1) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn1 {
            f: Arc::new(self),
            _t1: PhantomData,
        })
    }
}

impl<State, F, Fut, R, T1, T2> IntoHandler<State, (T1, T2)> for F
where
    F: Send + Sync + 'static + Fn(T1, T2) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    T2: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn2 {
            f: Arc::new(self),
            _t: PhantomData,
        })
    }
}

impl<State, F, Fut, R, T1, T2, T3> IntoHandler<State, (T1, T2, T3)> for F
where
    F: Send + Sync + 'static + Fn(T1, T2, T3) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    T2: FromRequest<State> + Send + 'static,
    T3: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn3 {
            f: Arc::new(self),
            _t: PhantomData,
        })
    }
}

impl<State, F, Fut, R, T1, T2, T3, T4> IntoHandler<State, (T1, T2, T3, T4)> for F
where
    F: Send + Sync + 'static + Fn(T1, T2, T3, T4) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    T2: FromRequest<State> + Send + 'static,
    T3: FromRequest<State> + Send + 'static,
    T4: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn4 {
            f: Arc::new(self),
            _t: PhantomData,
        })
    }
}

impl<State, F, Fut, R, T1, T2, T3, T4, T5> IntoHandler<State, (T1, T2, T3, T4, T5)> for F
where
    F: Send + Sync + 'static + Fn(T1, T2, T3, T4, T5) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    T2: FromRequest<State> + Send + 'static,
    T3: FromRequest<State> + Send + 'static,
    T4: FromRequest<State> + Send + 'static,
    T5: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn5 {
            f: Arc::new(self),
            _t: PhantomData,
        })
    }
}

impl<State, F, Fut, R, T1, T2, T3, T4, T5, T6> IntoHandler<State, (T1, T2, T3, T4, T5, T6)> for F
where
    F: Send + Sync + 'static + Fn(T1, T2, T3, T4, T5, T6) -> Fut,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
    T1: FromRequest<State> + Send + 'static,
    T2: FromRequest<State> + Send + 'static,
    T3: FromRequest<State> + Send + 'static,
    T4: FromRequest<State> + Send + 'static,
    T5: FromRequest<State> + Send + 'static,
    T6: FromRequest<State> + Send + 'static,
    State: Send + Sync + 'static,
{
    fn into_handler(self) -> Arc<dyn Handler<State>> {
        Arc::new(HandlerFn6 {
            f: Arc::new(self),
            _t: PhantomData,
        })
    }
}

/// Router mapping command names to handlers.
pub struct Router<State = ()> {
    inner: Arc<RouterInner<State>>,
}

impl<State> Clone for Router<State> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<State> Default for Router<State>
where
    State: Default + Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

struct RouterInner<State> {
    state: Arc<State>,
    routes: HashMap<Bytes, Arc<dyn Handler<State>>>,
}

impl<State> Router<State>
where
    State: Default + Send + Sync + 'static,
{
    /// Create a new router using `State::default()`.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RouterInner {
                state: Arc::new(State::default()),
                routes: HashMap::new(),
            }),
        }
    }
}

impl<State> Router<State>
where
    State: Send + Sync + 'static,
{
    /// Create a new router with the provided state.
    pub fn from_state(state: State) -> Self {
        Self {
            inner: Arc::new(RouterInner {
                state: Arc::new(state),
                routes: HashMap::new(),
            }),
        }
    }

    /// Replace the state while keeping existing routes.
    pub fn with_state(self, state: State) -> Self {
        let mut inner = self.into_inner();
        inner.state = Arc::new(state);
        Self {
            inner: Arc::new(inner),
        }
    }

    /// Register a command handler.
    ///
    /// The handler must be an async function or `Fn` that returns a `Send + 'static` future.
    pub fn route<H, Args>(self, command: &'static str, handler: H) -> Self
    where
        H: IntoHandler<State, Args>,
    {
        let mut inner = self.into_inner();
        inner
            .routes
            .insert(normalize_command_key(command), handler.into_handler());
        Self {
            inner: Arc::new(inner),
        }
    }

    pub(crate) fn state(&self) -> Arc<State> {
        Arc::clone(&self.inner.state)
    }

    pub(crate) fn call(&self, ctx: RequestContext) -> BoxFuture<Response> {
        let Some(handler) = self.inner.routes.get(&ctx.command.name_upper).cloned() else {
            return Box::pin(async move {
                RespError::invalid_data(format!(
                    "ERR unknown command '{}'",
                    display_command_name(&ctx.command.name)
                ))
                .into_response()
            });
        };
        handler.call(ctx, self.state())
    }

    fn into_inner(self) -> RouterInner<State> {
        Arc::try_unwrap(self.inner).unwrap_or_else(|arc| RouterInner {
            state: Arc::clone(&arc.state),
            routes: arc.routes.clone(),
        })
    }
}

fn normalize_command_key(command: &str) -> Bytes {
    let bytes = command.as_bytes();
    let mut needs = false;
    for &b in bytes {
        if b.is_ascii_lowercase() {
            needs = true;
            break;
        }
    }
    if !needs {
        return Bytes::copy_from_slice(command.as_bytes());
    }
    let mut buf = BytesMut::with_capacity(bytes.len());
    for &b in bytes {
        buf.put_u8(b.to_ascii_uppercase());
    }
    buf.freeze()
}

fn display_command_name(bytes: &Bytes) -> String {
    display_bytes(bytes)
}

fn display_bytes(bytes: &Bytes) -> String {
    match std::str::from_utf8(bytes) {
        Ok(s) => s.to_owned(),
        Err(_) => format!("0x{}", hex_bytes(bytes)),
    }
}

fn hex_bytes(bytes: &Bytes) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = String::with_capacity(bytes.len() * 2);
    for &b in bytes.iter() {
        out.push(HEX[(b >> 4) as usize] as char);
        out.push(HEX[(b & 0x0f) as usize] as char);
    }
    out
}

fn log_handler_start(ctx: &RequestContext) {
    if log::log_enabled!(log::Level::Debug) {
        let name = display_command_name(&ctx.command.name_upper);
        log::debug!(
            target: "handler",
            "start id={} cmd={} args={}",
            ctx.client_id,
            name,
            ctx.command.args.len()
        );
    }
}

fn log_handler_result(ctx: &RequestContext, response: &Response) {
    if !log::log_enabled!(log::Level::Debug) {
        return;
    }
    if let Value::Error(msg) = response {
        let name = display_command_name(&ctx.command.name_upper);
        let detail = display_bytes(msg);
        log::debug!(
            target: "handler",
            "error id={} cmd={} msg={}",
            ctx.client_id,
            name,
            detail
        );
    }
}

impl<State> FromRequest<State> for Cmd
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(Cmd(ctx.command.clone()))
    }
}

impl<T> FromRequest<T> for AppState<T>
where
    T: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        _ctx: &mut RequestContext,
        state: &Arc<T>,
    ) -> Result<Self, Self::Rejection> {
        Ok(AppState(Arc::clone(state)))
    }
}

impl<State> FromRequest<State> for PeerAddr
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(PeerAddr(ctx.peer_addr))
    }
}

impl<State> FromRequest<State> for LocalAddr
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(LocalAddr(ctx.local_addr))
    }
}

impl<State> FromRequest<State> for ClientId
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(ClientId(ctx.client_id))
    }
}

impl<State> FromRequest<State> for Extensions
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(ctx.extensions.clone())
    }
}

impl<State> FromRequest<State> for PushHandle
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(ctx.push.clone())
    }
}

impl<State> FromRequest<State> for PubSubHandle
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(ctx.pubsub.clone())
    }
}

impl<State> FromRequest<State> for Command
where
    State: Send + Sync + 'static,
{
    type Rejection = Infallible;

    async fn from_request(
        ctx: &mut RequestContext,
        _state: &Arc<State>,
    ) -> Result<Self, Self::Rejection> {
        Ok(ctx.command.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicUsize;

    use crate::Value;
    use bytes::Bytes;
    use tokio::sync::mpsc;

    fn make_ctx(cmd: Command) -> RequestContext {
        let (push_tx, _push_rx) = mpsc::channel(1);
        let (close_tx, _close_rx) = mpsc::channel(1);
        RequestContext {
            command: cmd,
            peer_addr: "127.0.0.1:1".parse().unwrap(),
            local_addr: "127.0.0.1:2".parse().unwrap(),
            client_id: 1,
            extensions: Extensions::default(),
            push: PushHandle::new(push_tx, close_tx),
            pubsub: PubSubHandle::new(Arc::new(AtomicUsize::new(0))),
        }
    }

    async fn ping() -> Value {
        Value::Simple(Bytes::from_static(b"PONG"))
    }

    #[tokio::test]
    async fn route_dispatches() {
        let app: Router<()> = Router::new().route("PING", ping);
        let cmd = Command::new(Bytes::from_static(b"PING"), Vec::new());
        let resp = app.call(make_ctx(cmd)).await;
        assert_eq!(resp, Value::Simple(Bytes::from_static(b"PONG")));
    }

    #[tokio::test]
    async fn unknown_command_returns_error() {
        let app: Router<()> = Router::new();
        let cmd = Command::new(Bytes::from_static(b"NOPE"), Vec::new());
        let resp = app.call(make_ctx(cmd)).await;
        assert!(matches!(resp, Value::Error(_)));
    }

    #[tokio::test]
    async fn route_accepts_capturing_closure() {
        let payload = Bytes::from_static(b"PONG");
        let handler = move || {
            let payload = payload.clone();
            async move { Value::Simple(payload) }
        };

        let app: Router<()> = Router::new().route("PING", handler);
        let cmd = Command::new(Bytes::from_static(b"PING"), Vec::new());
        let resp = app.call(make_ctx(cmd)).await;
        assert_eq!(resp, Value::Simple(Bytes::from_static(b"PONG")));
    }

    #[tokio::test]
    async fn state_extractor_works() {
        async fn handler(AppState(state): AppState<u64>) -> Value {
            Value::Integer(*state as i64)
        }

        let app = Router::from_state(5u64).route("GET", handler);
        let cmd = Command::new(Bytes::from_static(b"GET"), Vec::new());
        let resp = app.call(make_ctx(cmd)).await;
        assert_eq!(resp, Value::Integer(5));
    }
}