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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Route table display component.
//!
//! Displays registered routes in a formatted table with method coloring
//! and auto-width calculation.

use crate::mode::OutputMode;
use crate::themes::FastApiTheme;
use std::fmt::Write;

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

/// A single route entry for display.
#[derive(Debug, Clone)]
pub struct RouteEntry {
    /// HTTP method.
    pub method: String,
    /// Route path pattern.
    pub path: String,
    /// Handler name or function.
    pub handler: Option<String>,
    /// Tags/groups for the route.
    pub tags: Vec<String>,
    /// Whether the route is deprecated.
    pub deprecated: bool,
}

impl RouteEntry {
    /// Create a new route entry.
    #[must_use]
    pub fn new(method: impl Into<String>, path: impl Into<String>) -> Self {
        Self {
            method: method.into(),
            path: path.into(),
            handler: None,
            tags: Vec::new(),
            deprecated: false,
        }
    }

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

    /// Add a tag.
    #[must_use]
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Add multiple tags.
    #[must_use]
    pub fn tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.tags.extend(tags.into_iter().map(Into::into));
        self
    }

    /// Mark as deprecated.
    #[must_use]
    pub fn deprecated(mut self, deprecated: bool) -> Self {
        self.deprecated = deprecated;
        self
    }
}

/// Configuration for route table display.
#[derive(Debug, Clone)]
pub struct RouteTableConfig {
    /// Whether to show handler names.
    pub show_handlers: bool,
    /// Whether to show tags.
    pub show_tags: bool,
    /// Whether to show deprecated routes.
    pub show_deprecated: bool,
    /// Maximum terminal width (0 = auto-detect or unlimited).
    pub max_width: usize,
    /// Title for the table.
    pub title: Option<String>,
}

impl Default for RouteTableConfig {
    fn default() -> Self {
        Self {
            show_handlers: true,
            show_tags: true,
            show_deprecated: true,
            max_width: 0,
            title: Some("Registered Routes".to_string()),
        }
    }
}

/// Route table display.
#[derive(Debug, Clone)]
pub struct RouteDisplay {
    mode: OutputMode,
    theme: FastApiTheme,
    config: RouteTableConfig,
}

impl RouteDisplay {
    /// Create a new route display.
    #[must_use]
    pub fn new(mode: OutputMode) -> Self {
        Self {
            mode,
            theme: FastApiTheme::default(),
            config: RouteTableConfig::default(),
        }
    }

    /// Create with custom configuration.
    #[must_use]
    pub fn with_config(mode: OutputMode, config: RouteTableConfig) -> Self {
        Self {
            mode,
            theme: FastApiTheme::default(),
            config,
        }
    }

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

    /// Render the route table.
    #[must_use]
    pub fn render(&self, routes: &[RouteEntry]) -> String {
        // Filter routes if needed
        let routes: Vec<_> = if self.config.show_deprecated {
            routes.to_vec()
        } else {
            routes.iter().filter(|r| !r.deprecated).cloned().collect()
        };

        if routes.is_empty() {
            return self.render_empty();
        }

        match self.mode {
            OutputMode::Plain => self.render_plain(&routes),
            OutputMode::Minimal => self.render_minimal(&routes),
            OutputMode::Rich => self.render_rich(&routes),
        }
    }

    fn render_empty(&self) -> String {
        match self.mode {
            OutputMode::Plain => "No routes registered.".to_string(),
            OutputMode::Minimal | OutputMode::Rich => {
                let muted = self.theme.muted.to_ansi_fg();
                format!("{muted}No routes registered.{ANSI_RESET}")
            }
        }
    }

    fn render_plain(&self, routes: &[RouteEntry]) -> String {
        let mut lines = Vec::new();

        // Title
        if let Some(title) = &self.config.title {
            lines.push(title.clone());
            lines.push("-".repeat(title.len()));
        }

        // Calculate column widths
        let method_width = routes.iter().map(|r| r.method.len()).max().unwrap_or(6);
        let path_width = routes.iter().map(|r| r.path.len()).max().unwrap_or(10);

        // Header
        let mut header = format!("{:width$}  Path", "Method", width = method_width);
        if self.config.show_handlers {
            header.push_str("  Handler");
        }
        if self.config.show_tags {
            header.push_str("  Tags");
        }
        lines.push(header);

        // Routes
        for route in routes {
            let mut line = format!(
                "{:width$}  {}",
                route.method,
                route.path,
                width = method_width
            );

            if self.config.show_handlers {
                if let Some(handler) = &route.handler {
                    // Pad path column
                    let padding = path_width.saturating_sub(route.path.len());
                    line.push_str(&" ".repeat(padding));
                    line.push_str("  ");
                    line.push_str(handler);
                }
            }

            if self.config.show_tags && !route.tags.is_empty() {
                line.push_str("  [");
                line.push_str(&route.tags.join(", "));
                line.push(']');
            }

            if route.deprecated {
                line.push_str(" (deprecated)");
            }

            lines.push(line);
        }

        // Summary
        lines.push(String::new());
        lines.push(format!("Total: {} route(s)", routes.len()));

        lines.join("\n")
    }

    fn render_minimal(&self, routes: &[RouteEntry]) -> String {
        let mut lines = Vec::new();
        let muted = self.theme.muted.to_ansi_fg();
        let accent = self.theme.accent.to_ansi_fg();

        // Title
        if let Some(title) = &self.config.title {
            lines.push(format!("{accent}{title}{ANSI_RESET}"));
            lines.push(format!("{muted}{}{ANSI_RESET}", "-".repeat(title.len())));
        }

        // Routes
        for route in routes {
            let method_color = self.method_color(&route.method).to_ansi_fg();

            let mut line = format!(
                "{method_color}{:7}{ANSI_RESET} {}",
                route.method, route.path
            );

            if self.config.show_tags && !route.tags.is_empty() {
                let _ = write!(line, " {muted}[{}]{ANSI_RESET}", route.tags.join(", "));
            }

            if route.deprecated {
                let warning = self.theme.warning.to_ansi_fg();
                let _ = write!(line, " {warning}(deprecated){ANSI_RESET}");
            }

            lines.push(line);
        }

        // Summary
        lines.push(String::new());
        lines.push(format!(
            "{muted}Total: {} route(s){ANSI_RESET}",
            routes.len()
        ));

        lines.join("\n")
    }

    fn render_rich(&self, routes: &[RouteEntry]) -> String {
        let mut lines = Vec::new();
        let muted = self.theme.muted.to_ansi_fg();
        let border = self.theme.border.to_ansi_fg();
        let header_color = self.theme.header.to_ansi_fg();

        // Calculate widths
        let method_width = routes
            .iter()
            .map(|r| r.method.len())
            .max()
            .unwrap_or(6)
            .max(7);
        let path_width = routes
            .iter()
            .map(|r| r.path.len())
            .max()
            .unwrap_or(10)
            .max(20);

        // Top border
        let table_width = method_width + path_width + 10;
        lines.push(format!("{border}{}{ANSI_RESET}", "".repeat(table_width)));

        // Title
        if let Some(title) = &self.config.title {
            let title_pad = (table_width - title.len()) / 2;
            lines.push(format!(
                "{border}{ANSI_RESET}{}{header_color}{ANSI_BOLD}{}{ANSI_RESET}{}{border}{ANSI_RESET}",
                " ".repeat(title_pad),
                title,
                " ".repeat(table_width - title_pad - title.len())
            ));
            lines.push(format!("{border}{}{ANSI_RESET}", "".repeat(table_width)));
        }

        // Header row
        lines.push(format!(
            "{border}{ANSI_RESET} {header_color}{:width$}{ANSI_RESET}  {header_color}{:pwidth$}{ANSI_RESET} {border}{ANSI_RESET}",
            "Method",
            "Path",
            width = method_width,
            pwidth = path_width + 4
        ));

        lines.push(format!("{border}{}{ANSI_RESET}", "".repeat(table_width)));

        // Routes
        for route in routes {
            let method_bg = self.method_color(&route.method).to_ansi_bg();

            // Format path with tags
            let mut path_display = route.path.clone();
            if self.config.show_tags && !route.tags.is_empty() {
                use std::fmt::Write;
                let _ = write!(path_display, " [{}]", route.tags.join(", "));
            }

            // Truncate if too long
            if path_display.len() > path_width + 4 {
                path_display = format!("{}...", &path_display[..=path_width]);
            }

            let deprecated_marker = if route.deprecated {
                let warning = self.theme.warning.to_ansi_fg();
                format!(" {warning}{ANSI_RESET}")
            } else {
                String::new()
            };

            lines.push(format!(
                "{border}{ANSI_RESET} {method_bg}{ANSI_BOLD} {:width$} {ANSI_RESET}  {}{}{} {border}{ANSI_RESET}",
                route.method,
                path_display,
                deprecated_marker,
                " ".repeat((path_width + 4).saturating_sub(path_display.len() + deprecated_marker.len() / 10)),
                width = method_width
            ));
        }

        // Bottom border
        lines.push(format!("{border}{}{ANSI_RESET}", "".repeat(table_width)));

        // Summary
        let success = self.theme.success.to_ansi_fg();
        lines.push(format!(
            "{success}{ANSI_RESET} {muted}Total: {} route(s) registered{ANSI_RESET}",
            routes.len()
        ));

        lines.join("\n")
    }

    fn method_color(&self, method: &str) -> crate::themes::Color {
        match method.to_uppercase().as_str() {
            "GET" => self.theme.http_get,
            "POST" => self.theme.http_post,
            "PUT" => self.theme.http_put,
            "DELETE" => self.theme.http_delete,
            "PATCH" => self.theme.http_patch,
            "OPTIONS" => self.theme.http_options,
            "HEAD" => self.theme.http_head,
            _ => self.theme.muted,
        }
    }
}

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

    fn sample_routes() -> Vec<RouteEntry> {
        vec![
            RouteEntry::new("GET", "/api/users")
                .handler("list_users")
                .tag("users"),
            RouteEntry::new("POST", "/api/users")
                .handler("create_user")
                .tag("users"),
            RouteEntry::new("GET", "/api/users/{id}")
                .handler("get_user")
                .tag("users"),
            RouteEntry::new("DELETE", "/api/users/{id}")
                .handler("delete_user")
                .tag("users")
                .deprecated(true),
        ]
    }

    #[test]
    fn test_route_entry_builder() {
        let route = RouteEntry::new("POST", "/api/items")
            .handler("create_item")
            .tags(["items", "v2"])
            .deprecated(false);

        assert_eq!(route.method, "POST");
        assert_eq!(route.path, "/api/items");
        assert_eq!(route.handler, Some("create_item".to_string()));
        assert_eq!(route.tags, vec!["items", "v2"]);
        assert!(!route.deprecated);
    }

    #[test]
    fn test_route_display_plain() {
        let display = RouteDisplay::new(OutputMode::Plain);
        let routes = sample_routes();

        let output = display.render(&routes);

        assert!(output.contains("Registered Routes"));
        assert!(output.contains("GET"));
        assert!(output.contains("POST"));
        assert!(output.contains("/api/users"));
        assert!(output.contains("list_users"));
        assert!(output.contains("4 route(s)"));
        assert!(!output.contains("\x1b["));
    }

    #[test]
    fn test_route_display_empty() {
        let display = RouteDisplay::new(OutputMode::Plain);

        let output = display.render(&[]);

        assert!(output.contains("No routes registered"));
    }

    #[test]
    fn test_route_display_rich_has_ansi() {
        let display = RouteDisplay::new(OutputMode::Rich);
        let routes = sample_routes();

        let output = display.render(&routes);

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

    #[test]
    fn test_route_display_hide_deprecated() {
        let config = RouteTableConfig {
            show_deprecated: false,
            ..Default::default()
        };
        let display = RouteDisplay::with_config(OutputMode::Plain, config);
        let routes = sample_routes();

        let output = display.render(&routes);

        assert!(output.contains("3 route(s)")); // One deprecated route hidden
        assert!(!output.contains("deprecated"));
    }

    #[test]
    fn test_route_display_no_handlers() {
        let config = RouteTableConfig {
            show_handlers: false,
            ..Default::default()
        };
        let display = RouteDisplay::with_config(OutputMode::Plain, config);
        let routes = sample_routes();

        let output = display.render(&routes);

        assert!(!output.contains("list_users"));
    }

    #[test]
    fn test_route_display_no_tags() {
        let config = RouteTableConfig {
            show_tags: false,
            ..Default::default()
        };
        let display = RouteDisplay::with_config(OutputMode::Plain, config);
        let routes = sample_routes();

        let output = display.render(&routes);

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

    #[test]
    fn test_route_display_custom_title() {
        let config = RouteTableConfig {
            title: Some("API Endpoints".to_string()),
            ..Default::default()
        };
        let display = RouteDisplay::with_config(OutputMode::Plain, config);
        let routes = sample_routes();

        let output = display.render(&routes);

        assert!(output.contains("API Endpoints"));
    }
}