inspect_rs/lib.rs
1//! # inspect-rs
2//!
3//! Universal introspection for Rust.
4//!
5//! `inspect-rs` provides a lightweight protocol for structured introspection
6//! of Rust values without coupling to debuggers, serializers, or specific
7//! UI frameworks.
8//!
9//! ## Quick Start
10//!
11//! ```
12//! use inspect_rs::Inspect;
13//!
14//! #[derive(Inspect)]
15//! struct User {
16//! id: u64,
17//! name: String,
18//! active: bool,
19//! }
20//!
21//! let user = User {
22//! id: 42,
23//! name: "Alice".to_string(),
24//! active: true,
25//! };
26//!
27//! let mut cx = inspect_rs::InspectCx::new();
28//! let inspected = user.inspect(&mut cx);
29//!
30//! println!("{}", inspect_rs::format_tree(&inspected));
31//! ```
32//!
33//! ## Design Principles
34//!
35//! - **Lazy**: Values are not eagerly traversed
36//! - **Zero-copy**: Borrows from original values where possible
37//! - **Safe**: Handles cycles, respects limits, protects secrets
38//! - **Minimal**: Zero runtime dependencies in core
39//! - **Semantic Styling**: Restrained, accessible, terminal-aware colored output
40
41pub use inspect_core::{
42 Capability, Children, DepthGuard, FieldInfo, Inspect, InspectCx, InspectError, InspectLimits,
43 InspectPath, InspectResult, Kind, PathSegment, Sensitivity, TypeInfo, ValueRef, VariantInfo,
44};
45pub use inspect_derive::Inspect;
46#[cfg(feature = "color")]
47pub use inspect_format::{Ansi256Color, Color, Effects, Reset, RgbColor};
48pub use inspect_format::{
49 AnsiColor, ColorChoice, ColorLevel, Style, StyleRole, Theme, TreeConfig, TreeFormatter,
50};
51
52/// Format an inspected value as a tree with automatic terminal color detection.
53///
54/// If stdout is a TTY and environment variables allow it, semantic colors are applied.
55/// If output is redirected to a file or pipe, colors are automatically omitted.
56///
57/// # Example
58///
59/// ```
60/// use inspect_rs::{Inspect, InspectCx, format_tree};
61///
62/// let value = vec![1, 2, 3];
63/// let mut cx = InspectCx::new();
64/// let inspected = value.inspect(&mut cx);
65///
66/// println!("{}", format_tree(&inspected));
67/// ```
68pub fn format_tree(value: &ValueRef<'_>) -> String {
69 TreeFormatter::new().format(value)
70}
71
72/// Format an inspected value as a compact tree.
73pub fn format_tree_compact(value: &ValueRef<'_>) -> String {
74 TreeFormatter::with_config(TreeConfig::compact()).format(value)
75}
76
77/// Format an inspected value as a tree with ANSI color codes explicitly enabled.
78pub fn format_tree_colored(value: &ValueRef<'_>) -> String {
79 TreeFormatter::new().format_colored(value)
80}
81
82/// Format an inspected value as a tree with ANSI color codes explicitly disabled.
83pub fn format_tree_plain(value: &ValueRef<'_>) -> String {
84 TreeFormatter::new().format_plain(value)
85}
86
87/// Format an inspected value as a tree using a custom [`TreeConfig`].
88pub fn format_tree_with(value: &ValueRef<'_>, config: TreeConfig) -> String {
89 TreeFormatter::with_config(config).format(value)
90}