chroma_dbg/
config.rs

1#[derive(Debug, Clone)]
2pub struct ChromaConfig {
3    pub inline_struct: InlineThreshold,
4    pub inline_array: InlineThreshold,
5    pub integer_format: IntegerFormat,
6
7    pub identifier_color: Color,
8    /// Color used for integers, floats, and booleans.
9    pub numerical_color: Color,
10    /// Color used for string literals.
11    pub string_color: Color,
12    /// Color used for string escape sequences in string literals.
13    pub string_escape_color: Color,
14    /// Color used for field names.
15    pub field_color: Color,
16}
17
18impl ChromaConfig {
19    pub const DEFAULT: ChromaConfig = ChromaConfig {
20        inline_struct: InlineThreshold::MaxLength(64),
21        inline_array: InlineThreshold::MaxLength(64),
22        integer_format: IntegerFormat::HexWhenOver(8192),
23
24        identifier_color: Color(19, 220, 242),
25        numerical_color: Color(200, 129, 255),
26        string_color: Color(232, 219, 97),
27        string_escape_color: Color(220, 108, 101),
28        field_color: Color(255, 255, 255),
29    };
30
31    pub const COMPACT: ChromaConfig = ChromaConfig {
32        inline_struct: InlineThreshold::Always,
33        inline_array: InlineThreshold::Always,
34        ..Self::DEFAULT
35    };
36}
37
38impl Default for ChromaConfig {
39    fn default() -> Self {
40        ChromaConfig::DEFAULT
41    }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum InlineThreshold {
46    /// Never inline
47    Never,
48    /// Always inline
49    Always,
50    /// Maximum length of a single line before it is considered too long to be inline.
51    MaxLength(usize),
52}
53
54impl InlineThreshold {
55    pub fn should_inline(&self, len: usize) -> bool {
56        match self {
57            InlineThreshold::Never => false,
58            InlineThreshold::Always => true,
59            InlineThreshold::MaxLength(max) => len <= *max,
60        }
61    }
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum IntegerFormat {
66    /// Always render integers as decimal.
67    AlwaysDecimal,
68    /// Always render integers as hexadecimal. Note that negative numbers will still be rendered as decimal.
69    AlwaysHex,
70    /// Render integers as hexadecimal when they are over a certain size.
71    HexWhenOver(u64),
72}
73
74impl IntegerFormat {
75    pub fn format(&self, value: u64) -> String {
76        match self {
77            IntegerFormat::AlwaysDecimal => value.to_string(),
78            IntegerFormat::AlwaysHex => format!("0x{:x}", value),
79            IntegerFormat::HexWhenOver(max) if value > *max => format!("0x{:x}", value),
80            IntegerFormat::HexWhenOver(_) => value.to_string(),
81        }
82    }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub struct Color(pub u8, pub u8, pub u8);
87
88impl From<Color> for anstyle::RgbColor {
89    fn from(color: Color) -> Self {
90        Self(color.0, color.1, color.2)
91    }
92}