nu_protocol/errors/
config.rs

1use std::hash::Hash;
2
3use crate::{ShellError, Span, Type};
4use miette::Diagnostic;
5use thiserror::Error;
6
7/// The errors that may occur when updating the config
8#[derive(Clone, Debug, PartialEq, Error, Diagnostic)]
9pub enum ConfigError {
10    #[error("Type mismatch at {path}")]
11    #[diagnostic(code(nu::shell::type_mismatch))]
12    TypeMismatch {
13        path: String,
14        expected: Type,
15        actual: Type,
16        #[label = "expected {expected}, but got {actual}"]
17        span: Span,
18    },
19    #[error("Invalid value for {path}")]
20    #[diagnostic(code(nu::shell::invalid_value))]
21    InvalidValue {
22        path: String,
23        valid: String,
24        actual: String,
25        #[label = "expected {valid}, but got {actual}"]
26        span: Span,
27    },
28    #[error("Unknown config option: {path}")]
29    #[diagnostic(code(nu::shell::unknown_config_option))]
30    UnknownOption {
31        path: String,
32        #[label("remove this")]
33        span: Span,
34    },
35    #[error("{path} requires a '{column}' column")]
36    #[diagnostic(code(nu::shell::missing_required_column))]
37    MissingRequiredColumn {
38        path: String,
39        column: &'static str,
40        #[label("has no '{column}' column")]
41        span: Span,
42    },
43    #[error("{path} is deprecated")]
44    #[diagnostic(
45        code(nu::shell::deprecated_config_option),
46        help("please {suggestion} instead")
47    )]
48    Deprecated {
49        path: String,
50        suggestion: &'static str,
51        #[label("deprecated")]
52        span: Span,
53    },
54    // TODO: remove this
55    #[error(transparent)]
56    #[diagnostic(transparent)]
57    ShellError(#[from] ShellError),
58}
59
60/// Warnings which don't prevent config from being loaded, but we should inform the user about
61#[derive(Clone, Debug, PartialEq, Error, Diagnostic)]
62#[diagnostic(severity(Warning))]
63pub enum ConfigWarning {
64    #[error("Incompatible options")]
65    #[diagnostic(code(nu::shell::incompatible_options), help("{help}"))]
66    IncompatibleOptions {
67        label: &'static str,
68        #[label = "{label}"]
69        span: Span,
70        help: &'static str,
71    },
72}
73
74// To keep track of reported warnings
75impl Hash for ConfigWarning {
76    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
77        match self {
78            ConfigWarning::IncompatibleOptions { label, help, .. } => {
79                label.hash(state);
80                help.hash(state);
81            }
82        }
83    }
84}