http-nu 0.15.0

The surprisingly performant, Nushell-scriptable, cross.stream-powered, Datastar-ready HTTP server that fits in your back pocket.
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
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
use std::collections::HashMap;
use std::io::{self, Write};
use std::pin::Pin;
use std::sync::OnceLock;
use std::task::{Context, Poll};
use std::time::Instant;

use chrono::Local;
use crossterm::{cursor, execute, terminal};
use hyper::body::{Body, Bytes, Frame, SizeHint};
use hyper::header::HeaderMap;
use serde::Serialize;
use tokio::sync::broadcast;

use crate::request::Request;

type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// Datastar SDK version (matches CDN URL in stdlib)
pub const DATASTAR_VERSION: &str = "1.0-RC.8";

/// Startup options to display in preamble
#[derive(Clone, Default)]
pub struct StartupOptions {
    pub watch: bool,
    pub tls: Option<String>,
    pub store: Option<String>,
    pub topic: Option<String>,
    pub expose: Option<String>,
    pub services: bool,
}

// --- Token bucket rate limiter ---

struct TokenBucket {
    tokens: f64,
    capacity: f64,
    refill_rate: f64,
    last_refill: Instant,
}

impl TokenBucket {
    fn new(capacity: f64, refill_rate: f64, now: Instant) -> Self {
        Self {
            tokens: capacity,
            capacity,
            refill_rate,
            last_refill: now,
        }
    }

    fn try_consume(&mut self, now: Instant) -> bool {
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.refill_rate).min(self.capacity);
        self.last_refill = now;

        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            false
        }
    }
}

// --- Event enum: owned data for async broadcast ---

#[derive(Clone, Serialize)]
pub struct RequestData {
    pub proto: String,
    pub method: String,
    pub remote_ip: Option<String>,
    pub remote_port: Option<u16>,
    pub trusted_ip: Option<String>,
    pub headers: HashMap<String, String>,
    pub uri: String,
    pub path: String,
    pub query: HashMap<String, String>,
}

impl From<&Request> for RequestData {
    fn from(req: &Request) -> Self {
        Self {
            proto: req.proto.to_string(),
            method: req.method.to_string(),
            remote_ip: req.remote_ip.map(|ip| ip.to_string()),
            remote_port: req.remote_port,
            trusted_ip: req.trusted_ip.map(|ip| ip.to_string()),
            headers: req
                .headers
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
                .collect(),
            uri: req.uri.to_string(),
            path: req.path.clone(),
            query: req.query.clone(),
        }
    }
}

#[derive(Clone)]
pub enum Event {
    // Access events
    Request {
        request_id: scru128::Scru128Id,
        request: Box<RequestData>,
    },
    Response {
        request_id: scru128::Scru128Id,
        status: u16,
        headers: HashMap<String, String>,
        latency_ms: u64,
    },
    Complete {
        request_id: scru128::Scru128Id,
        bytes: u64,
        duration_ms: u64,
    },

    // Lifecycle events
    Started {
        address: String,
        startup_ms: u64,
        options: StartupOptions,
    },
    Reloaded,
    Error {
        error: String,
    },
    Print {
        message: String,
    },
    Stopping {
        inflight: usize,
    },
    Stopped,
    StopTimedOut,
    Shutdown,
}

// --- Broadcast channel ---

static SENDER: OnceLock<broadcast::Sender<Event>> = OnceLock::new();

pub fn init_broadcast() -> broadcast::Receiver<Event> {
    let (tx, rx) = broadcast::channel(65536);
    let _ = SENDER.set(tx);
    rx
}

pub fn subscribe() -> Option<broadcast::Receiver<Event>> {
    SENDER.get().map(|tx| tx.subscribe())
}

fn emit(event: Event) {
    if let Some(tx) = SENDER.get() {
        let _ = tx.send(event); // non-blocking, drops if no receivers
    }
}

// --- Public emit functions ---

pub fn log_request(request_id: scru128::Scru128Id, request: &Request) {
    emit(Event::Request {
        request_id,
        request: Box::new(RequestData::from(request)),
    });
}

pub fn log_response(
    request_id: scru128::Scru128Id,
    status: u16,
    headers: &HeaderMap,
    start_time: Instant,
) {
    let headers_map: HashMap<String, String> = headers
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
        .collect();

    emit(Event::Response {
        request_id,
        status,
        headers: headers_map,
        latency_ms: start_time.elapsed().as_millis() as u64,
    });
}

pub fn log_complete(request_id: scru128::Scru128Id, bytes: u64, response_time: Instant) {
    emit(Event::Complete {
        request_id,
        bytes,
        duration_ms: response_time.elapsed().as_millis() as u64,
    });
}

pub fn log_started(address: &str, startup_ms: u128, options: StartupOptions) {
    emit(Event::Started {
        address: address.to_string(),
        startup_ms: startup_ms as u64,
        options,
    });
}

pub fn log_reloaded() {
    emit(Event::Reloaded);
}

pub fn log_error(error: &str) {
    emit(Event::Error {
        error: error.to_string(),
    });
}

pub fn log_print(message: &str) {
    emit(Event::Print {
        message: message.to_string(),
    });
}

pub fn log_stopping(inflight: usize) {
    emit(Event::Stopping { inflight });
}

pub fn log_stopped() {
    emit(Event::Stopped);
}

pub fn log_stop_timed_out() {
    emit(Event::StopTimedOut);
}

pub fn shutdown() {
    emit(Event::Shutdown);
}

// --- JSONL handler (dedicated writer thread does serialization + IO) ---

pub fn run_jsonl_handler(rx: broadcast::Receiver<Event>) -> std::thread::JoinHandle<()> {
    use std::io::Write;

    std::thread::spawn(move || {
        let mut rx = rx;
        let mut stdout = std::io::BufWriter::new(std::io::stdout().lock());

        loop {
            let event = match rx.blocking_recv() {
                Ok(event) => event,
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    let json = serde_json::json!({
                        "stamp": scru128::new().to_string(),
                        "message": "lagged",
                        "dropped": n,
                    });
                    if let Ok(line) = serde_json::to_string(&json) {
                        let _ = writeln!(stdout, "{line}");
                        let _ = stdout.flush();
                    }
                    continue;
                }
                Err(broadcast::error::RecvError::Closed) => break,
            };

            if matches!(event, Event::Shutdown) {
                let _ = stdout.flush();
                break;
            }

            let needs_flush = matches!(
                &event,
                Event::Started { .. }
                    | Event::Stopped
                    | Event::StopTimedOut
                    | Event::Reloaded
                    | Event::Error { .. }
                    | Event::Print { .. }
            );

            let stamp = scru128::new().to_string();

            let json = match event {
                Event::Request {
                    request_id,
                    request,
                } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "request",
                        "request_id": request_id.to_string(),
                        "method": &request.method,
                        "path": &request.path,
                        "trusted_ip": &request.trusted_ip,
                        "request": request,
                    })
                }
                Event::Response {
                    request_id,
                    status,
                    headers,
                    latency_ms,
                } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "response",
                        "request_id": request_id.to_string(),
                        "status": status,
                        "headers": headers,
                        "latency_ms": latency_ms,
                    })
                }
                Event::Complete {
                    request_id,
                    bytes,
                    duration_ms,
                } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "complete",
                        "request_id": request_id.to_string(),
                        "bytes": bytes,
                        "duration_ms": duration_ms,
                    })
                }
                Event::Started {
                    address,
                    startup_ms,
                    options,
                } => {
                    let xs_version: Option<&str> = if options.store.is_some() {
                        #[cfg(feature = "cross-stream")]
                        {
                            Some(env!("XS_VERSION"))
                        }
                        #[cfg(not(feature = "cross-stream"))]
                        {
                            None
                        }
                    } else {
                        None
                    };
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "started",
                        "address": address,
                        "startup_ms": startup_ms,
                        "watch": options.watch,
                        "tls": options.tls,
                        "store": options.store,
                        "topic": options.topic,
                        "expose": options.expose,
                        "services": options.services,
                        "nu_version": env!("NU_VERSION"),
                        "xs_version": xs_version,
                        "datastar_version": DATASTAR_VERSION,
                    })
                }
                Event::Reloaded => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "reloaded",
                    })
                }
                Event::Error { error } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "error",
                        "error": error,
                    })
                }
                Event::Print { message } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "print",
                        "content": message,
                    })
                }
                Event::Stopping { inflight } => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "stopping",
                        "inflight": inflight,
                    })
                }
                Event::Stopped => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "stopped",
                    })
                }
                Event::StopTimedOut => {
                    serde_json::json!({
                        "stamp": stamp,
                        "message": "stop_timed_out",
                    })
                }
                Event::Shutdown => unreachable!(),
            };

            if let Ok(line) = serde_json::to_string(&json) {
                let _ = writeln!(stdout, "{line}");
            }

            // Flush if lifecycle event or channel is empty (idle)
            if needs_flush || rx.is_empty() {
                let _ = stdout.flush();
            }
        }

        let _ = stdout.flush();
    })
}

// --- Human-readable handler (dedicated thread) ---

struct RequestState {
    method: String,
    path: String,
    trusted_ip: Option<String>,
    start_time: Instant,
    status: Option<u16>,
    latency_ms: Option<u64>,
}

fn truncate_middle(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        return s.to_string();
    }
    let keep = (max_len - 3) / 2;
    format!("{}...{}", &s[..keep], &s[s.len() - keep..])
}

struct ActiveZone {
    stdout: io::Stdout,
    line_count: usize,
}

impl ActiveZone {
    fn new() -> Self {
        Self {
            stdout: io::stdout(),
            line_count: 0,
        }
    }

    /// Clear the active zone and move cursor back to start
    fn clear(&mut self) {
        if self.line_count > 0 {
            let _ = execute!(
                self.stdout,
                cursor::MoveUp(self.line_count as u16),
                terminal::Clear(terminal::ClearType::FromCursorDown)
            );
            self.line_count = 0;
        }
    }

    /// Print a permanent line (scrolls up, not part of active zone)
    fn print_permanent(&mut self, line: &str) {
        self.clear();
        println!("{line}");
        let _ = self.stdout.flush();
    }

    /// Redraw all active requests
    fn redraw(&mut self, active_ids: &[String], requests: &HashMap<String, RequestState>) {
        self.line_count = 0;
        if !active_ids.is_empty() {
            println!("· · ·  ✈ in flight  · · ·");
            self.line_count += 1;
            for id in active_ids {
                if let Some(state) = requests.get(id) {
                    let line = format_active_line(state);
                    println!("{line}");
                    self.line_count += 1;
                }
            }
        }
        let _ = self.stdout.flush();
    }
}

fn format_active_line(state: &RequestState) -> String {
    let timestamp = Local::now().format("%H:%M:%S%.3f");
    let ip = state.trusted_ip.as_deref().unwrap_or("-");
    let method = &state.method;
    let path = truncate_middle(&state.path, 40);
    let elapsed = state.start_time.elapsed().as_secs_f64();

    match (state.status, state.latency_ms) {
        (Some(status), Some(latency)) => {
            format!(
                "{timestamp} {ip:>15} {method:<6} {path:<40} {status} {latency:>6}ms {elapsed:>6.1}s"
            )
        }
        _ => {
            format!("{timestamp} {ip:>15} {method:<6} {path:<40} ... {elapsed:>6.1}s")
        }
    }
}

fn format_complete_line(state: &RequestState, duration_ms: u64, bytes: u64) -> String {
    let timestamp = Local::now().format("%H:%M:%S%.3f");
    let ip = state.trusted_ip.as_deref().unwrap_or("-");
    let method = &state.method;
    let path = truncate_middle(&state.path, 40);
    let status = state.status.unwrap_or(0);
    let latency = state.latency_ms.unwrap_or(0);

    format!(
        "{timestamp} {ip:>15} {method:<6} {path:<40} {status} {latency:>6}ms {duration_ms:>6}ms {bytes:>8}b"
    )
}

pub fn run_human_handler(rx: broadcast::Receiver<Event>) -> std::thread::JoinHandle<()> {
    std::thread::spawn(move || {
        let mut rx = rx;
        let mut zone = ActiveZone::new();
        let mut requests: HashMap<String, RequestState> = HashMap::new();
        let mut active_ids: Vec<String> = Vec::new();

        // Rate limiting: token bucket (burst 40, refill 20/sec)
        let mut rate_limiter = TokenBucket::new(40.0, 20.0, Instant::now());
        let mut skipped: u64 = 0;
        let mut lagged: u64 = 0;

        loop {
            let event = match rx.blocking_recv() {
                Ok(event) => event,
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    lagged += n;
                    // Clear all pending - their Response/Complete events may have been dropped
                    requests.clear();
                    active_ids.clear();
                    zone.print_permanent(&format!(
                        "âš  lagged: dropped {n} events, cleared in-flight"
                    ));
                    continue;
                }
                Err(broadcast::error::RecvError::Closed) => break,
            };
            match event {
                Event::Started {
                    address,
                    startup_ms,
                    options,
                } => {
                    let version = env!("CARGO_PKG_VERSION");
                    let pid = std::process::id();
                    let now = Local::now().to_rfc2822();
                    zone.print_permanent(&format!("<http-nu version=\"{version}\">"));
                    zone.print_permanent("     __  ,");
                    zone.print_permanent(&format!(
                        " .--()°'.'  pid {pid} · {address} · {startup_ms}ms 💜"
                    ));
                    zone.print_permanent(&format!("'|, . ,'    {now}"));

                    // Build options line: [http-nu opts] │ xs [store] [expose] [services]
                    let mut http_opts = Vec::new();
                    if options.watch {
                        http_opts.push("watch".to_string());
                    }
                    if let Some(ref tls) = options.tls {
                        http_opts.push(format!("tls:{tls}"));
                    }

                    let mut xs_opts = Vec::new();
                    if let Some(ref store) = options.store {
                        xs_opts.push(store.to_string());
                    }
                    if let Some(ref topic) = options.topic {
                        xs_opts.push(format!("topic:{topic}"));
                    }
                    if let Some(ref expose) = options.expose {
                        xs_opts.push(expose.to_string());
                    }
                    if options.services {
                        xs_opts.push("services".to_string());
                    }

                    // Build versions string
                    let mut versions = vec![format!("nu {}", env!("NU_VERSION"))];
                    #[cfg(feature = "cross-stream")]
                    if options.store.is_some() {
                        versions.push(format!("xs {}", env!("XS_VERSION")));
                    }
                    versions.push(format!("datastar {DATASTAR_VERSION}"));
                    let versions_str = versions.join(" · ");

                    let has_opts = !http_opts.is_empty() || !xs_opts.is_empty();
                    if has_opts {
                        // Options on duck line, versions on next line
                        let opts_str = match (http_opts.is_empty(), xs_opts.is_empty()) {
                            (false, true) => http_opts.join(" "),
                            (true, false) => format!("xs {}", xs_opts.join(" ")),
                            (false, false) => {
                                format!("{} │ xs {}", http_opts.join(" "), xs_opts.join(" "))
                            }
                            _ => unreachable!(),
                        };
                        zone.print_permanent(&format!(" !_-(_\\     {opts_str}"));
                        zone.print_permanent(&format!("            {versions_str}"));
                    } else {
                        // No options: versions on duck line
                        zone.print_permanent(&format!(" !_-(_\\     {versions_str}"));
                    }

                    zone.redraw(&active_ids, &requests);
                }
                Event::Reloaded => {
                    zone.print_permanent("reloaded 🔄");
                    zone.redraw(&active_ids, &requests);
                }
                Event::Error { error } => {
                    zone.clear();
                    eprintln!("ERROR: {error}");
                    zone.redraw(&active_ids, &requests);
                }
                Event::Print { message } => {
                    zone.print_permanent(&format!("PRINT: {message}"));
                    zone.redraw(&active_ids, &requests);
                }
                Event::Stopping { inflight } => {
                    zone.print_permanent(&format!(
                        "stopping, {inflight} connection(s) in flight..."
                    ));
                    zone.redraw(&active_ids, &requests);
                }
                Event::Stopped => {
                    let timestamp = Local::now().format("%H:%M:%S%.3f");
                    zone.print_permanent(&format!("{timestamp} cu l8r"));
                    zone.clear();
                    println!("</http-nu>");
                }
                Event::StopTimedOut => {
                    zone.print_permanent("stop timed out, forcing exit");
                }
                Event::Request {
                    request_id,
                    request,
                } => {
                    if !rate_limiter.try_consume(Instant::now()) {
                        skipped += 1;
                        continue;
                    }

                    // Print skip summary if any
                    if skipped > 0 {
                        zone.print_permanent(&format!("... skipped {skipped} requests"));
                        skipped = 0;
                    }

                    let id = request_id.to_string();
                    let state = RequestState {
                        method: request.method.clone(),
                        path: request.path.clone(),
                        trusted_ip: request.trusted_ip.clone(),
                        start_time: Instant::now(),
                        status: None,
                        latency_ms: None,
                    };
                    requests.insert(id.clone(), state);
                    active_ids.push(id);
                    zone.clear();
                    zone.redraw(&active_ids, &requests);
                }
                Event::Response {
                    request_id,
                    status,
                    latency_ms,
                    ..
                } => {
                    let id = request_id.to_string();
                    if let Some(state) = requests.get_mut(&id) {
                        state.status = Some(status);
                        state.latency_ms = Some(latency_ms);
                        zone.clear();
                        zone.redraw(&active_ids, &requests);
                    }
                }
                Event::Complete {
                    request_id,
                    bytes,
                    duration_ms,
                } => {
                    let id = request_id.to_string();
                    if let Some(state) = requests.remove(&id) {
                        active_ids.retain(|x| x != &id);
                        let line = format_complete_line(&state, duration_ms, bytes);
                        zone.print_permanent(&line);
                        zone.redraw(&active_ids, &requests);
                    }
                }
                Event::Shutdown => break,
            }
        }

        // Final summaries
        zone.clear();
        if skipped > 0 {
            println!("... skipped {skipped} requests");
        }
        if lagged > 0 {
            println!("âš  total lagged: {lagged} events dropped");
        }
    })
}

// --- RequestGuard: ensures Complete fires even on abort ---

pub struct RequestGuard {
    request_id: scru128::Scru128Id,
    start: Instant,
    bytes_sent: u64,
}

impl RequestGuard {
    pub fn new(request_id: scru128::Scru128Id) -> Self {
        Self {
            request_id,
            start: Instant::now(),
            bytes_sent: 0,
        }
    }

    pub fn request_id(&self) -> scru128::Scru128Id {
        self.request_id
    }
}

impl Drop for RequestGuard {
    fn drop(&mut self) {
        log_complete(self.request_id, self.bytes_sent, self.start);
    }
}

// --- LoggingBody wrapper ---

pub struct LoggingBody<B> {
    inner: B,
    guard: RequestGuard,
}

impl<B> LoggingBody<B> {
    pub fn new(inner: B, guard: RequestGuard) -> Self {
        Self { inner, guard }
    }
}

impl<B> Body for LoggingBody<B>
where
    B: Body<Data = Bytes, Error = BoxError> + Unpin,
{
    type Data = Bytes;
    type Error = BoxError;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        let inner = Pin::new(&mut self.inner);
        match inner.poll_frame(cx) {
            Poll::Ready(Some(Ok(frame))) => {
                if let Some(data) = frame.data_ref() {
                    self.guard.bytes_sent += data.len() as u64;
                }
                Poll::Ready(Some(Ok(frame)))
            }
            Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }

    fn is_end_stream(&self) -> bool {
        self.inner.is_end_stream()
    }

    fn size_hint(&self) -> SizeHint {
        self.inner.size_hint()
    }
}

// No Drop impl needed - guard's Drop fires Complete

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn token_bucket_allows_burst() {
        let start = Instant::now();
        let mut bucket = TokenBucket::new(40.0, 20.0, start);

        // Should allow 40 requests immediately
        for _ in 0..40 {
            assert!(bucket.try_consume(start));
        }
        // 41st should fail
        assert!(!bucket.try_consume(start));
    }

    #[test]
    fn token_bucket_refills_over_time() {
        let start = Instant::now();
        let mut bucket = TokenBucket::new(40.0, 20.0, start);

        // Drain the bucket
        for _ in 0..40 {
            bucket.try_consume(start);
        }
        assert!(!bucket.try_consume(start));

        // After 100ms, should have 2 tokens (20/sec * 0.1s)
        let later = start + Duration::from_millis(100);
        assert!(bucket.try_consume(later));
        assert!(bucket.try_consume(later));
        assert!(!bucket.try_consume(later));
    }

    #[test]
    fn token_bucket_caps_at_capacity() {
        let start = Instant::now();
        let mut bucket = TokenBucket::new(40.0, 20.0, start);

        // Wait a long time - should still cap at 40
        let later = start + Duration::from_secs(10);
        for _ in 0..40 {
            assert!(bucket.try_consume(later));
        }
        assert!(!bucket.try_consume(later));
    }
}