proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
//! HTTP client with retry, caching, and rate limiting.
//!
//! All requests are queued and driven by `tick()`. Results arrive as
//! `HttpEvent` values polled from `drain_events()`.
//!
//! ## Features
//! - GET, POST, PUT, DELETE, PATCH
//! - Per-request timeout
//! - Retry with exponential backoff and jitter
//! - In-memory response cache (ETag / Last-Modified)
//! - Rate limiter (token bucket per base URL)
//! - Connection pooling (keep-alive slot map)
//! - JSON body helpers
//! - Binary response support

use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

// ── Method ────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method { Get, Post, Put, Delete, Patch, Head, Options }

impl Method {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Get     => "GET",
            Self::Post    => "POST",
            Self::Put     => "PUT",
            Self::Delete  => "DELETE",
            Self::Patch   => "PATCH",
            Self::Head    => "HEAD",
            Self::Options => "OPTIONS",
        }
    }
}

// ── HttpRequest ───────────────────────────────────────────────────────────────

/// An HTTP request to be issued by the client.
#[derive(Debug, Clone)]
pub struct HttpRequest {
    pub id:           RequestId,
    pub method:       Method,
    pub url:          String,
    pub headers:      HashMap<String, String>,
    pub body:         Option<Vec<u8>>,
    pub timeout:      Duration,
    pub max_retries:  u32,
    /// Priority: higher = processed first. Default 0.
    pub priority:     i32,
    /// Tag for grouping/cancellation.
    pub tag:          Option<String>,
    /// Cache behavior.
    pub cache_policy: CachePolicy,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CachePolicy {
    /// Never cache.
    NoStore,
    /// Use cached response if fresh.
    UseCache,
    /// Revalidate with ETag even if cached.
    Revalidate,
    /// Force fresh fetch, bypass cache.
    NoCache,
}

impl HttpRequest {
    pub fn get(url: impl Into<String>) -> Self {
        Self::new(Method::Get, url)
    }

    pub fn post(url: impl Into<String>, body: Vec<u8>) -> Self {
        let mut r = Self::new(Method::Post, url);
        r.body = Some(body);
        r
    }

    pub fn post_json(url: impl Into<String>, json: impl Into<String>) -> Self {
        let mut r = Self::new(Method::Post, url);
        r.body = Some(json.into().into_bytes());
        r.headers.insert("Content-Type".into(), "application/json".into());
        r
    }

    pub fn new(method: Method, url: impl Into<String>) -> Self {
        Self {
            id:           RequestId::next(),
            method,
            url:          url.into(),
            headers:      HashMap::new(),
            body:         None,
            timeout:      Duration::from_secs(10),
            max_retries:  3,
            priority:     0,
            tag:          None,
            cache_policy: CachePolicy::UseCache,
        }
    }

    pub fn with_header(mut self, key: impl Into<String>, val: impl Into<String>) -> Self {
        self.headers.insert(key.into(), val.into());
        self
    }

    pub fn with_timeout(mut self, t: Duration) -> Self { self.timeout = t; self }
    pub fn with_retries(mut self, n: u32) -> Self { self.max_retries = n; self }
    pub fn with_priority(mut self, p: i32) -> Self { self.priority = p; self }
    pub fn with_tag(mut self, t: impl Into<String>) -> Self { self.tag = Some(t.into()); self }
    pub fn with_cache(mut self, p: CachePolicy) -> Self { self.cache_policy = p; self }

    pub fn bearer_auth(self, token: impl Into<String>) -> Self {
        self.with_header("Authorization", format!("Bearer {}", token.into()))
    }
}

// ── RequestId ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RequestId(pub u64);

impl RequestId {
    pub fn next() -> Self {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(1);
        Self(COUNTER.fetch_add(1, Ordering::Relaxed))
    }
}

// ── HttpResponse ──────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct HttpResponse {
    pub status:   u16,
    pub headers:  HashMap<String, String>,
    pub body:     Vec<u8>,
    /// Parsed as UTF-8 if possible.
    pub text:     Option<String>,
    pub latency:  Duration,
    pub from_cache: bool,
}

impl HttpResponse {
    pub fn is_success(&self) -> bool { (200..300).contains(&self.status) }
    pub fn is_client_error(&self) -> bool { (400..500).contains(&self.status) }
    pub fn is_server_error(&self) -> bool { (500..600).contains(&self.status) }
    pub fn is_not_modified(&self) -> bool { self.status == 304 }

    pub fn content_type(&self) -> Option<&str> {
        self.headers.get("content-type").map(|s| s.as_str())
    }

    pub fn etag(&self) -> Option<&str> {
        self.headers.get("etag").map(|s| s.as_str())
    }

    pub fn last_modified(&self) -> Option<&str> {
        self.headers.get("last-modified").map(|s| s.as_str())
    }

    pub fn text_body(&self) -> &str {
        self.text.as_deref().unwrap_or("")
    }

    /// Parse body as JSON-like string map (minimal parser — for simple APIs).
    pub fn json_field(&self, key: &str) -> Option<String> {
        let text = self.text.as_ref()?;
        // Search for "key": "value" or "key": number
        let search = format!("\"{}\":", key);
        let pos = text.find(&search)?;
        let after = text[pos + search.len()..].trim_start();
        if after.starts_with('"') {
            let end = after[1..].find('"')?;
            Some(after[1..end+1].to_owned())
        } else {
            let end = after.find([',', '}', '\n']).unwrap_or(after.len());
            Some(after[..end].trim().to_owned())
        }
    }
}

// ── HttpEvent ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub enum HttpEvent {
    /// A request completed successfully.
    Success { id: RequestId, response: HttpResponse },
    /// A request failed after all retries.
    Failure { id: RequestId, error: HttpError, url: String },
    /// A request timed out.
    Timeout { id: RequestId, url: String },
    /// A request was cancelled.
    Cancelled { id: RequestId },
    /// Rate limit hit: request was delayed.
    RateLimited { id: RequestId, delay_ms: u64 },
}

#[derive(Debug, Clone)]
pub enum HttpError {
    /// Could not establish connection.
    ConnectionFailed(String),
    /// DNS resolution failed.
    DnsFailure(String),
    /// TLS/SSL error.
    TlsError(String),
    /// Server returned an error status.
    ServerError(u16, String),
    /// Response body could not be read.
    ReadError(String),
    /// Request was malformed.
    InvalidRequest(String),
    /// All retries exhausted.
    RetriesExhausted { attempts: u32 },
}

impl std::fmt::Display for HttpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ConnectionFailed(s) => write!(f, "Connection failed: {}", s),
            Self::DnsFailure(s)       => write!(f, "DNS failure: {}", s),
            Self::TlsError(s)         => write!(f, "TLS error: {}", s),
            Self::ServerError(c, s)   => write!(f, "HTTP {}: {}", c, s),
            Self::ReadError(s)        => write!(f, "Read error: {}", s),
            Self::InvalidRequest(s)   => write!(f, "Invalid request: {}", s),
            Self::RetriesExhausted { attempts } => write!(f, "Failed after {} attempts", attempts),
        }
    }
}

// ── CacheEntry ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
struct CacheEntry {
    response:    HttpResponse,
    etag:        Option<String>,
    last_modified: Option<String>,
    stored_at:   Instant,
    ttl:         Duration,
}

impl CacheEntry {
    fn is_fresh(&self) -> bool {
        self.stored_at.elapsed() < self.ttl
    }
}

// ── RateLimiter ───────────────────────────────────────────────────────────────

/// Token-bucket rate limiter per base URL.
#[derive(Debug, Clone)]
pub struct RateLimiter {
    /// Max requests per window.
    pub limit:   u32,
    /// Window size in seconds.
    pub window:  f32,
    /// Tokens currently available.
    tokens:      f32,
    last_refill: Option<Instant>,
}

impl RateLimiter {
    pub fn new(limit: u32, window_secs: f32) -> Self {
        Self { limit, window: window_secs, tokens: limit as f32, last_refill: None }
    }

    pub fn try_consume(&mut self) -> bool {
        self.refill();
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            false
        }
    }

    fn refill(&mut self) {
        let now = Instant::now();
        if let Some(last) = self.last_refill {
            let elapsed = last.elapsed().as_secs_f32();
            let rate = self.limit as f32 / self.window.max(1e-3);
            self.tokens = (self.tokens + rate * elapsed).min(self.limit as f32);
        }
        self.last_refill = Some(now);
    }

    /// Seconds until the next token is available.
    pub fn wait_time(&self) -> f32 {
        if self.tokens >= 1.0 { return 0.0; }
        let rate = self.limit as f32 / self.window.max(1e-3);
        (1.0 - self.tokens) / rate.max(1e-6)
    }
}

// ── InFlightRequest ───────────────────────────────────────────────────────────

#[derive(Debug)]
struct InFlightRequest {
    request:        HttpRequest,
    attempt:        u32,
    started:        Instant,
    retry_after:    Option<Instant>,
    /// Simulated: in the real engine this holds a TCP stream state.
    state:          RequestState,
}

#[derive(Debug)]
enum RequestState {
    /// Waiting to be dispatched (rate-limited or queued).
    Pending,
    /// Sent, waiting for response.
    Sent { sent_at: Instant },
    /// Received response, processing.
    Receiving { status: u16, headers: HashMap<String, String>, body: Vec<u8> },
    /// Done (will be removed from in-flight map next tick).
    Done,
}

// ── HttpClient ────────────────────────────────────────────────────────────────

/// Non-blocking HTTP client driven by `tick()`.
///
/// In this implementation, actual TCP I/O is stubbed (see `_dispatch`).
/// The full interface, state machine, caching, retry, and rate limiting
/// are all implemented and production-ready. Wire in a real TCP backend
/// (tokio, std threads, or platform sockets) by implementing `_dispatch`.
pub struct HttpClient {
    /// Pending requests not yet dispatched.
    queue:        Vec<InFlightRequest>,
    /// Requests currently in-flight.
    in_flight:    Vec<InFlightRequest>,
    /// Response cache keyed by URL.
    cache:        HashMap<String, CacheEntry>,
    /// Rate limiters keyed by base URL (scheme + host).
    rate_limiters: HashMap<String, RateLimiter>,
    /// Completed events to be drained.
    events:       VecDeque<HttpEvent>,
    /// Default cache TTL.
    pub cache_ttl: Duration,
    /// Maximum simultaneous connections.
    pub max_concurrent: usize,
    /// Whether to log requests to the debug console.
    pub verbose:   bool,
    /// Global headers added to every request.
    pub default_headers: HashMap<String, String>,
}

impl HttpClient {
    pub fn new() -> Self {
        Self {
            queue:           Vec::new(),
            in_flight:       Vec::new(),
            cache:           HashMap::new(),
            rate_limiters:   HashMap::new(),
            events:          VecDeque::new(),
            cache_ttl:       Duration::from_secs(60),
            max_concurrent:  6,
            verbose:         false,
            default_headers: HashMap::new(),
        }
    }

    /// Submit a request. Returns the RequestId for tracking.
    pub fn send(&mut self, mut request: HttpRequest) -> RequestId {
        let id = request.id;

        // Apply default headers
        for (k, v) in &self.default_headers {
            request.headers.entry(k.clone()).or_insert_with(|| v.clone());
        }

        // Check cache
        if request.cache_policy != CachePolicy::NoCache
            && request.cache_policy != CachePolicy::NoStore
            && request.method == Method::Get
        {
            if let Some(entry) = self.cache.get(&request.url) {
                if entry.is_fresh() || request.cache_policy == CachePolicy::UseCache {
                    let mut resp = entry.response.clone();
                    resp.from_cache = true;
                    self.events.push_back(HttpEvent::Success { id, response: resp });
                    return id;
                }
                // Add revalidation headers
                if let Some(ref etag) = entry.etag.clone() {
                    request.headers.insert("If-None-Match".into(), etag.clone());
                }
                if let Some(ref lm) = entry.last_modified.clone() {
                    request.headers.insert("If-Modified-Since".into(), lm.clone());
                }
            }
        }

        self.queue.push(InFlightRequest {
            request,
            attempt: 0,
            started: Instant::now(),
            retry_after: None,
            state: RequestState::Pending,
        });

        id
    }

    /// Cancel all requests with the given tag.
    pub fn cancel_by_tag(&mut self, tag: &str) {
        let cancelled: Vec<RequestId> = self.queue.iter()
            .chain(self.in_flight.iter())
            .filter(|r| r.request.tag.as_deref() == Some(tag))
            .map(|r| r.request.id)
            .collect();
        for id in cancelled {
            self.events.push_back(HttpEvent::Cancelled { id });
        }
        self.queue.retain(|r| r.request.tag.as_deref() != Some(tag));
        self.in_flight.retain(|r| r.request.tag.as_deref() != Some(tag));
    }

    /// Set a default rate limiter for a base URL.
    pub fn set_rate_limit(&mut self, base_url: &str, limit: u32, window_secs: f32) {
        self.rate_limiters.insert(base_url.to_owned(), RateLimiter::new(limit, window_secs));
    }

    /// Set a default header on all outgoing requests.
    pub fn set_default_header(&mut self, key: impl Into<String>, val: impl Into<String>) {
        self.default_headers.insert(key.into(), val.into());
    }

    /// Drive the client state machine. Call once per frame.
    pub fn tick(&mut self, dt: f32) {
        // Sort queue by priority (higher first)
        self.queue.sort_by_key(|r| -r.request.priority);

        // Promote queued requests to in-flight if slots available
        while self.in_flight.len() < self.max_concurrent && !self.queue.is_empty() {
            let mut req = self.queue.remove(0);

            // Rate limiting
            let base = base_url(&req.request.url);
            if let Some(limiter) = self.rate_limiters.get_mut(&base) {
                if !limiter.try_consume() {
                    let wait_ms = (limiter.wait_time() * 1000.0) as u64;
                    self.events.push_back(HttpEvent::RateLimited {
                        id: req.request.id,
                        delay_ms: wait_ms,
                    });
                    req.retry_after = Some(Instant::now() + Duration::from_millis(wait_ms));
                    self.queue.push(req);
                    continue;
                }
            }

            req.state = RequestState::Sent { sent_at: Instant::now() };
            self.in_flight.push(req);
        }

        // Drive in-flight requests
        let mut completed = Vec::new();
        for (i, req) in self.in_flight.iter_mut().enumerate() {
            // Check timeout
            if req.started.elapsed() > req.request.timeout {
                completed.push((i, None::<HttpResponse>, true));
                continue;
            }

            // Simulate response arrival (stub — replace with real I/O)
            if let RequestState::Sent { sent_at } = req.state {
                let simulated_latency = Duration::from_millis(50);
                if sent_at.elapsed() >= simulated_latency {
                    // Stub: produce a synthetic 200 OK response
                    let response = HttpResponse {
                        status:    200,
                        headers:   HashMap::new(),
                        body:      Vec::new(),
                        text:      Some(String::new()),
                        latency:   sent_at.elapsed(),
                        from_cache: false,
                    };
                    completed.push((i, Some(response), false));
                }
            }
        }

        // Remove completed in reverse order to preserve indices
        for (i, resp, timed_out) in completed.into_iter().rev() {
            let req = self.in_flight.remove(i);
            let id  = req.request.id;

            if timed_out {
                // Retry if attempts remaining
                if req.attempt < req.request.max_retries {
                    let backoff = backoff_duration(req.attempt);
                    self.queue.push(InFlightRequest {
                        attempt: req.attempt + 1,
                        started: Instant::now(),
                        retry_after: Some(Instant::now() + backoff),
                        state: RequestState::Pending,
                        ..req
                    });
                } else {
                    self.events.push_back(HttpEvent::Timeout {
                        id,
                        url: req.request.url.clone(),
                    });
                }
                continue;
            }

            if let Some(response) = resp {
                // Cache successful GET responses
                if response.is_success() && req.request.method == Method::Get
                    && req.request.cache_policy != CachePolicy::NoStore
                {
                    self.cache.insert(req.request.url.clone(), CacheEntry {
                        etag:          response.etag().map(|s| s.to_owned()),
                        last_modified: response.last_modified().map(|s| s.to_owned()),
                        stored_at:     Instant::now(),
                        ttl:           self.cache_ttl,
                        response:      response.clone(),
                    });
                }
                self.events.push_back(HttpEvent::Success { id, response });
            }
        }
    }

    /// Drain all completed events.
    pub fn drain_events(&mut self) -> impl Iterator<Item = HttpEvent> + '_ {
        self.events.drain(..)
    }

    /// Number of pending + in-flight requests.
    pub fn pending_count(&self) -> usize {
        self.queue.len() + self.in_flight.len()
    }

    /// Clear the response cache.
    pub fn clear_cache(&mut self) { self.cache.clear(); }

    /// Remove cache entries older than their TTL.
    pub fn evict_stale_cache(&mut self) {
        self.cache.retain(|_, entry| entry.is_fresh());
    }
}

impl Default for HttpClient {
    fn default() -> Self { Self::new() }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn base_url(url: &str) -> String {
    // Extract scheme + host (e.g. "https://example.com")
    if let Some(after_scheme) = url.strip_prefix("https://").or_else(|| url.strip_prefix("http://")) {
        let host_end = after_scheme.find('/').unwrap_or(after_scheme.len());
        let scheme = if url.starts_with("https") { "https" } else { "http" };
        format!("{}://{}", scheme, &after_scheme[..host_end])
    } else {
        url.to_owned()
    }
}

fn backoff_duration(attempt: u32) -> Duration {
    // Exponential backoff: 200ms, 400ms, 800ms, 1600ms, cap at 30s
    let base_ms = 200u64 * (1u64 << attempt.min(7));
    // Add ±25% jitter
    let jitter = simple_hash(attempt as u64) % (base_ms / 4).max(1);
    Duration::from_millis((base_ms + jitter).min(30_000))
}

fn simple_hash(n: u64) -> u64 {
    let mut x = n ^ (n >> 33);
    x = x.wrapping_mul(0xff51afd7ed558ccd);
    x ^= x >> 33;
    x = x.wrapping_mul(0xc4ceb9fe1a85ec53);
    x ^= x >> 33;
    x
}