fastapi-output 0.3.0

Agent-aware rich console output for fastapi_rust
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
//! Request/response logging component.
//!
//! Provides colorized HTTP request/response logging with timing information.

use crate::mode::OutputMode;
use crate::themes::FastApiTheme;
use std::time::Duration;

const ANSI_RESET: &str = "\x1b[0m";
const ANSI_BOLD: &str = "\x1b[1m";

/// HTTP methods supported for logging.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpMethod {
    /// GET request.
    Get,
    /// POST request.
    Post,
    /// PUT request.
    Put,
    /// DELETE request.
    Delete,
    /// PATCH request.
    Patch,
    /// OPTIONS request.
    Options,
    /// HEAD request.
    Head,
    /// TRACE request.
    Trace,
    /// CONNECT request.
    Connect,
}

impl HttpMethod {
    /// Get the method name as a string.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Get => "GET",
            Self::Post => "POST",
            Self::Put => "PUT",
            Self::Delete => "DELETE",
            Self::Patch => "PATCH",
            Self::Options => "OPTIONS",
            Self::Head => "HEAD",
            Self::Trace => "TRACE",
            Self::Connect => "CONNECT",
        }
    }

    /// Get the color for this method from the theme.
    fn color(self, theme: &FastApiTheme) -> crate::themes::Color {
        match self {
            Self::Get => theme.http_get,
            Self::Post => theme.http_post,
            Self::Put => theme.http_put,
            Self::Delete => theme.http_delete,
            Self::Patch => theme.http_patch,
            Self::Options => theme.http_options,
            Self::Head => theme.http_head,
            // Fallback for less common methods
            Self::Trace | Self::Connect => theme.muted,
        }
    }
}

impl std::str::FromStr for HttpMethod {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "GET" => Ok(Self::Get),
            "POST" => Ok(Self::Post),
            "PUT" => Ok(Self::Put),
            "DELETE" => Ok(Self::Delete),
            "PATCH" => Ok(Self::Patch),
            "OPTIONS" => Ok(Self::Options),
            "HEAD" => Ok(Self::Head),
            "TRACE" => Ok(Self::Trace),
            "CONNECT" => Ok(Self::Connect),
            _ => Err(()),
        }
    }
}

/// Response timing information.
#[derive(Debug, Clone, Copy)]
pub struct ResponseTiming {
    /// Total request duration.
    pub total: Duration,
}

impl ResponseTiming {
    /// Create a new timing with the given duration.
    #[must_use]
    pub const fn new(total: Duration) -> Self {
        Self { total }
    }

    /// Format the timing as a human-readable string.
    #[must_use]
    pub fn format(&self) -> String {
        let micros = self.total.as_micros();
        if micros < 1000 {
            format!("{micros}µs")
        } else if micros < 1_000_000 {
            let whole = micros / 1000;
            let frac = (micros % 1000) / 10;
            format!("{whole}.{frac:02}ms")
        } else {
            let whole = micros / 1_000_000;
            let frac = (micros % 1_000_000) / 10_000;
            format!("{whole}.{frac:02}s")
        }
    }
}

/// A log entry for request/response logging.
#[derive(Debug, Clone)]
pub struct LogEntry {
    /// HTTP method.
    pub method: HttpMethod,
    /// Request path.
    pub path: String,
    /// Query string (if any).
    pub query: Option<String>,
    /// Response status code.
    pub status: u16,
    /// Response timing.
    pub timing: Option<ResponseTiming>,
    /// Client IP address.
    pub client_ip: Option<String>,
    /// Request ID.
    pub request_id: Option<String>,
}

impl LogEntry {
    /// Create a new log entry.
    #[must_use]
    pub fn new(method: HttpMethod, path: impl Into<String>, status: u16) -> Self {
        Self {
            method,
            path: path.into(),
            query: None,
            status,
            timing: None,
            client_ip: None,
            request_id: None,
        }
    }

    /// Set the query string.
    #[must_use]
    pub fn query(mut self, query: impl Into<String>) -> Self {
        self.query = Some(query.into());
        self
    }

    /// Set the response timing.
    #[must_use]
    pub fn timing(mut self, timing: ResponseTiming) -> Self {
        self.timing = Some(timing);
        self
    }

    /// Set the client IP.
    #[must_use]
    pub fn client_ip(mut self, ip: impl Into<String>) -> Self {
        self.client_ip = Some(ip.into());
        self
    }

    /// Set the request ID.
    #[must_use]
    pub fn request_id(mut self, id: impl Into<String>) -> Self {
        self.request_id = Some(id.into());
        self
    }
}

/// Request/response logger.
#[derive(Debug, Clone)]
pub struct RequestLogger {
    mode: OutputMode,
    theme: FastApiTheme,
    /// Show client IP in logs.
    pub show_client_ip: bool,
    /// Show request ID in logs.
    pub show_request_id: bool,
    /// Show query string in logs.
    pub show_query: bool,
}

impl RequestLogger {
    /// Create a new logger with the specified mode.
    #[must_use]
    pub fn new(mode: OutputMode) -> Self {
        Self {
            mode,
            theme: FastApiTheme::default(),
            show_client_ip: false,
            show_request_id: false,
            show_query: true,
        }
    }

    /// Set the theme.
    #[must_use]
    pub fn theme(mut self, theme: FastApiTheme) -> Self {
        self.theme = theme;
        self
    }

    /// Format a log entry to a string.
    #[must_use]
    pub fn format(&self, entry: &LogEntry) -> String {
        match self.mode {
            OutputMode::Plain => self.format_plain(entry),
            OutputMode::Minimal => self.format_minimal(entry),
            OutputMode::Rich => self.format_rich(entry),
        }
    }

    fn format_plain(&self, entry: &LogEntry) -> String {
        let mut parts = Vec::new();

        // Method
        parts.push(format!("{:7}", entry.method.as_str()));

        // Path with query
        let path = if self.show_query {
            match &entry.query {
                Some(q) => format!("{}?{}", entry.path, q),
                None => entry.path.clone(),
            }
        } else {
            entry.path.clone()
        };
        parts.push(path);

        // Status
        parts.push(format!("{}", entry.status));

        // Timing
        if let Some(timing) = &entry.timing {
            parts.push(timing.format());
        }

        // Client IP
        if self.show_client_ip {
            if let Some(ip) = &entry.client_ip {
                parts.push(format!("[{ip}]"));
            }
        }

        // Request ID
        if self.show_request_id {
            if let Some(id) = &entry.request_id {
                parts.push(format!("({id})"));
            }
        }

        parts.join(" ")
    }

    fn format_minimal(&self, entry: &LogEntry) -> String {
        let method_color = entry.method.color(&self.theme).to_ansi_fg();
        let status_color = self.status_color(entry.status).to_ansi_fg();

        let mut parts = Vec::new();

        // Method with color
        parts.push(format!(
            "{method_color}{:7}{ANSI_RESET}",
            entry.method.as_str()
        ));

        // Path with query
        let path = if self.show_query {
            match &entry.query {
                Some(q) => format!("{}?{}", entry.path, q),
                None => entry.path.clone(),
            }
        } else {
            entry.path.clone()
        };
        parts.push(path);

        // Status with color
        parts.push(format!("{status_color}{}{ANSI_RESET}", entry.status));

        // Timing
        if let Some(timing) = &entry.timing {
            let muted = self.theme.muted.to_ansi_fg();
            parts.push(format!("{muted}{}{ANSI_RESET}", timing.format()));
        }

        parts.join(" ")
    }

    fn format_rich(&self, entry: &LogEntry) -> String {
        let status_color = self.status_color(entry.status).to_ansi_fg();
        let muted = self.theme.muted.to_ansi_fg();

        let mut parts = Vec::new();

        // Method badge
        let method_bg = entry.method.color(&self.theme).to_ansi_bg();
        parts.push(format!(
            "{method_bg}{ANSI_BOLD} {:7} {ANSI_RESET}",
            entry.method.as_str()
        ));

        // Path with query highlighting
        if self.show_query {
            match &entry.query {
                Some(q) => {
                    let accent = self.theme.accent.to_ansi_fg();
                    parts.push(format!("{}{accent}?{q}{ANSI_RESET}", entry.path));
                }
                None => parts.push(entry.path.clone()),
            }
        } else {
            parts.push(entry.path.clone());
        }

        // Status code with icon
        let status_icon = Self::status_icon(entry.status);
        parts.push(format!(
            "{status_color}{status_icon} {}{ANSI_RESET}",
            entry.status
        ));

        // Timing
        if let Some(timing) = &entry.timing {
            parts.push(format!("{muted}{}{ANSI_RESET}", timing.format()));
        }

        // Client IP
        if self.show_client_ip {
            if let Some(ip) = &entry.client_ip {
                parts.push(format!("{muted}[{ip}]{ANSI_RESET}"));
            }
        }

        // Request ID
        if self.show_request_id {
            if let Some(id) = &entry.request_id {
                parts.push(format!("{muted}({id}){ANSI_RESET}"));
            }
        }

        parts.join(" ")
    }

    fn status_color(&self, status: u16) -> crate::themes::Color {
        match status {
            100..=199 => self.theme.status_1xx,
            200..=299 => self.theme.status_2xx,
            300..=399 => self.theme.status_3xx,
            400..=499 => self.theme.status_4xx,
            500..=599 => self.theme.status_5xx,
            _ => self.theme.muted,
        }
    }

    fn status_icon(status: u16) -> &'static str {
        match status {
            100..=199 => "",
            200..=299 => "",
            300..=399 => "",
            400..=499 => "",
            500..=599 => "",
            _ => "?",
        }
    }
}

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

    #[test]
    fn test_http_method_as_str() {
        assert_eq!(HttpMethod::Get.as_str(), "GET");
        assert_eq!(HttpMethod::Post.as_str(), "POST");
        assert_eq!(HttpMethod::Delete.as_str(), "DELETE");
    }

    #[test]
    fn test_http_method_from_str() {
        assert_eq!("get".parse::<HttpMethod>().ok(), Some(HttpMethod::Get));
        assert_eq!("POST".parse::<HttpMethod>().ok(), Some(HttpMethod::Post));
        assert!("invalid".parse::<HttpMethod>().is_err());
    }

    #[test]
    fn test_response_timing_format() {
        assert_eq!(
            ResponseTiming::new(Duration::from_micros(500)).format(),
            "500µs"
        );
        assert_eq!(
            ResponseTiming::new(Duration::from_micros(1500)).format(),
            "1.50ms"
        );
        assert_eq!(
            ResponseTiming::new(Duration::from_secs(2)).format(),
            "2.00s"
        );
    }

    #[test]
    fn test_log_entry_builder() {
        let entry = LogEntry::new(HttpMethod::Get, "/api/users", 200)
            .query("page=1")
            .timing(ResponseTiming::new(Duration::from_millis(50)))
            .client_ip("127.0.0.1")
            .request_id("req-123");

        assert_eq!(entry.method, HttpMethod::Get);
        assert_eq!(entry.path, "/api/users");
        assert_eq!(entry.query, Some("page=1".to_string()));
        assert_eq!(entry.status, 200);
    }

    #[test]
    fn test_logger_plain_format() {
        let logger = RequestLogger::new(OutputMode::Plain);
        let entry = LogEntry::new(HttpMethod::Get, "/api/users", 200)
            .timing(ResponseTiming::new(Duration::from_millis(50)));

        let output = logger.format(&entry);

        assert!(output.contains("GET"));
        assert!(output.contains("/api/users"));
        assert!(output.contains("200"));
        assert!(!output.contains("\x1b[")); // No ANSI codes
    }

    #[test]
    fn test_logger_plain_with_query() {
        let logger = RequestLogger::new(OutputMode::Plain);
        let entry = LogEntry::new(HttpMethod::Get, "/api/users", 200).query("page=1&limit=10");

        let output = logger.format(&entry);

        assert!(output.contains("/api/users?page=1&limit=10"));
    }

    #[test]
    fn test_logger_rich_has_ansi() {
        let logger = RequestLogger::new(OutputMode::Rich);
        let entry = LogEntry::new(HttpMethod::Post, "/api/create", 201);

        let output = logger.format(&entry);

        assert!(output.contains("\x1b["));
    }

    #[test]
    fn test_logger_with_client_ip() {
        let mut logger = RequestLogger::new(OutputMode::Plain);
        logger.show_client_ip = true;

        let entry = LogEntry::new(HttpMethod::Get, "/", 200).client_ip("192.168.1.1");

        let output = logger.format(&entry);

        assert!(output.contains("[192.168.1.1]"));
    }

    #[test]
    fn test_logger_with_request_id() {
        let mut logger = RequestLogger::new(OutputMode::Plain);
        logger.show_request_id = true;

        let entry = LogEntry::new(HttpMethod::Get, "/", 200).request_id("abc-123");

        let output = logger.format(&entry);

        assert!(output.contains("(abc-123)"));
    }
}