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
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
//! An experimental web framework based on functional generic programming.
#![feature(trait_alias, const_generics, type_alias_impl_trait)]
#![allow(incomplete_features, clippy::type_complexity)]
#![doc(test(
    no_crate_inject,
    attr(deny(rust_2018_idioms, unused_imports, unused_mut))
))]
#![warn(rust_2018_idioms, missing_docs)]

#[doc(inline)]
pub use frunk_core::{coproduct, hlist, Coprod, Hlist};

/// An attribute macro to reduce boilerplate when writing functions that accept an hlist as
/// an argument (handlers and middlewares).
///
/// This will translate the annotated function's argument list into an hlist. Any arguments
/// whose names start with underscores will be translated to their type, while all other
/// arguments will be translated to [named fields][field::Field] of their name and type.
///
/// # Examples
///
/// `fn f(a: u32, b: u32)` translates to `fn f(_: Hlist![f![a: u32], f![b: u32]])`.
///
/// `fn f(a: u32, _b: u32)` translates to `fn f(_: Hlist![f![a: u32], u32])`.
///
/// ```
/// use hyperbole::{f, hlist, record, record_args, Hlist};
///
/// #[record_args]
/// async fn my_func_a(first: u8, second: String, _third: Vec<u8>) {
///     // do stuff
/// }
///
/// async fn call_my_func_a() {
///     // the translated function can be called using record syntax:
///     my_func_a(record![first = 4, second = "test-string".to_owned(), [
///         vec![1, 2, 3]
///     ]])
///     .await;
///
///     // or desugared hlist syntax:
///     my_func_a(hlist![4.into(), "test-string".to_owned().into(), vec![
///         1, 2, 3
///     ]])
///     .await;
/// }
///
/// // 'my_func_a' above is translated to something like this:
/// async fn my_func_b(cx: Hlist![f![first: u8], f![second: String], Vec<u8>]) {
///     async fn my_func_b(first: u8, second: String, _third: Vec<u8>) {
///         // do stuff
///     }
///
///     my_func_b(
///         cx.head.into_inner(),
///         cx.tail.head.into_inner(),
///         cx.tail.tail.head,
///     )
///     .await
/// }
/// ```
pub use hyperbole_macros::record_args;

pub mod body;
mod combinators;
pub mod field;
mod handler;
pub mod mw;
pub mod reply;
pub mod test;
pub mod tree;

use combinators::{
    Add2, Base, End, Inject, Link, Map, MapErr, MapErrs, Path, Then, TryMap, TryThen,
};
use handler::{Chain, Handler, NotFound};
use reply::Reply;
use tree::{Cluster, Node, Params, Parser, PathSpec, Route, Segment};

use frunk_core::{
    coproduct::{CNil, Coproduct},
    hlist::{HList, HNil},
    indices::Here,
};
use futures::future::{ready, BoxFuture, FutureExt, NeverError, Ready};
use http::{Extensions, HeaderMap, HeaderValue, Uri, Version};
use hyper::{
    header::LOCATION, server::conn::AddrStream, service::Service, Body, Method, Request, StatusCode,
};
use std::{
    borrow::Cow,
    collections::HashMap,
    convert::Infallible,
    future::Future,
    net::SocketAddr,
    ops::{Add, Deref},
    sync::Arc,
    task::{Context, Poll},
};

/// An http response.
pub type Response = hyper::Response<Body>;

#[doc(hidden)]
#[derive(Clone)]
pub struct AppDispatch<I>(Arc<App<I>>);

impl<I> Service<&AddrStream> for AppDispatch<I> {
    type Response = AppService<I>;

    type Error = Infallible;

    type Future = NeverError<Ready<Self::Response>>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, tgt: &AddrStream) -> Self::Future {
        let svc = AppService {
            app: Arc::clone(&self.0),
            addr: tgt.remote_addr(),
        };

        ready(svc).never_error()
    }
}

#[doc(hidden)]
#[derive(Clone)]
pub struct AppService<I> {
    app: Arc<App<I>>,
    addr: SocketAddr,
}

impl<I> Deref for AppService<I> {
    type Target = App<I>;

    fn deref(&self) -> &Self::Target {
        &self.app
    }
}

impl<I: Sync + Send + Clone + 'static> Service<Request<Body>> for AppService<I> {
    type Response = Response;

    type Error = Infallible;

    type Future = NeverError<BoxFuture<'static, Response>>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        self.dispatch(req, self.addr).never_error()
    }
}

/// The set of request scoped state that contexts are initialized with.
///
/// The `I` type variable refers to the top-level state (if any) passed into [`App::new`].
pub type Init<I> = Hlist![
    Body,
    Method,
    Uri,
    Version,
    HeaderMap<HeaderValue>,
    Extensions,
    SocketAddr,
    ...I
];

/// Contains routes and handlers for a given http service, as well as top-level application
/// state.
///
/// # Examples
/// ```no_run
/// use hyper::{server::Server, Body};
/// use hyperbole::{path, record, App};
///
/// #[tokio::main]
/// async fn main() -> hyper::Result<()> {
///     let app = App::empty()
///         .context_path(path!["first" / param: u32])
///         .map(|cx: record![param]| cx)
///         // GET /first/:param/echo
///         .get(path!["echo"], |cx: record![param, [Body]]| async move {
///             let (param, body) = cx.into();
///             format!("param: {}, body: {:?}", param, body)
///         })
///         .collapse();
///
///     Server::bind(&([127, 0, 0, 1], 12345).into())
///         .serve(app.into_make_service())
///         .await
/// }
/// ```
pub struct App<I> {
    // top-level injected state
    state: I,
    // routes for common methods
    common: [Node<dyn Handler>; 9],
    // routes for custom methods
    custom: HashMap<Method, Node<dyn Handler>>,
    // 404 handler
    not_found: Box<dyn Handler>,
}

impl App<HNil> {
    /// Returns a new [App] without any top-level state.
    pub fn empty() -> Self {
        Self::new(HNil)
    }
}

impl<I: Sync + Send + Clone + 'static> App<I> {
    /// Returns a new [App] with the provided top-level state.
    ///
    /// The `state` passed here should be an hlist as constructed by [hlist!] or [record!],
    /// and will be cloned for each incoming request.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{access, hlist, record, App, Hlist};
    ///
    /// // equivalent to App::empty()
    /// let _app = App::new(hlist![]);
    /// let _app = App::new(record![]);
    ///
    /// // &'static str will be available in contexts
    /// let _app = App::new(hlist!["hello world"])
    ///     .context()
    ///     .map(|cx: Hlist![&str]| {
    ///         println!("str is {:?}", cx.get::<&str, _>());
    ///         cx
    ///     })
    ///     .map(|cx: Hlist![&'static str]| cx)
    ///     .collapse();
    ///
    /// // 'x: {integer}' will be available in contexts
    /// let _app = App::new(record![x = 40])
    ///     .context()
    ///     .map(|cx: record![x: u64]| {
    ///         println!("x is {}", access!(&cx.x));
    ///         cx
    ///     })
    ///     .collapse();
    /// ```
    pub fn new(state: I) -> Self
    where I: HList {
        Self {
            state,
            common: Default::default(),
            custom: HashMap::new(),
            not_found: Box::new(NotFound),
        }
    }

    /// Begin a new request context at the provided base path. Any parameters parsed from the
    /// uri will be merged into the context's state.
    ///
    /// The [path!] macro can be used to construct an appropriate [`PathSpec`].
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{path, App};
    ///
    /// let _app = App::empty()
    ///     // begin at /
    ///     .context_path(path![])
    ///     .collapse()
    ///     // begin at /abc
    ///     .context_path(path!["abc"])
    ///     .collapse()
    ///     // begin at /xyz/:param
    ///     .context_path(path!["xyz" / param: u32])
    ///     .collapse();
    /// ```
    pub fn context_path<P: Parser<Segment>>(
        self,
        spec: PathSpec<P>,
    ) -> Ctx<I, Params<P>, Path<Base, P, Here>>
    {
        let spec = PathSpec::ROOT.append(spec);
        let chain = Chain::new(self.state.clone(), spec).link_next(Path::new);

        Ctx { app: self, chain }
    }

    /// Begin a new request context at the root path.
    pub fn context(self) -> Ctx<I, Params<HNil>, Path<Base, HNil, Here>> {
        self.context_path(PathSpec::ROOT)
    }

    /// Configure a handler for the case where an incoming request does not match any existing
    /// routes.
    ///
    /// As in [Ctx::handle], the `handler` closure should accept an hlist argument, and return a
    /// future that evaluates to an http response (via the [Reply] trait).
    ///
    /// The argument hlist may consist of any subset of types from [Init].
    ///
    /// # Examples
    /// ```
    /// use hyper::{Method, StatusCode, Uri};
    /// use hyperbole::{record_args, reply::Reply, App};
    ///
    /// #[record_args]
    /// async fn handler(_m: Method, _u: Uri) -> impl Reply {
    ///     "not found".with_status(StatusCode::NOT_FOUND)
    /// }
    ///
    /// let _app = App::empty().not_found(handler);
    /// ```
    pub fn not_found<F, Args, Ix, Fut, Resp>(mut self, handler: F) -> Self
    where
        F: Fn(Args) -> Fut + Sync + Send + 'static,
        Fut: Future<Output = Resp> + Send + 'static,
        Resp: Reply + 'static,
        End<Base, F, Args, Ix>: Link<Init<I>, HNil, Output = Response, Params = HNil> + 'static,
    {
        let chain: Chain<I, HNil, End<Base, F, Args, Ix>> =
            Chain::new(self.state.clone(), path![]).link_next(|link| End::new(link, handler));

        self.not_found = Box::new(chain);
        self
    }

    /// Consume this [App], turning it into a `MakeService` compatible with hyper servers.
    ///
    /// See [`hyper::server::Builder::serve`] for usage details.
    pub fn into_make_service(self) -> AppDispatch<I> {
        AppDispatch(Arc::new(self))
    }

    fn get_node_mut(&mut self, method: Method) -> &mut Node<dyn Handler> {
        if let Some(i) = method_idx(&method) {
            &mut self.common[i]
        } else {
            self.custom.entry(method).or_default()
        }
    }

    fn lookup_route(&self, method: &Method, path: &str) -> Option<Route<'_, dyn Handler>> {
        method_idx(method)
            .map(|i| &self.common[i])
            .or_else(|| self.custom.get(method))
            .map(|n| n.lookup(path))
    }

    fn dispatch(&self, req: Request<Body>, addr: SocketAddr) -> BoxFuture<'static, Response> {
        #[inline]
        fn swap_trailing_slash(path: &str) -> Cow<'_, str> {
            if let Some(stripped) = path.strip_suffix('/') {
                return Cow::Borrowed(stripped);
            }

            let mut s = String::with_capacity(path.len() + 1);
            s.push_str(path);
            s.push('/');
            Cow::Owned(s)
        }

        let method = req.method();
        let path = req.uri().path();

        match self.lookup_route(method, path) {
            Some(Route { entry: Some(h), .. }) => h.handle(req, addr),

            Some(Route { tsr: true, .. }) if method != Method::CONNECT && path != "/" => {
                let code = if let Method::GET = *method {
                    StatusCode::MOVED_PERMANENTLY
                } else {
                    StatusCode::PERMANENT_REDIRECT
                };

                let resp = hyper::Response::builder()
                    .status(code)
                    .header(LOCATION, &*swap_trailing_slash(path))
                    .body(Body::empty())
                    .unwrap();

                Box::pin(async { resp })
            }

            _ => self.not_found.handle(req, addr),
        }
    }

    /// Create a test client for this app.
    #[inline]
    pub fn test_client(self) -> test::Client<I> {
        test::Client { app: self }
    }
}

#[inline]
fn method_idx(m: &Method) -> Option<usize> {
    Some(match *m {
        Method::GET => 0,
        Method::POST => 1,
        Method::PUT => 2,
        Method::DELETE => 3,
        Method::HEAD => 4,
        Method::OPTIONS => 5,
        Method::CONNECT => 6,
        Method::PATCH => 7,
        Method::TRACE => 8,
        _ => return None,
    })
}

/// A request processing context.
///
/// It can be thought of as a composable chain of partial transformations over an hlist of
/// request scoped values; a lazily declarative specification of 'how to process a request'.
///
/// Much like [Iterator], a context by itself doesn't really *do* anything (it is dropped by
/// the time requests are being processed). It exists only to construct request handlers for
/// registration as routes in an [App].
///
/// Each context tracks three hlists and a combinator chain that transforms them:
///
/// 1. An hlist provided at [App] initialization time (via [`App::new`] or [`App::empty`]).
/// 2. An hlist generated from incoming http request parts.
/// 2. An hlist of parameters parsed from request uris.
///
/// See [Init] for the exact set of initial state available in fresh contexts.
///
/// When a request is matched, these are all merged and fed into the chain of combinators to
/// be transformed before being passed into an eventual handler.
///
/// Combinators may move arbitrary subsets of the context's request scoped state, and return
/// arbitrary sets of state to merge for later combinators to use. In this way, the data
/// dependencies between middlewares / handlers can be expressed at compile time, without
/// introducing unnecessary coupling between components that are logically unrelated.
///
/// ```
/// use hyper::Body;
/// use hyperbole::{hlist, App, Hlist};
///
/// #[derive(Copy, Clone)]
/// struct DbHandle;
///
/// struct A;
/// struct B;
///
/// let _ctx = App::new(hlist![DbHandle]) // top-level state can be injected here
///     .context()
///     // move nothing, and return nothing to be merged
///     .map(|cx: Hlist![]| cx)
///     // move req body, but merge it back
///     .map(|cx: Hlist![Body]| cx)
///     // move req body, and merge an A and B
///     .map(|cx: Hlist![Body]| hlist![A, B])
///     // subset ordering doesn't matter
///     .map(|cx: Hlist![B, DbHandle, A]| cx)
///     .then(|cx: Hlist![A]| async { cx })
///     .map(|cx: Hlist![A, B]| cx)
///     .then(|cx: Hlist![B]| async { cx });
/// ```
///
/// At any point in a context chain, a handler can be registered by calling [handle] or one
/// of its analogues (ex: [get], [post]). Like middleware combinators, handlers move a subset
/// of the request scoped state into themselves, but instead of returning more state to merge
/// they return a value that implements [Reply].
///
/// ```
/// use hyper::Body;
/// use hyperbole::{access, path, record, record_args, App, Hlist};
///
/// let _ctx = App::empty()
///     .context_path(path![dynamic: u32])
///     // GET /:dynamic/echo_req
///     .get(path!["echo_req"], |cx: Hlist![Body]| async move {
///         // hlists can be converted into tuples
///         let (body,) = cx.into();
///         format!("echo: {:?}", body)
///     })
///     // GET /:dynamic/tell_dynamic
///     .get(path!["tell_dynamic"], |cx: record![dynamic]| async move {
///         // or use the access! macro for named field accesses
///         format!("dynamic is {}", access!(&cx.dynamic))
///     })
///     .path(path!["all" / "the" / "subpaths"])
///     // GET /:dynamic/all/the/subpaths/discrete
///     .get(path!["discrete"], fn_handler);
///
/// // or use the record_args attribute to reduce boilerplate
/// #[record_args]
/// async fn fn_handler(dynamic: u32, _body: Body) -> &'static [u8] {
///     println!("dynamic: {}", dynamic);
///     println!("reqbody: {:?}", _body);
///
///     b"here's an octet-stream"
/// }
/// ```
///
/// # Error Handling
/// Errors that arise during request resolution are represented in the context as [coproducts]
/// (a generalization of enums). When a fallible middleware is used ([try_map] or [try_then]),
/// an additional error variant is added to the context's error coproduct. This also applies
/// to parse errors of dynamic path parameters (which are wrapped in a [tree::UriError]).
///
/// If a fallible middleware returns `Err`, the request being processed short circuits and cannot
/// be recovered. The specific error response returned to the client can however be modified by
/// [map_errs] or [map_err]. The former transforms the complete error coproduct, while the latter
/// maps over a single variant.
///
/// Much like with request scoped state, any referenced errors in a [map_errs] or [map_err] must
/// appear in some fallible combinator. This is enforced at compile time.
///
/// ```
/// use hyper::StatusCode;
/// use hyperbole::{
///     body::{jsonr, JsonBodyError},
///     record,
///     reply::Reply,
///     App, Coprod,
/// };
///
/// let _app = App::empty()
///     .context()
///     // attempt to parse the body as a json object:
///     .try_then(jsonr::<record![x: u32, y: String]>)
///     // if the above fails, we can adjust the error with map_err:
///     .map_err(|err: JsonBodyError| err)
///     // or we can adjust all possible errors with map_errs:
///     .map_errs(|errs| {
///         let code = StatusCode::INTERNAL_SERVER_ERROR;
///         let err = format!("{:?}", errs).with_status(code);
///
///         // return type must be a coproduct as well
///         <Coprod![_]>::inject(err)
///     })
///     .collapse();
/// ```
///
/// # Limitations
/// Due to the extensive use of type inference to extract subsets of the request scoped state,
/// the state may not contain duplicate instances of a type. That is, it is a *set*, and not
/// a *list*. This property arises without explicit enforcement; type inference will simply
/// begin failing if duplicate types are encountered.
///
/// ```compile_fail,E0282
/// use hyperbole::{hlist, App, Hlist};
///
/// struct A(u32);
///
/// let _app = App::empty()
///     .context()
///     // merge an A
///     .map(|cx: Hlist![]| hlist![A(1)])
///     // merge an A (state now contains two `A`s)
///     .map(|cx: Hlist![]| hlist![A(2)])
///     // this fails during type inference, because it's ambiguous _which_ A we want
///     //
///     // error[E0282]: type annotations needed
///     //     cannot infer type for type parameter `TailIndex`
///     .map(|cx: Hlist![A]| cx)
///     .collapse();
/// ```
///
/// [Named fields][field::Field] can be used to disambiguate between what would otherwise be
/// duplicate types. [path!] in particular takes advantage of this to allow multiple instances
/// of common primitive types like `u32` or `String` to be extracted from a uri.
///
/// The above example can be rewritten using named fields to avoid the inference failure:
///
/// ```
/// use hyperbole::{record, App};
///
/// struct A(u32);
///
/// let _app = App::empty()
///     .context()
///     .map(|cx: record![]| record![first = A(1)])
///     .map(|cx: record![]| record![second = A(2)])
///     // we want the A called 'second'
///     .map(|cx: record![second]| cx)
///     // we want the A called 'first'
///     .map(|cx: record![first]| cx)
///     // we want both of them
///     .map(|cx: record![first, second]| cx)
///     .collapse();
/// ```
///
/// [handle]: Ctx::handle
/// [get]: Ctx::get
/// [post]: Ctx::post
/// [coproducts]: coproduct
/// [try_map]: Ctx::try_map
/// [try_then]: Ctx::try_then
/// [map_errs]: Ctx::map_errs
/// [map_err]: Ctx::map_err
pub struct Ctx<I, P, L> {
    app: App<I>,
    chain: Chain<I, P, L>,
}

macro_rules! handle {
    ($( [$name:ident, $method:path] ),+) => {
        $(handle! {@withdoc
            concat!(
                "A convenience method to call [handle][Ctx::handle] with ",
                stringify!([$method].),
            ),
            $name,
            $method,
        })+
    };

    (@withdoc $desc:expr, $name:ident, $method:path $(,)?) => {
        #[doc = $desc]
        pub fn $name<_P, F, Args, Ix, Pix, Fut, Resp>(self, spec: PathSpec<_P>, handler: F) -> Self
        where
            F: Fn(Args) -> Fut + Sync + Send,
            Fut: Future<Output = Resp> + Send,
            Resp: Reply ,
            (): CtxState2<L, I, P, _P, Pix, F, Args, Ix>,
        {
            self.handle($method, spec, handler)
        }
    };
}

macro_rules! handle_with {
    ($( [$name:ident, $method:path] ),+) => {
        $(handle_with! {@withdoc
            concat!(
                "A convenience method to call [handle_with][Ctx::handle_with] with ",
                stringify!([$method].),
            ),
            $name,
            $method,
        })+
    };

    (@withdoc $desc:expr, $name:ident, $method:path $(,)?) => {
        #[doc = $desc]
        pub fn $name<_P, Pix, W, WArgs, WFut, Merge, E, Wix, F, Args, Fut, Resp, Ix>(
            self,
            spec: PathSpec<_P>,
            with: W,
            handler: F,
        ) -> Self
        where
            W: Fn(WArgs) -> WFut + Sync + Send,
            WFut: Future<Output = Result<Merge, E>> + Send,
            E: Reply,

            F: Fn(Args) -> Fut + Sync + Send,
            Fut: Future<Output = Resp> + Send,
            Resp: Reply,

            (): CtxState3<L, I, P, _P, Pix, W, WArgs, Wix, F, Args, Ix>,
        {
            self.handle_with($method, spec, with, handler)
        }
    };
}

impl<I: 'static, P: 'static, L: 'static> Ctx<I, P, L>
where
    I: Sync + Send + Clone,
    L: Sync + Send + Clone,
{
    fn link_next<Ln, F: FnOnce(L) -> Ln>(self, wrap: F) -> Ctx<I, P, Ln> {
        Ctx {
            app: self.app,
            chain: self.chain.link_next(wrap),
        }
    }

    /// Inject a cloneable value into the request scoped state.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{f, hlist, record, App, Hlist};
    ///
    /// let _ctx = App::empty()
    ///     .context()
    ///     .inject("just an &str")
    ///     .map(|cx: Hlist![&str]| hlist![])
    ///     .inject(f![xyz = "this is a named field"])
    ///     .map(|cx: record![xyz]| hlist![]);
    /// ```
    pub fn inject<T: Clone>(self, value: T) -> Ctx<I, P, Inject<L, T>>
    where Inject<L, T>: Link<Init<I>, P> {
        self.link_next(|link| Inject::new(link, value))
    }

    /// Transform a subset of the request scoped state with a closure.
    ///
    /// The provided closure should accept an hlist argument, and return an hlist.
    ///
    /// The argument hlist may consist of any subset of types that are present within the
    /// context's state up to this point. Each element will be removed from the context's
    /// state and *moved* into `f` upon execution (making them inaccessible to subsequent
    /// middlewares and handlers).
    ///
    /// Likewise, any types in the returned hlist will be *moved* into the context's state.
    ///
    /// # Examples
    /// ```
    /// use hyper::Body;
    /// use hyperbole::{hlist, record_args, App, Hlist};
    ///
    /// #[record_args]
    /// fn fun(_: Body, _: u32) -> Hlist![] {
    ///     hlist![]
    /// }
    ///
    /// let _ctx = App::empty()
    ///     .context()
    ///     .map(|cx: Hlist![Body]| cx)
    ///     .map(|cx: Hlist![]| hlist![12345])
    ///     .map(fun);
    /// ```
    pub fn map<F, Args, Ix, Merge>(self, f: F) -> Ctx<I, P, Map<L, F, Args, Ix>>
    where
        F: Fn(Args) -> Merge,
        Merge: HList,
        Map<L, F, Args, Ix>: Link<Init<I>, P>,
    {
        self.link_next(|link| Map::new(link, f))
    }

    /// Transform a subset of the request scoped state with a fallible closure.
    ///
    /// The provided closure should accept an hlist argument, and return an hlist in a [Result]
    /// (where the error type implements [Reply]).
    ///
    /// The argument hlist may consist of any subset of types that are present within the
    /// context's state up to this point. Each element will be removed from the context's
    /// state and *moved* into `f` upon execution (making them inaccessible to subsequent
    /// middlewares and handlers).
    ///
    /// If the closure returns `Ok`, any types in the returned hlist will be *moved* into the
    /// context's state.
    ///
    /// If the closure returns `Err`, the request will short circuit with a response created
    /// via the error's [Reply] implementation.
    ///
    /// For subsequent combinators, the context's error type will contain an additional variant
    /// for `E`.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{access, path, record, App};
    ///
    /// let _ctx = App::empty()
    ///     .context_path(path![a: u32 / b: u32])
    ///     .try_map(|cx: record![a, b]| match access!(&cx.a) > access!(&cx.b) {
    ///         false => Err("uh oh"),
    ///         true => Ok(cx),
    ///     })
    ///     .map_err(|e: &str| "e is the above error, if it happened");
    /// ```
    pub fn try_map<F, Args, Ix, Merge, E>(self, f: F) -> Ctx<I, P, TryMap<L, F, Args, Ix>>
    where
        F: Fn(Args) -> Result<Merge, E>,
        Merge: HList,
        E: Reply,
        TryMap<L, F, Args, Ix>: Link<Init<I>, P>,
    {
        self.link_next(|link| TryMap::new(link, f))
    }

    /// Transform a subset of the request scoped state with a closure that returns a
    /// future.
    ///
    /// The provided closure should accept an hlist argument, and return a future that
    /// evaluates to an hlist.
    ///
    /// The argument hlist may consist of any subset of types that are present within the
    /// context's state up to this point. Each element will be removed from the context's
    /// state and *moved* into `f` upon execution (making them inaccessible to subsequent
    /// middlewares and handlers).
    ///
    /// Likewise, any types in the returned hlist will be *moved* into the context's state.
    ///
    /// # Examples
    /// ```
    /// use hyper::Body;
    /// use hyperbole::{hlist, record_args, App, Hlist};
    ///
    /// #[record_args]
    /// async fn fun(_: Body) -> Hlist![] {
    ///     hlist![]
    /// }
    ///
    /// let _ctx = App::empty()
    ///     .context()
    ///     .then(|cx: Hlist![Body]| async move { cx })
    ///     .then(|cx: Hlist![]| async move { cx })
    ///     .then(fun);
    /// ```
    pub fn then<F, Args, Ix, Fut, Merge>(self, f: F) -> Ctx<I, P, Then<L, F, Args, Ix>>
    where
        F: Fn(Args) -> Fut,
        Fut: Future<Output = Merge>,
        Merge: HList,
        Then<L, F, Args, Ix>: Link<Init<I>, P>,
    {
        self.link_next(|link| Then::new(link, f))
    }

    /// Transform a subset of the request scoped state with a closure that returns a fallible
    /// future.
    ///
    /// The provided closure should accept an hlist argument, and return a future that evaluates
    /// to an hlist in a [Result] (where the error type implements [Reply]).
    ///
    /// The argument hlist may consist of any subset of types that are present within the
    /// context's state up to this point. Each element will be removed from the context's
    /// state and *moved* into `f` upon execution (making them inaccessible to subsequent
    /// middlewares and handlers).
    ///
    /// If the future evaluates to `Ok`, any types in the returned hlist will be *moved* into
    /// the context's state.
    ///
    /// If the future evaluates to `Err`, the request will short circuit with a response created
    /// via the error's [Reply] implementation.
    ///
    /// For subsequent combinators, the context's error type will contain an additional variant
    /// for `E`.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{path, record, App};
    ///
    /// let _ctx = App::empty()
    ///     .context_path(path![a: f64 / b: String])
    ///     .try_then(|cx: record![a, b]| async move {
    ///         let (a, b) = cx.into();
    ///         if *a == 3.14159265 && *b != "blue" {
    ///             Err("always blue!")
    ///         } else {
    ///             Ok(record![color = "it was blue!"])
    ///         }
    ///     })
    ///     .map(|cx: record![color]| cx);
    /// ```
    pub fn try_then<F, Args, Ix, Fut, Merge, E>(self, f: F) -> Ctx<I, P, TryThen<L, F, Args, Ix>>
    where
        F: Fn(Args) -> Fut,
        Fut: Future<Output = Result<Merge, E>>,
        Merge: HList,
        E: Reply,
        TryThen<L, F, Args, Ix>: Link<Init<I>, P>,
    {
        self.link_next(|link| TryThen::new(link, f))
    }

    /// Transform the context's error type with a closure.
    ///
    /// The error will be a [Coproduct] with a variant for all potential error cases so far.
    ///
    /// Any [Coproduct] may be returned, so long as any variants it contains all implement
    /// [Reply].
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{record, App, Coprod};
    ///
    /// let _ctx = App::empty()
    ///     .context()
    ///     // without any fallible combinators, the error is an uninhabitable enum:
    ///     .map_errs(|err: Coprod![]| -> Coprod![] { match err {} })
    ///     .map(|cx: record![]| cx)
    ///     .collapse();
    /// ```
    pub fn map_errs<F, E>(self, f: F) -> Ctx<I, P, MapErrs<L, F>>
    where
        F: Fn(<L as Link<Init<I>, P>>::Error) -> E,
        E: IsCoproduct + Reply,
        L: Link<Init<I>, P>,
        MapErrs<L, F>: Link<Init<I>, P>,
    {
        self.link_next(|link| MapErrs::new(link, f))
    }

    /// Transform a single variant of the context's error type with a closure.
    ///
    /// This can be used to selectively modify only a single type of error. Note that if more
    /// than one instance of the same error type may have occurred, this will only affect the
    /// most recent of them.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{record, record_args, App};
    ///
    /// fn fallible_a(_: record![]) -> Result<record![], String> {
    ///     Err("uh oh".to_owned())
    /// }
    ///
    /// #[record_args]
    /// fn fallible_b() -> Result<record![], Vec<u8>> {
    ///     Err(b"uh oh".to_vec())
    /// }
    ///
    /// let _app = App::empty()
    ///     .context()
    ///     .try_map(fallible_a)
    ///     .try_map(fallible_b)
    ///     .map_err(|e: String| "it was String")
    ///     .map_err(|e: Vec<u8>| "it was Vec<u8>")
    ///     .collapse();
    /// ```
    pub fn map_err<F, E, Ix, R>(self, f: F) -> Ctx<I, P, MapErr<L, F, E, Ix>>
    where
        F: Fn(E) -> R,
        R: Reply,
        MapErr<L, F, E, Ix>: Link<Init<I>, P>,
    {
        self.link_next(|link| MapErr::new(link, f))
    }

    /// Append additional path segments to this context's base path. Any new parameters parsed
    /// from the uri will be merged into the context's state at this point.
    ///
    /// The [path!] macro can be used to construct an appropriate [`PathSpec`].
    ///
    /// When a request is being handled, the concatenated path specification is parsed before
    /// any middlewares execute. However, all extracted parameters (and parsing errors) are
    /// deferred such that they only appear at the point where they were specified.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{path, record, tree::UriError, App};
    /// use std::num::ParseFloatError;
    ///
    /// let _ctx = App::empty()
    ///     .context()
    ///     .path(path!["first" / x: usize / y: f64])
    ///     .map(|cx: record![x]| cx)
    ///     .map_err(|e: UriError<ParseFloatError>| e.item)
    ///     .map(|cx: record![y]| cx)
    ///     .map(|cx: record![x, y]| cx)
    ///     // GET /first/:x/:y/abc
    ///     .get(path!["abc"], |cx: record![x, y]| async { "" });
    /// ```
    pub fn path<_P, Ix>(self, spec: PathSpec<_P>) -> Ctx<I, Add2<P, Params<_P>>, Path<L, _P, Ix>>
    where
        P: Add<Params<_P>>,
        _P: Parser<Segment>,
        Path<L, _P, Ix>: Link<Init<I>, Add2<P, Params<_P>>>,
    {
        Ctx {
            app: self.app,
            chain: self.chain.add_path(spec).link_next(Path::new),
        }
    }

    /// Register a request handler for this context's base path with `spec` appended to it.
    ///
    /// The provided `handler` closure should accept an hlist argument, and return a future
    /// that evaluates to an http response (via the [Reply] trait).
    ///
    /// The argument hlist may consist of any subset of types that are present within the
    /// context's state up to this point, or any parameters parsed from the provided path
    /// `spec`.
    ///
    /// If an incoming request matches this route, every middleware accumulated in the context
    /// up to this point will execute. Assuming none of them short circuit with an error, this
    /// handler will then be executed.
    ///
    /// # Examples
    /// ```
    /// use hyper::Body;
    /// use hyperbole::{hlist, path, record, record_args, reply::Reply, App, Hlist};
    ///
    /// #[record_args]
    /// async fn doit(baz: f64) -> &'static str {
    ///     "&'static str implements Reply"
    /// }
    ///
    /// async fn more(cx: Hlist![Body, u32]) -> &'static [u8] {
    ///     b"so does &'static [u8]"
    /// }
    ///
    /// async fn using_impl(cx: Hlist![]) -> impl Reply {
    ///     vec![1, 2, 3, 4, 5]
    /// }
    ///
    /// let _ctx = App::empty()
    ///     .context_path(path!["foo" / "bar" / baz: f64])
    ///     .get(path!["doit"], doit)
    ///     .map(|cx: record![baz]| hlist![15])
    ///     .get(path!["more" / neat: u32], more)
    ///     .get(path!["more"], more)
    ///     .get(path!["more" / neat: u32 / "nested"], more)
    ///     .get(path!["impl_reply"], using_impl);
    /// ```
    ///
    /// # Panics
    /// This method will panic if the complete path conflicts with any other route registered
    /// under the same http method.
    ///
    /// ```should_panic
    /// use hyperbole::{path, record, App};
    ///
    /// // 'a handler is already registered for path "/conflict"'
    /// let _ctx = App::empty()
    ///     .context()
    ///     .get(path!["conflict"], |_: record![]| async { "" })
    ///     .get(path!["conflict"], |_: record![]| async { "" });
    /// ```
    ///
    /// ```should_panic
    /// use hyperbole::{path, record, App};
    ///
    /// // 'wildcard ":param" conflicts with existing children in path "/:param"'
    /// let _ctx = App::empty()
    ///     .context()
    ///     .get(path!["something"], |_: record![]| async { "" })
    ///     .get(path![param: u32], |_: record![]| async { "" });
    /// ```
    pub fn handle<_P, F, Args, Ix, Pix, Fut, Resp>(
        mut self,
        method: Method,
        spec: PathSpec<_P>,
        handler: F,
    ) -> Self
    where
        F: Fn(Args) -> Fut + Sync + Send,
        Fut: Future<Output = Resp> + Send,
        Resp: Reply,
        (): CtxState2<L, I, P, _P, Pix, F, Args, Ix>,
    {
        let chain = (self.chain.clone())
            .add_path(spec)
            .link_next(|link| -> Path<_, _P, Pix> { Path::new(link) })
            .link_next(|link| -> End<_, F, Args, Ix> { End::new(link, handler) });

        let path = format!("{}", chain.path());

        self.app.get_node_mut(method).insert(&path, Box::new(chain));

        self
    }

    handle!(
        [get, Method::GET],
        [post, Method::POST],
        [put, Method::PUT],
        [patch, Method::PATCH],
        [delete, Method::DELETE]
    );

    /// Register a request handler for this context's base path with `spec` appended to it.
    ///
    /// The semantics of this are mostly equivalent to [handle], except for an additional `with`
    /// argument, which should be a closure that could be passed to [try_then]. It will behave
    /// as if added between the path combinator and handler (that is, it has access to any new
    /// types introduced in `spec`, and merges types accessible to `handler`).
    ///
    /// This is useful for specifying a different request body parser for multiple handlers
    /// that otherwise share the same chain of middlewares.
    ///
    /// # Examples
    /// ```
    /// use hyperbole::{body::jsonr, path, record, App};
    ///
    /// async fn handle_abc(cx: record![a: u32, b: String, c: f64]) -> &'static str {
    ///     "neat"
    /// }
    ///
    /// let _app = App::empty()
    ///     .context()
    ///     .get_with(path![a: u32], jsonr::<record![b, c]>, handle_abc)
    ///     .collapse();
    /// ```
    ///
    /// # Panics
    /// This method will panic if the complete path conflicts with any other route registered
    /// under the same http method.
    ///
    /// ```should_panic
    /// use hyperbole::{path, record, App};
    /// use std::convert::Infallible;
    ///
    /// async fn noop(cx: record![]) -> Result<record![], Infallible> {
    ///     Ok(cx)
    /// }
    ///
    /// // 'a handler is already registered for path "/conflict"'
    /// let _ctx = App::empty()
    ///     .context()
    ///     .get_with(path!["conflict"], noop, |_: record![]| async { "" })
    ///     .get_with(path!["conflict"], noop, |_: record![]| async { "" });
    /// ```
    ///
    /// ```should_panic
    /// use hyperbole::{path, record, App};
    /// use std::convert::Infallible;
    ///
    /// async fn noop(cx: record![]) -> Result<record![], Infallible> {
    ///     Ok(cx)
    /// }
    ///
    /// // 'wildcard ":param" conflicts with existing children in path "/:param"'
    /// let _ctx = App::empty()
    ///     .context()
    ///     .get_with(path!["something"], noop, |_: record![]| async { "" })
    ///     .get_with(path![param: u32], noop, |_: record![]| async { "" });
    /// ```
    ///
    /// [handle]: Ctx::handle
    /// [try_then]: Ctx::try_then
    pub fn handle_with<_P, Pix, W, WArgs, WFut, Merge, E, Wix, F, Args, Fut, Resp, Ix>(
        mut self,
        method: Method,
        spec: PathSpec<_P>,
        with: W,
        handler: F,
    ) -> Self
    where
        W: Fn(WArgs) -> WFut + Sync + Send,
        WFut: Future<Output = Result<Merge, E>> + Send,
        E: Reply,

        F: Fn(Args) -> Fut + Sync + Send,
        Fut: Future<Output = Resp> + Send,
        Resp: Reply,

        (): CtxState3<L, I, P, _P, Pix, W, WArgs, Wix, F, Args, Ix>,
    {
        let chain = (self.chain.clone())
            .add_path(spec)
            .link_next(|link| -> Path<_, _P, Pix> { Path::new(link) })
            .link_next(|link| -> TryThen<_, W, WArgs, Wix> { TryThen::new(link, with) })
            .link_next(|link| -> End<_, F, Args, Ix> { End::new(link, handler) });

        let path = format!("{}", chain.path());

        self.app.get_node_mut(method).insert(&path, Box::new(chain));

        self
    }

    handle_with!(
        [get_with, Method::GET],
        [post_with, Method::POST],
        [put_with, Method::PUT],
        [patch_with, Method::PATCH],
        [delete_with, Method::DELETE]
    );

    /// Collapse this context and return to the base [App].
    ///
    /// This discards any accumulated combinators and path specifications, while retaining
    /// handlers registered via [handle] (or helpers like [get], [post], etc).
    ///
    /// [handle]: Ctx::handle
    /// [get]: Ctx::get
    /// [post]: Ctx::post
    pub fn collapse(self) -> App<I> {
        self.app
    }
}

#[doc(hidden)]
pub trait CtxState2<L, I, P, _P, Pix, F, Args, Ix> = where
    P: Add<Params<_P>>,
    _P: Parser<Segment>,
    Add2<P, Params<_P>>: Parser<Cluster>,
    End<Path<L, _P, Pix>, F, Args, Ix>:
        Link<Init<I>, Add2<P, Params<_P>>, Output = Response, Params = HNil> + 'static;

#[doc(hidden)]
pub trait CtxState3<L, I, P, _P, Pix, W, WArgs, Wix, F, Args, Ix> = where
    P: Add<Params<_P>>,
    _P: Parser<Segment>,
    Add2<P, Params<_P>>: Parser<Cluster>,
    End<TryThen<Path<L, _P, Pix>, W, WArgs, Wix>, F, Args, Ix>:
        Link<Init<I>, Add2<P, Params<_P>>, Output = Response, Params = HNil> + 'static;

mod sealed {
    pub trait Seal {}
}

/// Types that represent coproducts.
pub trait IsCoproduct: sealed::Seal {}

impl sealed::Seal for CNil {}
impl IsCoproduct for CNil {}

impl<H, T: sealed::Seal> sealed::Seal for Coproduct<H, T> {}
impl<H, T: IsCoproduct> IsCoproduct for Coproduct<H, T> {}