tryphon 0.2.0

Type-safe configuration loading from environment variables using derive macros
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
use crate::printer::field_path::FieldPath;
use crate::{ConfigError, ConfigFieldError};

pub(crate) struct TablePrinter {
    rows: Vec<(String, String, String)>,
}

impl TablePrinter {
    pub(crate) fn new() -> Self {
        TablePrinter { rows: vec![] }
    }

    fn collect_errors_as_rows(
        &mut self,
        errors: &Vec<ConfigFieldError>,
        parent_field_path: FieldPath,
    ) {
        for error in errors {
            match error {
                ConfigFieldError::Nested {
                    field_name,
                    field_idx,
                    error: ConfigError { field_errors },
                } => {
                    let field_path = parent_field_path
                        .with_segment(field_name.clone().unwrap_or(field_idx.to_string()).as_str());
                    self.collect_errors_as_rows(field_errors, field_path);
                }
                ConfigFieldError::ParsingError {
                    field_name,
                    field_idx,
                    raw,
                    message,
                    env_var_name,
                } => {
                    let field_path = parent_field_path
                        .with_segment(field_name.clone().unwrap_or(field_idx.to_string()).as_str());
                    self.rows.push((
                        field_path.dotted_path(),
                        env_var_name.clone(),
                        format!("{} (raw value: '{}')", message, raw),
                    ));
                }
                ConfigFieldError::MissingValue {
                    field_name,
                    field_idx,
                    env_vars,
                } => {
                    let field_path = parent_field_path
                        .with_segment(field_name.clone().unwrap_or(field_idx.to_string()).as_str());
                    self.rows.push((
                        field_path.dotted_path(),
                        env_vars.join(", "),
                        "Required variable not set".to_string(),
                    ));
                }
                ConfigFieldError::Other {
                    field_name,
                    field_idx,
                    message,
                } => {
                    let field_path = parent_field_path
                        .with_segment(field_name.clone().unwrap_or(field_idx.to_string()).as_str());
                    self.rows
                        .push((field_path.dotted_path(), "-".to_string(), message.clone()));
                }
            }
        }
    }

    pub(crate) fn print(&mut self, errors: &Vec<ConfigFieldError>) -> String {
        self.collect_errors_as_rows(errors, FieldPath::root());

        if self.rows.is_empty() {
            return "No configuration errors\n".to_string();
        }

        let headers = ["Field Name", "Environment Variables", "Error Details"];

        format_ascii_table(&headers, &self.rows)
    }
}

fn calculate_column_widths(headers: &[&str; 3], rows: &[(String, String, String)]) -> [usize; 3] {
    let mut widths = [headers[0].len(), headers[1].len(), headers[2].len()];

    for row in rows {
        widths[0] = widths[0].max(row.0.len());
        widths[1] = widths[1].max(row.1.len());
        widths[2] = widths[2].max(row.2.len());
    }

    widths
}

fn top_border(buffer: &mut String, widths: &[usize; 3]) {
    buffer.push_str(&format_border(widths, "", "", ""));
}

fn header_row(buffer: &mut String, headers: &[&str; 3], widths: &[usize; 3]) {
    buffer.push_str(&format_row(&[headers[0], headers[1], headers[2]], widths));
}

fn header_separator(buffer: &mut String, widths: &[usize; 3]) {
    buffer.push_str(&format_border(widths, "", "", ""));
}

fn data_rows(buffer: &mut String, rows: &[(String, String, String)], widths: &[usize; 3]) {
    for row in rows {
        let row_strs = [row.0.as_str(), row.1.as_str(), row.2.as_str()];
        buffer.push_str(&format_row(&row_strs, widths));
    }
}

fn bottom_border(buffer: &mut String, widths: &[usize; 3]) {
    buffer.push_str(&format_border(widths, "", "", ""));
}

fn format_ascii_table(headers: &[&str; 3], rows: &[(String, String, String)]) -> String {
    let widths = calculate_column_widths(headers, rows);
    let mut output = String::new();

    top_border(&mut output, &widths);
    header_row(&mut output, headers, &widths);
    header_separator(&mut output, &widths);
    data_rows(&mut output, rows, &widths);
    bottom_border(&mut output, &widths);

    output
}

fn format_border(widths: &[usize; 3], left: &str, mid: &str, right: &str) -> String {
    format!(
        "{}{}{}{}{}{}{}\n",
        left,
        "".repeat(widths[0] + 2),
        mid,
        "".repeat(widths[1] + 2),
        mid,
        "".repeat(widths[2] + 2),
        right
    )
}

fn format_row(cells: &[&str; 3], widths: &[usize; 3]) -> String {
    format!(
        "│ {:<width0$} │ {:<width1$} │ {:<width2$} │\n",
        cells[0],
        cells[1],
        cells[2],
        width0 = widths[0],
        width1 = widths[1],
        width2 = widths[2]
    )
}

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

    #[test]
    fn test_empty_error_list() {
        let mut printer = TablePrinter::new();
        let errors = vec![];
        let result = printer.print(&errors);

        assert_eq!(result, "No configuration errors\n");
    }

    #[test]
    fn test_single_parsing_error() {
        let mut printer = TablePrinter::new();
        let errors = vec![ConfigFieldError::ParsingError {
            field_idx: 0,
            field_name: Some("port".to_string()),
            raw: "invalid".to_string(),
            message: "invalid digit found in string".to_string(),
            env_var_name: "PORT".to_string(),
        }];

        let result = printer.print(&errors);

        assert!(result.contains("Field Name"));
        assert!(result.contains("Environment Variables"));
        assert!(result.contains("Error Details"));
        assert!(result.contains("port"));
        assert!(result.contains("PORT"));
        assert!(result.contains("invalid digit found in string"));
        assert!(result.contains("(raw value: 'invalid')"));
        assert!(result.contains(""));
        assert!(result.contains(""));
    }

    #[test]
    fn test_single_missing_value_error() {
        let mut printer = TablePrinter::new();
        let errors = vec![ConfigFieldError::MissingValue {
            field_name: Some("database_url".to_string()),
            field_idx: 0,
            env_vars: vec!["DATABASE_URL".to_string(), "DB_URL".to_string()],
        }];

        let result = printer.print(&errors);

        assert!(result.contains("database_url"));
        assert!(result.contains("DATABASE_URL, DB_URL"));
        assert!(result.contains("Required variable not set"));
    }

    #[test]
    fn test_single_other_error() {
        let mut printer = TablePrinter::new();
        let errors = vec![ConfigFieldError::Other {
            field_idx: 0,
            field_name: Some("custom_field".to_string()),
            message: "custom validation failed".to_string(),
        }];

        let result = printer.print(&errors);

        assert!(result.contains("custom_field"));
        assert!(result.contains("-"));
        assert!(result.contains("custom validation failed"));
    }

    #[test]
    fn test_nested_error() {
        let mut printer = TablePrinter::new();
        let nested_errors = vec![ConfigFieldError::ParsingError {
            field_idx: 0,
            field_name: Some("host".to_string()),
            raw: "".to_string(),
            message: "empty string not allowed".to_string(),
            env_var_name: "DB_HOST".to_string(),
        }];

        let errors = vec![ConfigFieldError::Nested {
            field_idx: 0,
            field_name: Some("database".to_string()),
            error: ConfigError {
                field_errors: nested_errors,
            },
        }];

        let result = printer.print(&errors);

        assert!(result.contains("database.host"));
        assert!(result.contains("DB_HOST"));
    }

    #[test]
    fn test_multiple_errors_at_same_level() {
        let mut printer = TablePrinter::new();
        let errors = vec![
            ConfigFieldError::MissingValue {
                field_name: Some("api_key".to_string()),
                field_idx: 0,
                env_vars: vec!["API_KEY".to_string()],
            },
            ConfigFieldError::ParsingError {
                field_idx: 1,
                field_name: Some("timeout".to_string()),
                raw: "abc".to_string(),
                message: "invalid digit found in string".to_string(),
                env_var_name: "TIMEOUT".to_string(),
            },
            ConfigFieldError::Other {
                field_idx: 2,
                field_name: Some("region".to_string()),
                message: "unsupported region".to_string(),
            },
        ];

        let result = printer.print(&errors);

        assert!(result.contains("api_key"));
        assert!(result.contains("timeout"));
        assert!(result.contains("region"));
        // Check for proper table structure with 3 rows
        let border_count = result.matches("").count();
        assert_eq!(border_count, 1); // Only header separator
    }

    #[test]
    fn test_deeply_nested_errors() {
        let mut printer = TablePrinter::new();

        // Create a deeply nested structure: app.database.connection.pool_size
        let deepest_error = vec![ConfigFieldError::ParsingError {
            field_idx: 0,
            field_name: Some("pool_size".to_string()),
            raw: "not_a_number".to_string(),
            message: "invalid digit found in string".to_string(),
            env_var_name: "POOL_SIZE".to_string(),
        }];

        let connection_error = vec![ConfigFieldError::Nested {
            field_idx: 0,
            field_name: Some("connection".to_string()),
            error: ConfigError {
                field_errors: deepest_error,
            },
        }];

        let database_error = vec![ConfigFieldError::Nested {
            field_idx: 0,
            field_name: Some("database".to_string()),
            error: ConfigError {
                field_errors: connection_error,
            },
        }];

        let errors = vec![ConfigFieldError::Nested {
            field_idx: 0,
            field_name: Some("app".to_string()),
            error: ConfigError {
                field_errors: database_error,
            },
        }];

        let result = printer.print(&errors);

        assert!(result.contains("app.database.connection.pool_size"));
    }

    #[test]
    fn test_field_without_name_uses_index() {
        let mut printer = TablePrinter::new();
        let errors = vec![ConfigFieldError::ParsingError {
            field_idx: 2,
            field_name: None,
            raw: "invalid".to_string(),
            message: "parse error".to_string(),
            env_var_name: "FIELD_2".to_string(),
        }];

        let result = printer.print(&errors);

        assert!(result.contains("2"));
    }

    #[test]
    fn test_multiple_nested_structures() {
        let mut printer = TablePrinter::new();

        let db_errors = vec![ConfigFieldError::MissingValue {
            field_name: Some("host".to_string()),
            field_idx: 0,
            env_vars: vec!["DB_HOST".to_string()],
        }];

        let cache_errors = vec![ConfigFieldError::ParsingError {
            field_idx: 0,
            field_name: Some("ttl".to_string()),
            raw: "forever".to_string(),
            message: "invalid duration".to_string(),
            env_var_name: "CACHE_TTL".to_string(),
        }];

        let errors = vec![
            ConfigFieldError::Nested {
                field_idx: 0,
                field_name: Some("database".to_string()),
                error: ConfigError {
                    field_errors: db_errors,
                },
            },
            ConfigFieldError::Nested {
                field_idx: 1,
                field_name: Some("cache".to_string()),
                error: ConfigError {
                    field_errors: cache_errors,
                },
            },
        ];

        let result = printer.print(&errors);

        assert!(result.contains("database.host"));
        assert!(result.contains("cache.ttl"));
    }

    #[test]
    fn test_table_structure() {
        let mut printer = TablePrinter::new();
        let errors = vec![ConfigFieldError::Other {
            field_idx: 0,
            field_name: Some("test".to_string()),
            message: "test error".to_string(),
        }];

        let result = printer.print(&errors);

        // Check for proper box drawing characters
        assert!(result.contains("")); // Top-left corner
        assert!(result.contains("")); // Top-right corner
        assert!(result.contains("")); // Bottom-left corner
        assert!(result.contains("")); // Bottom-right corner
        assert!(result.contains("")); // Left T-junction
        assert!(result.contains("")); // Right T-junction
        assert!(result.contains("")); // Cross
        assert!(result.contains("")); // Horizontal line
        assert!(result.contains("")); // Vertical line
    }

    #[test]
    fn test_column_width_adjustment() {
        let mut printer = TablePrinter::new();
        let errors = vec![
            ConfigFieldError::Other {
                field_idx: 0,
                field_name: Some("short".to_string()),
                message: "short".to_string(),
            },
            ConfigFieldError::Other {
                field_idx: 1,
                field_name: Some("very_long_field_name_that_should_adjust_width".to_string()),
                message: "This is a very long error message that should cause the column to expand"
                    .to_string(),
            },
        ];

        let result = printer.print(&errors);

        // Verify both errors are present
        assert!(result.contains("short"));
        assert!(result.contains("very_long_field_name_that_should_adjust_width"));
        assert!(result.contains("This is a very long error message"));

        // Verify table structure is maintained
        let lines: Vec<&str> = result.lines().collect();
        if lines.len() > 2 {
            // All content rows should have the same width
            let first_line_len = lines[0].chars().count();
            for line in &lines {
                assert_eq!(
                    line.chars().count(),
                    first_line_len,
                    "All rows should have equal width"
                );
            }
        }
    }
}