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))]
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    #[error("{path} cannot be changed after Nushell has started")]
55    #[diagnostic(
56        code(nu::shell::config_option_locked_after_startup),
57        help(
58            "set {path} in your config.nu (or via --config / the relevant env var) and restart Nushell"
59        )
60    )]
61    LockedAfterStartup {
62        path: String,
63        #[label("cannot be changed at runtime")]
64        span: Span,
65    },
66    // TODO: remove this
67    #[error(transparent)]
68    #[diagnostic(transparent)]
69    ShellError(#[from] ShellError),
70}
71
72/// Warnings which don't prevent config from being loaded, but we should inform the user about
73#[derive(Clone, Debug, PartialEq, Error, Diagnostic)]
74#[diagnostic(severity(Warning))]
75pub enum ConfigWarning {
76    #[error("Incompatible options")]
77    #[diagnostic(code(nu::shell::incompatible_options), help("{help}"))]
78    IncompatibleOptions {
79        label: &'static str,
80        #[label = "{label}"]
81        span: Span,
82        help: &'static str,
83    },
84}
85
86// To keep track of reported warnings
87impl Hash for ConfigWarning {
88    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
89        match self {
90            ConfigWarning::IncompatibleOptions { label, help, .. } => {
91                label.hash(state);
92                help.hash(state);
93            }
94        }
95    }
96}