nautilus-common 0.59.0

Common functionality and machinery for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Path-aware configuration validation errors and checks.

use std::{error::Error, fmt::Display};

/// Result type for configuration validation.
pub type ConfigResult<T> = Result<T, ConfigError>;

/// A typed configuration validation error with owned field paths.
///
/// Variants store owned field paths and explanatory text. Callers should avoid placing
/// secrets in reason strings, reference names, or duplicate labels.
#[allow(
    clippy::module_name_repetitions,
    reason = "public name states the error domain when imported outside the module"
)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
    /// A field is not supported by the current runtime.
    UnsupportedField { field: String, reason: String },
    /// A field value is not supported by the current runtime.
    UnsupportedValue { field: String, reason: String },
    /// A required field is missing.
    MissingField { field: String },
    /// A required field is present but empty.
    EmptyField { field: String },
    /// A field value is invalid.
    InvalidValue { field: String, reason: String },
    /// A field value has an invalid format.
    InvalidFormat { field: String, expected: String },
    /// A field value is outside the accepted range.
    Range { field: String, reason: String },
    /// Fields were set together but are mutually exclusive.
    MutuallyExclusiveFields { fields: Vec<String> },
    /// At least one of the listed fields is required.
    RequiredOneOf { fields: Vec<String> },
    /// A field requires another field.
    Dependency {
        field: String,
        depends_on: String,
        reason: String,
    },
    /// A field contains a duplicate entry.
    Duplicate {
        field: String,
        value: Option<String>,
    },
    /// A field requires a disabled feature.
    FeatureDisabled { field: String, feature: String },
    /// A field references an invalid object.
    InvalidReference {
        field: String,
        reference: String,
        reason: String,
    },
    /// Multiple config validation errors were collected.
    Multiple { errors: Vec<Self> },
}

impl ConfigError {
    /// Creates an unsupported-field error.
    pub fn unsupported_field(field: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::UnsupportedField {
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Creates an unsupported-value error.
    pub fn unsupported_value(field: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::UnsupportedValue {
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Creates a missing-field error.
    pub fn missing_field(field: impl Into<String>) -> Self {
        Self::MissingField {
            field: field.into(),
        }
    }

    /// Creates an empty-field error.
    pub fn empty_field(field: impl Into<String>) -> Self {
        Self::EmptyField {
            field: field.into(),
        }
    }

    /// Creates an invalid-value error.
    pub fn invalid_value(field: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::InvalidValue {
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Creates an invalid-format error.
    pub fn invalid_format(field: impl Into<String>, expected: impl Into<String>) -> Self {
        Self::InvalidFormat {
            field: field.into(),
            expected: expected.into(),
        }
    }

    /// Creates a range error.
    pub fn range(field: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::Range {
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Creates a mutually-exclusive-fields error.
    pub fn mutually_exclusive_fields(fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::MutuallyExclusiveFields {
            fields: fields.into_iter().map(Into::into).collect(),
        }
    }

    /// Creates a required-one-of error.
    pub fn required_one_of(fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::RequiredOneOf {
            fields: fields.into_iter().map(Into::into).collect(),
        }
    }

    /// Creates a dependency error.
    pub fn dependency(
        field: impl Into<String>,
        depends_on: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self::Dependency {
            field: field.into(),
            depends_on: depends_on.into(),
            reason: reason.into(),
        }
    }

    /// Creates a duplicate-entry error.
    pub fn duplicate(field: impl Into<String>, value: Option<String>) -> Self {
        Self::Duplicate {
            field: field.into(),
            value,
        }
    }

    /// Creates a feature-disabled error.
    pub fn feature_disabled(field: impl Into<String>, feature: impl Into<String>) -> Self {
        Self::FeatureDisabled {
            field: field.into(),
            feature: feature.into(),
        }
    }

    /// Creates an invalid-reference error.
    pub fn invalid_reference(
        field: impl Into<String>,
        reference: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self::InvalidReference {
            field: field.into(),
            reference: reference.into(),
            reason: reason.into(),
        }
    }

    /// Creates a multiple-errors error.
    pub fn multiple(errors: Vec<Self>) -> Self {
        Self::Multiple { errors }
    }
}

impl Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnsupportedField { field, reason } => write!(f, "{field} is {reason}"),
            Self::UnsupportedValue { field, reason } => {
                write!(f, "{field} has unsupported value: {reason}")
            }
            Self::MissingField { field } => write!(f, "{field} is required"),
            Self::EmptyField { field } => write!(f, "{field} must not be empty"),
            Self::InvalidValue { field, reason } | Self::Range { field, reason } => {
                write!(f, "invalid {field}: {reason}")
            }
            Self::InvalidFormat { field, expected } => write!(f, "invalid {field}: {expected}"),
            Self::MutuallyExclusiveFields { fields } => {
                write!(f, "mutually exclusive fields: ")?;
                write_fields(f, fields)
            }
            Self::RequiredOneOf { fields } => {
                write!(f, "one of these fields is required: ")?;
                write_fields(f, fields)
            }
            Self::Dependency {
                field,
                depends_on,
                reason,
            } => write!(f, "{field} requires {depends_on}: {reason}"),
            Self::Duplicate { field, value } => match value {
                Some(value) => write!(f, "duplicate {field}: {value}"),
                None => write!(f, "duplicate {field}"),
            },
            Self::FeatureDisabled { field, feature } => {
                write!(f, "{field} requires feature `{feature}`")
            }
            Self::InvalidReference {
                field,
                reference,
                reason,
            } => write!(f, "invalid {field} reference {reference}: {reason}"),
            Self::Multiple { errors } => {
                write!(f, "multiple config validation errors")?;
                if !errors.is_empty() {
                    write!(f, ": ")?;

                    for (index, error) in errors.iter().enumerate() {
                        if index > 0 {
                            write!(f, "; ")?;
                        }
                        write!(f, "{}. {error}", index + 1)?;
                    }
                }
                Ok(())
            }
        }
    }
}

impl Error for ConfigError {}

fn write_fields(f: &mut std::fmt::Formatter<'_>, fields: &[String]) -> std::fmt::Result {
    for (index, field) in fields.iter().enumerate() {
        if index > 0 {
            write!(f, ", ")?;
        }
        write!(f, "{field}")?;
    }
    Ok(())
}

/// Collects configuration validation errors.
#[allow(
    clippy::module_name_repetitions,
    reason = "public name states the error domain when imported outside the module"
)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ConfigErrorCollector {
    errors: Vec<ConfigError>,
}

impl ConfigErrorCollector {
    /// Creates an empty collector.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates an empty collector with capacity for `capacity` errors.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            errors: Vec::with_capacity(capacity),
        }
    }

    /// Returns `true` if no errors have been collected.
    pub fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }

    /// Returns the number of collected errors.
    pub fn len(&self) -> usize {
        self.errors.len()
    }

    /// Returns the collected errors.
    pub fn errors(&self) -> &[ConfigError] {
        &self.errors
    }

    /// Adds an error to the collection.
    pub fn push(&mut self, error: ConfigError) {
        match error {
            ConfigError::Multiple { errors } => self.errors.extend(errors),
            error => self.errors.push(error),
        }
    }

    /// Adds `error` when `condition` is false.
    pub fn check(&mut self, condition: bool, error: ConfigError) {
        if !condition {
            self.push(error);
        }
    }

    /// Adds the error from a validation result.
    pub fn collect(&mut self, result: ConfigResult<()>) {
        if let Err(e) = result {
            self.push(e);
        }
    }

    /// Converts the collection to a validation result.
    ///
    /// # Errors
    ///
    /// Returns the collected error, or a [`ConfigError::Multiple`] when more than one error
    /// was collected.
    pub fn into_result(self) -> ConfigResult<()> {
        let mut errors = self.errors;
        if errors.is_empty() {
            Ok(())
        } else if errors.len() == 1 {
            Err(errors.remove(0))
        } else {
            Err(ConfigError::Multiple { errors })
        }
    }
}

/// Checks a boolean validation condition.
///
/// # Errors
///
/// Returns `error` when `condition` is false.
pub fn check(condition: bool, config_error: ConfigError) -> ConfigResult<()> {
    if condition { Ok(()) } else { Err(config_error) }
}

/// Checks that a field is supported.
///
/// # Errors
///
/// Returns [`ConfigError::UnsupportedField`] when `supported` is false.
pub fn check_supported_field(
    field: impl Into<String>,
    supported: bool,
    reason: impl Into<String>,
) -> ConfigResult<()> {
    check(supported, ConfigError::unsupported_field(field, reason))
}

/// Checks that a field value is supported.
///
/// # Errors
///
/// Returns [`ConfigError::UnsupportedValue`] when `supported` is false.
pub fn check_supported_value(
    field: impl Into<String>,
    supported: bool,
    reason: impl Into<String>,
) -> ConfigResult<()> {
    check(supported, ConfigError::unsupported_value(field, reason))
}

/// Checks that a string field is present and non-empty after trimming.
///
/// # Errors
///
/// Returns [`ConfigError::EmptyField`] when `value` is empty after trimming.
pub fn check_non_empty_field(field: impl Into<String>, value: &str) -> ConfigResult<()> {
    check(!value.trim().is_empty(), ConfigError::empty_field(field))
}

/// Checks that a field value is valid.
///
/// # Errors
///
/// Returns [`ConfigError::InvalidValue`] when `valid` is false.
pub fn check_valid_value(
    field: impl Into<String>,
    valid: bool,
    reason: impl Into<String>,
) -> ConfigResult<()> {
    check(valid, ConfigError::invalid_value(field, reason))
}

/// Checks that a field value has the expected format.
///
/// # Errors
///
/// Returns [`ConfigError::InvalidFormat`] when `valid` is false.
pub fn check_valid_format(
    field: impl Into<String>,
    valid: bool,
    expected: impl Into<String>,
) -> ConfigResult<()> {
    check(valid, ConfigError::invalid_format(field, expected))
}

/// Checks that a field value is in range.
///
/// # Errors
///
/// Returns [`ConfigError::Range`] when `in_range` is false.
pub fn check_range(
    field: impl Into<String>,
    in_range: bool,
    reason: impl Into<String>,
) -> ConfigResult<()> {
    check(in_range, ConfigError::range(field, reason))
}

/// Checks that a field's feature is enabled.
///
/// # Errors
///
/// Returns [`ConfigError::FeatureDisabled`] when `enabled` is false.
pub fn check_feature_enabled(
    field: impl Into<String>,
    feature: impl Into<String>,
    enabled: bool,
) -> ConfigResult<()> {
    check(enabled, ConfigError::feature_disabled(field, feature))
}

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

    use super::*;

    #[rstest]
    fn test_config_error_display_uses_field_path() {
        let error = ConfigError::invalid_format(
            "LiveNodeConfig.plugins[0].sha256",
            "must be a 64-character hex digest",
        );

        assert_eq!(
            error.to_string(),
            "invalid LiveNodeConfig.plugins[0].sha256: must be a 64-character hex digest",
        );
    }

    #[rstest]
    #[case(
        ConfigError::unsupported_field("field_a", "disabled"),
        "field_a is disabled"
    )]
    #[case(
        ConfigError::unsupported_value("field_a", "mode is disabled"),
        "field_a has unsupported value: mode is disabled"
    )]
    #[case(ConfigError::missing_field("field_a"), "field_a is required")]
    #[case(ConfigError::empty_field("field_a"), "field_a must not be empty")]
    #[case(
        ConfigError::invalid_value("field_a", "must be positive"),
        "invalid field_a: must be positive"
    )]
    #[case(
        ConfigError::invalid_format("field_a", "expected kind/name"),
        "invalid field_a: expected kind/name"
    )]
    #[case(
        ConfigError::range("field_a", "must be <= 10"),
        "invalid field_a: must be <= 10"
    )]
    #[case(
        ConfigError::mutually_exclusive_fields(["field_a", "field_b"]),
        "mutually exclusive fields: field_a, field_b"
    )]
    #[case(
        ConfigError::required_one_of(["field_a", "field_b"]),
        "one of these fields is required: field_a, field_b"
    )]
    #[case(
        ConfigError::dependency("field_a", "field_b", "field_b must be set first"),
        "field_a requires field_b: field_b must be set first"
    )]
    #[case(
        ConfigError::duplicate("field_a", Some("entry_a".to_string())),
        "duplicate field_a: entry_a"
    )]
    #[case(ConfigError::duplicate("field_a", None), "duplicate field_a")]
    #[case(
        ConfigError::feature_disabled("field_a", "live"),
        "field_a requires feature `live`"
    )]
    #[case(
        ConfigError::invalid_reference("field_a", "instrument ID", "not found"),
        "invalid field_a reference instrument ID: not found"
    )]
    fn test_config_error_display_covers_public_vocabulary(
        #[case] error: ConfigError,
        #[case] expected: &str,
    ) {
        assert_eq!(error.to_string(), expected);
    }

    #[rstest]
    fn test_check_non_empty_field_rejects_blank_values() {
        let error = check_non_empty_field("LiveNodeConfig.plugins[0].path", "  ").unwrap_err();

        assert_eq!(
            error,
            ConfigError::EmptyField {
                field: "LiveNodeConfig.plugins[0].path".to_string(),
            },
        );
    }

    #[rstest]
    #[case(
        check_supported_field("field_a", false, "unsupported"),
        ConfigError::unsupported_field("field_a", "unsupported")
    )]
    #[case(
        check_supported_value("field_a", false, "unsupported"),
        ConfigError::unsupported_value("field_a", "unsupported")
    )]
    #[case(
        check_valid_value("field_a", false, "must be positive"),
        ConfigError::invalid_value("field_a", "must be positive")
    )]
    #[case(
        check_valid_format("field_a", false, "expected kind/name"),
        ConfigError::invalid_format("field_a", "expected kind/name")
    )]
    #[case(
        check_range("field_a", false, "must be <= 10"),
        ConfigError::range("field_a", "must be <= 10")
    )]
    #[case(
        check_feature_enabled("field_a", "live", false),
        ConfigError::feature_disabled("field_a", "live")
    )]
    fn test_check_functions_return_expected_errors(
        #[case] result: ConfigResult<()>,
        #[case] expected: ConfigError,
    ) {
        assert_eq!(result.unwrap_err(), expected);
    }

    #[rstest]
    fn test_collector_returns_single_error_without_wrapping() {
        let mut collector = ConfigErrorCollector::new();
        collector.push(ConfigError::empty_field("LiveNodeConfig.plugins[0].path"));

        let error = collector.into_result().unwrap_err();

        assert_eq!(
            error,
            ConfigError::EmptyField {
                field: "LiveNodeConfig.plugins[0].path".to_string(),
            },
        );
    }

    #[rstest]
    fn test_collector_flattens_multiple_errors() {
        let mut collector = ConfigErrorCollector::new();
        collector.push(ConfigError::multiple(vec![
            ConfigError::empty_field("field_a"),
            ConfigError::empty_field("field_b"),
        ]));
        collector.push(ConfigError::empty_field("field_c"));

        let error = collector.into_result().unwrap_err();

        match error {
            ConfigError::Multiple { errors } => assert_eq!(errors.len(), 3),
            _ => panic!("Expected multiple config errors, received {error:?}"),
        }
    }
}