Skip to main content

nu_protocol/errors/
config.rs

1#![allow(unused_assignments)]
2use crate::{ShellError, Span, Type};
3use miette::Diagnostic;
4use std::hash::Hash;
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), help("{help}"))]
12    TypeMismatch {
13        path: String,
14        expected: Type,
15        actual: Type,
16        #[label = "expected {expected}, but got {actual}"]
17        span: Span,
18        /// Optional guidance (empty string when no extra help).
19        help: String,
20    },
21    #[error("Invalid value for {path}")]
22    #[diagnostic(code(nu::shell::invalid_value))]
23    InvalidValue {
24        path: String,
25        valid: String,
26        actual: String,
27        #[label = "expected {valid}, but got {actual}"]
28        span: Span,
29    },
30    #[error("Unknown config option: {path}")]
31    #[diagnostic(code(nu::shell::unknown_config_option))]
32    UnknownOption {
33        path: String,
34        #[label("remove this")]
35        span: Span,
36    },
37    #[error("{path} requires a '{column}' column")]
38    #[diagnostic(code(nu::shell::missing_required_column))]
39    MissingRequiredColumn {
40        path: String,
41        column: &'static str,
42        #[label("has no '{column}' column")]
43        span: Span,
44    },
45    #[error("{path} is deprecated")]
46    #[diagnostic(
47        code(nu::shell::deprecated_config_option),
48        help("please {suggestion} instead")
49    )]
50    Deprecated {
51        path: String,
52        suggestion: &'static str,
53        #[label("deprecated")]
54        span: Span,
55    },
56    #[error("{path} cannot be changed after Nushell has started")]
57    #[diagnostic(
58        code(nu::shell::config_option_locked_after_startup),
59        help(
60            "set {path} in your config.nu (or via --config / the relevant env var) and restart Nushell"
61        )
62    )]
63    LockedAfterStartup {
64        path: String,
65        #[label("cannot be changed at runtime")]
66        span: Span,
67    },
68    // TODO: remove this
69    #[error(transparent)]
70    #[diagnostic(transparent)]
71    ShellError(#[from] ShellError),
72}
73
74/// Warnings which don't prevent config from being loaded, but we should inform the user about
75#[derive(Clone, Debug, PartialEq, Error, Diagnostic)]
76#[diagnostic(severity(Warning))]
77pub enum ConfigWarning {
78    #[error("Incompatible options")]
79    #[diagnostic(code(nu::shell::incompatible_options), help("{help}"))]
80    IncompatibleOptions {
81        label: &'static str,
82        #[label = "{label}"]
83        span: Span,
84        help: &'static str,
85    },
86    #[error("Multiple keybindings share a name")]
87    #[diagnostic(
88        code(nu::shell::shared_keybindings_name),
89        help(
90            "all of these bindings remain in the configuration; give each a unique name to silence this warning"
91        )
92    )]
93    SharedKeybindingName {
94        names: String,
95        #[label("{names}: each names more than one keybinding")]
96        span: Span,
97    },
98}
99
100// To keep track of reported warnings
101impl Hash for ConfigWarning {
102    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
103        match self {
104            ConfigWarning::IncompatibleOptions { label, help, .. } => {
105                label.hash(state);
106                help.hash(state);
107            }
108            ConfigWarning::SharedKeybindingName { names, .. } => {
109                names.hash(state);
110            }
111        }
112    }
113}