holoconf-core 0.5.1

Core configuration library with resolver support
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Error types for holoconf
//!
//! Error handling follows ADR-008: structured errors with context,
//! path information, and actionable help messages.

use std::fmt;

/// Result type alias for holoconf operations
pub type Result<T> = std::result::Result<T, Error>;

/// Main error type for holoconf operations
#[derive(Debug, Clone)]
pub struct Error {
    /// The kind of error that occurred
    pub kind: ErrorKind,
    /// Path in the config where the error occurred (e.g., "database.port")
    pub path: Option<String>,
    /// Source location (file, line) if available
    pub source_location: Option<SourceLocation>,
    /// Actionable help message
    pub help: Option<String>,
    /// Underlying cause (as string for Clone compatibility)
    pub cause: Option<String>,
}

/// Location in a source file
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceLocation {
    pub file: String,
    pub line: Option<usize>,
    pub column: Option<usize>,
}

/// Categories of errors that can occur
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
    /// Error parsing YAML/JSON
    Parse,
    /// Error during value resolution
    Resolver(ResolverErrorKind),
    /// Error during schema validation
    Validation,
    /// Error accessing a path that doesn't exist
    PathNotFound,
    /// Circular reference detected
    CircularReference,
    /// Type coercion failed
    TypeCoercion,
    /// I/O error (file not found, etc.)
    Io,
    /// Internal error (bug in holoconf)
    Internal,
}

/// Specific resolver error categories
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResolverErrorKind {
    /// Resource not found (triggers default handling if default is provided)
    /// This is used when the resolver cannot find the requested resource
    /// (e.g., env var not set, file not found, SSM parameter missing)
    NotFound { resource: String },
    /// Environment variable not found
    EnvNotFound { var_name: String },
    /// File not found
    FileNotFound { path: String },
    /// HTTP request failed
    HttpError { url: String, status: Option<u16> },
    /// HTTP resolver is disabled
    HttpDisabled,
    /// URL not in allowlist
    HttpNotAllowed { url: String },
    /// TLS configuration error
    TlsConfigError { message: String },
    /// Proxy configuration error
    ProxyConfigError { message: String },
    /// PEM file loading error
    PemLoadError { path: String, message: String },
    /// P12/PFX file loading error
    P12LoadError { path: String, message: String },
    /// Key decryption error
    KeyDecryptionError { message: String },
    /// Referenced config path not found
    RefNotFound { ref_path: String },
    /// Unknown resolver
    UnknownResolver { name: String },
    /// Resolver returned an error
    Custom { resolver: String, message: String },
    /// Resolver already registered
    AlreadyRegistered { name: String },
}

impl Error {
    /// Create a new parse error
    pub fn parse(message: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Parse,
            path: None,
            source_location: None,
            help: None,
            cause: Some(message.into()),
        }
    }

    /// Create a path not found error
    pub fn path_not_found(path: impl Into<String>) -> Self {
        let path_str = path.into();
        Self {
            kind: ErrorKind::PathNotFound,
            path: Some(path_str.clone()),
            source_location: None,
            help: Some(format!(
                "Check that '{}' exists in the configuration",
                path_str
            )),
            cause: None,
        }
    }

    /// Create a circular reference error
    pub fn circular_reference(path: impl Into<String>, chain: Vec<String>) -> Self {
        let chain_str = chain.join(" → ");
        Self {
            kind: ErrorKind::CircularReference,
            path: Some(path.into()),
            source_location: None,
            help: Some("Break the circular dependency by removing one of the references".into()),
            cause: Some(format!("Chain: {}", chain_str)),
        }
    }

    /// Create a not found error (triggers default handling at framework level)
    pub fn not_found(resource: impl Into<String>, config_path: Option<String>) -> Self {
        let res = resource.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::NotFound { resource: res }),
            path: config_path,
            source_location: None,
            help: None,
            cause: None,
        }
    }

    /// Create an env var not found error
    pub fn env_not_found(var_name: impl Into<String>, config_path: Option<String>) -> Self {
        let var = var_name.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::EnvNotFound {
                var_name: var.clone(),
            }),
            path: config_path,
            source_location: None,
            help: Some(format!(
                "Set the {} environment variable or provide a default: ${{env:{},default=value}}",
                var, var
            )),
            cause: None,
        }
    }

    /// Create a reference not found error
    pub fn ref_not_found(ref_path: impl Into<String>, config_path: Option<String>) -> Self {
        let ref_p = ref_path.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::RefNotFound {
                ref_path: ref_p.clone(),
            }),
            path: config_path,
            source_location: None,
            help: Some(format!(
                "Check that '{}' exists in the configuration",
                ref_p
            )),
            cause: None,
        }
    }

    /// Create a file not found error
    pub fn file_not_found(file_path: impl Into<String>, config_path: Option<String>) -> Self {
        let fp = file_path.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::FileNotFound { path: fp.clone() }),
            path: config_path,
            source_location: None,
            help: Some("Check that the file exists relative to the config file".into()),
            cause: None,
        }
    }

    /// Create an unknown resolver error
    pub fn unknown_resolver(name: impl Into<String>, config_path: Option<String>) -> Self {
        let n = name.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::UnknownResolver { name: n.clone() }),
            path: config_path,
            source_location: None,
            help: Some(format!("Register the '{}' resolver or check for typos", n)),
            cause: None,
        }
    }

    /// Create a resolver already registered error
    pub fn resolver_already_registered(name: impl Into<String>) -> Self {
        let n = name.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::AlreadyRegistered { name: n.clone() }),
            path: None,
            source_location: None,
            help: Some(format!(
                "Use register_with_force(..., force=true) to override the '{}' resolver",
                n
            )),
            cause: None,
        }
    }

    /// Create a type coercion error
    pub fn type_coercion(
        path: impl Into<String>,
        expected: impl Into<String>,
        got: impl Into<String>,
    ) -> Self {
        Self {
            kind: ErrorKind::TypeCoercion,
            path: Some(path.into()),
            source_location: None,
            help: Some(format!(
                "Ensure the value can be converted to {}",
                expected.into()
            )),
            cause: Some(format!("Got: {}", got.into())),
        }
    }

    /// Create a validation error
    pub fn validation(path: impl Into<String>, message: impl Into<String>) -> Self {
        let p = path.into();
        Self {
            kind: ErrorKind::Validation,
            path: if p.is_empty() || p == "<root>" {
                None
            } else {
                Some(p)
            },
            source_location: None,
            help: Some("Fix the value to match the schema requirements".into()),
            cause: Some(message.into()),
        }
    }

    /// Add path context to the error
    pub fn with_path(mut self, path: impl Into<String>) -> Self {
        self.path = Some(path.into());
        self
    }

    /// Add source location to the error
    pub fn with_source_location(mut self, loc: SourceLocation) -> Self {
        self.source_location = Some(loc);
        self
    }

    /// Add help message to the error
    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Create a custom resolver error
    pub fn resolver_custom(resolver: impl Into<String>, message: impl Into<String>) -> Self {
        let resolver_name = resolver.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::Custom {
                resolver: resolver_name.clone(),
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some(format!(
                "Check the '{}' resolver implementation",
                resolver_name
            )),
            cause: None,
        }
    }

    /// Create an HTTP request failed error
    pub fn http_request_failed(
        url: impl Into<String>,
        message: impl Into<String>,
        config_path: Option<String>,
    ) -> Self {
        let url_str = url.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::HttpError {
                url: url_str.clone(),
                status: None,
            }),
            path: config_path,
            source_location: None,
            help: Some(format!(
                "Check that the URL '{}' is accessible and returns valid content",
                url_str
            )),
            cause: Some(message.into()),
        }
    }

    /// Create an HTTP not in allowlist error
    pub fn http_not_in_allowlist(
        url: impl Into<String>,
        allowlist: &[String],
        config_path: Option<String>,
    ) -> Self {
        let url_str = url.into();
        let allowlist_str = if allowlist.is_empty() {
            "(empty)".to_string()
        } else {
            allowlist.join(", ")
        };

        let help_msg = if let Some(ref path) = config_path {
            format!(
                "The URL specified by '{}' is not in the allowlist.\n\
                 Update the allowlist or change the URL to match an allowed pattern.\n\
                 Current allowlist patterns: {}\n\
                 Use 'holoconf dump --include-sources' to see which file contains this value.",
                path, allowlist_str
            )
        } else {
            format!(
                "The URL is not in the allowlist.\n\
                 Current allowlist patterns: {}",
                allowlist_str
            )
        };

        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::HttpNotAllowed { url: url_str }),
            path: config_path,
            source_location: None,
            help: Some(help_msg),
            cause: None,
        }
    }

    /// Create a TLS configuration error
    pub fn tls_config_error(message: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::TlsConfigError {
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some("Check your TLS configuration (CA bundles, client certificates)".into()),
            cause: None,
        }
    }

    /// Create a proxy configuration error
    pub fn proxy_config_error(message: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::ProxyConfigError {
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some(
                "Check your proxy URL format (e.g., http://proxy:8080 or socks5://proxy:1080)"
                    .into(),
            ),
            cause: None,
        }
    }

    /// Create a PEM file loading error
    pub fn pem_load_error(path: impl Into<String>, message: impl Into<String>) -> Self {
        let path_str = path.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::PemLoadError {
                path: path_str.clone(),
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some(format!(
                "Ensure '{}' exists and contains valid PEM-encoded data",
                path_str
            )),
            cause: None,
        }
    }

    /// Create a P12/PFX file loading error
    pub fn p12_load_error(path: impl Into<String>, message: impl Into<String>) -> Self {
        let path_str = path.into();
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::P12LoadError {
                path: path_str.clone(),
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some(format!(
                "Ensure '{}' exists and contains a valid PKCS#12/PFX bundle with the correct password",
                path_str
            )),
            cause: None,
        }
    }

    /// Create a key decryption error
    pub fn key_decryption_error(message: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Resolver(ResolverErrorKind::KeyDecryptionError {
                message: message.into(),
            }),
            path: None,
            source_location: None,
            help: Some("Check that the password is correct for the encrypted private key".into()),
            cause: None,
        }
    }

    /// Create an internal error (bug in holoconf)
    pub fn internal(message: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Internal,
            path: None,
            source_location: None,
            help: Some("This is likely a bug in holoconf. Please report it.".into()),
            cause: Some(message.into()),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Main error message
        match &self.kind {
            ErrorKind::Parse => write!(f, "Parse error")?,
            ErrorKind::Resolver(r) => match r {
                ResolverErrorKind::NotFound { resource } => {
                    write!(f, "Resource not found: {}", resource)?
                }
                ResolverErrorKind::EnvNotFound { var_name } => {
                    write!(f, "Environment variable not found: {}", var_name)?
                }
                ResolverErrorKind::FileNotFound { path } => write!(f, "File not found: {}", path)?,
                ResolverErrorKind::HttpError { url, status } => {
                    write!(f, "HTTP request failed: {}", url)?;
                    if let Some(s) = status {
                        write!(f, " (status {})", s)?;
                    }
                }
                ResolverErrorKind::HttpDisabled => write!(f, "HTTP resolver is disabled")?,
                ResolverErrorKind::HttpNotAllowed { url } => {
                    write!(f, "URL not in allowlist: {}", url)?
                }
                ResolverErrorKind::RefNotFound { ref_path } => {
                    write!(f, "Referenced path not found: {}", ref_path)?
                }
                ResolverErrorKind::UnknownResolver { name } => {
                    write!(f, "Unknown resolver: {}", name)?
                }
                ResolverErrorKind::Custom { resolver, message } => {
                    write!(f, "Resolver '{}' error: {}", resolver, message)?
                }
                ResolverErrorKind::AlreadyRegistered { name } => {
                    write!(f, "Resolver '{}' is already registered", name)?
                }
                ResolverErrorKind::TlsConfigError { message } => {
                    write!(f, "TLS configuration error: {}", message)?
                }
                ResolverErrorKind::ProxyConfigError { message } => {
                    write!(f, "Proxy configuration error: {}", message)?
                }
                ResolverErrorKind::PemLoadError { path, message } => {
                    write!(f, "Failed to load PEM file '{}': {}", path, message)?
                }
                ResolverErrorKind::P12LoadError { path, message } => {
                    write!(f, "Failed to load P12/PFX file '{}': {}", path, message)?
                }
                ResolverErrorKind::KeyDecryptionError { message } => {
                    write!(f, "Failed to decrypt private key: {}", message)?
                }
            },
            ErrorKind::Validation => write!(f, "Validation error")?,
            ErrorKind::PathNotFound => write!(f, "Path not found")?,
            ErrorKind::CircularReference => write!(f, "Circular reference detected")?,
            ErrorKind::TypeCoercion => write!(f, "Type coercion failed")?,
            ErrorKind::Io => write!(f, "I/O error")?,
            ErrorKind::Internal => write!(f, "Internal error")?,
        }

        // Path context
        if let Some(path) = &self.path {
            write!(f, "\n  Path: {}", path)?;
        }

        // Source location
        if let Some(loc) = &self.source_location {
            write!(f, "\n  File: {}", loc.file)?;
            if let Some(line) = loc.line {
                write!(f, ":{}", line)?;
            }
        }

        // Cause
        if let Some(cause) = &self.cause {
            write!(f, "\n  {}", cause)?;
        }

        // Help
        if let Some(help) = &self.help {
            write!(f, "\n  Help: {}", help)?;
        }

        Ok(())
    }
}

impl std::error::Error for Error {}

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

    #[test]
    fn test_env_not_found_error_display() {
        let err = Error::env_not_found("MY_VAR", Some("database.password".into()));
        let display = format!("{}", err);

        assert!(display.contains("Environment variable not found: MY_VAR"));
        assert!(display.contains("Path: database.password"));
        assert!(display.contains("Help:"));
        assert!(display.contains("${env:MY_VAR,default=value}"));
    }

    #[test]
    fn test_circular_reference_error_display() {
        let err = Error::circular_reference(
            "config.a",
            vec!["a".into(), "b".into(), "c".into(), "a".into()],
        );
        let display = format!("{}", err);

        assert!(display.contains("Circular reference detected"));
        assert!(display.contains("a → b → c → a"));
    }

    #[test]
    fn test_path_not_found_error() {
        let err = Error::path_not_found("database.host");

        assert_eq!(err.kind, ErrorKind::PathNotFound);
        assert_eq!(err.path, Some("database.host".into()));
    }

    #[test]
    fn test_not_found_error() {
        let err = Error::not_found("my-resource", Some("config.key".into()));
        let display = format!("{}", err);

        assert!(display.contains("Resource not found: my-resource"));
        assert!(display.contains("Path: config.key"));
        assert!(matches!(
            err.kind,
            ErrorKind::Resolver(ResolverErrorKind::NotFound { .. })
        ));
    }

    #[test]
    fn test_ref_not_found_error() {
        let err = Error::ref_not_found("database.missing", Some("app.db".into()));
        let display = format!("{}", err);

        assert!(display.contains("Referenced path not found: database.missing"));
        assert!(display.contains("Path: app.db"));
        assert!(display.contains("Help:"));
    }

    #[test]
    fn test_file_not_found_error() {
        let err = Error::file_not_found("/path/to/missing.yaml", Some("config.file".into()));
        let display = format!("{}", err);

        assert!(display.contains("File not found: /path/to/missing.yaml"));
        assert!(display.contains("Path: config.file"));
    }

    #[test]
    fn test_unknown_resolver_error() {
        let err = Error::unknown_resolver("unknown", Some("config.value".into()));
        let display = format!("{}", err);

        assert!(display.contains("Unknown resolver: unknown"));
        assert!(display.contains("Help:"));
        assert!(display.contains("Register the 'unknown' resolver"));
    }

    #[test]
    fn test_resolver_custom_error() {
        let err = Error::resolver_custom("myresolver", "Something went wrong");
        let display = format!("{}", err);

        assert!(display.contains("Resolver 'myresolver' error: Something went wrong"));
        assert!(display.contains("Help:"));
    }

    #[test]
    fn test_internal_error() {
        let err = Error::internal("Unexpected state");
        let display = format!("{}", err);

        assert!(display.contains("Internal error"));
        // The message goes into the cause field
        assert!(display.contains("Unexpected state"));
    }

    #[test]
    fn test_with_source_location() {
        let err = Error::parse("syntax error").with_source_location(SourceLocation {
            file: "config.yaml".into(),
            line: Some(42),
            column: None,
        });
        let display = format!("{}", err);

        assert!(display.contains("config.yaml:42"));
    }

    #[test]
    fn test_with_help() {
        let err = Error::parse("bad input").with_help("Try fixing the syntax");
        let display = format!("{}", err);

        assert!(display.contains("Help: Try fixing the syntax"));
    }

    #[test]
    fn test_type_coercion_error() {
        let err = Error::type_coercion("server.port", "integer", "string");
        let display = format!("{}", err);

        assert!(display.contains("Type coercion failed"));
        assert!(display.contains("Path: server.port"));
        assert!(display.contains("Got: string"));
    }

    #[test]
    fn test_validation_error() {
        let err = Error::validation("users[0].name", "must be at least 3 characters");
        let display = format!("{}", err);

        assert!(display.contains("Validation error"));
        assert!(display.contains("Path: users[0].name"));
        assert!(display.contains("must be at least 3 characters"));
    }

    #[test]
    fn test_validation_error_root_path() {
        // Root path should not show path field
        let err = Error::validation("<root>", "missing required field");
        assert!(err.path.is_none());

        let err2 = Error::validation("", "missing required field");
        assert!(err2.path.is_none());
    }

    #[test]
    fn test_http_error_display() {
        let err = Error {
            kind: ErrorKind::Resolver(ResolverErrorKind::HttpError {
                url: "https://example.com/config".into(),
                status: Some(404),
            }),
            path: Some("remote.config".into()),
            source_location: None,
            help: None,
            cause: None,
        };
        let display = format!("{}", err);

        assert!(display.contains("HTTP request failed: https://example.com/config"));
        assert!(display.contains("status 404"));
    }

    #[test]
    fn test_http_disabled_error() {
        let err = Error {
            kind: ErrorKind::Resolver(ResolverErrorKind::HttpDisabled),
            path: Some("remote.config".into()),
            source_location: None,
            help: None,
            cause: None,
        };
        let display = format!("{}", err);

        assert!(display.contains("HTTP resolver is disabled"));
    }
}