nu_protocol/errors/
shell_warning.rs

1use crate::Span;
2use miette::Diagnostic;
3use std::hash::Hash;
4use thiserror::Error;
5
6use crate::{ConfigWarning, ReportMode, Reportable};
7
8#[derive(Clone, Debug, Error, Diagnostic)]
9#[diagnostic(severity(Warning))]
10pub enum ShellWarning {
11    /// A parse-time deprectaion. Indicates that something will be removed in a future release.
12    ///
13    /// Use [`ParseWarning::Deprecated`] if this is a deprecation which is detectable at parse-time.
14    #[error("{dep_type} deprecated.")]
15    #[diagnostic(code(nu::shell::deprecated))]
16    Deprecated {
17        dep_type: String,
18        label: String,
19        #[label("{label}")]
20        span: Span,
21        #[help]
22        help: Option<String>,
23        report_mode: ReportMode,
24    },
25    /// Warnings reported while updating the config
26    #[error("Encountered {} warnings(s) when updating config", warnings.len())]
27    #[diagnostic(code(nu::shell::invalid_config))]
28    InvalidConfig {
29        #[related]
30        warnings: Vec<ConfigWarning>,
31    },
32}
33
34impl Reportable for ShellWarning {
35    fn report_mode(&self) -> ReportMode {
36        match self {
37            ShellWarning::Deprecated { report_mode, .. } => *report_mode,
38            ShellWarning::InvalidConfig { .. } => ReportMode::FirstUse,
39        }
40    }
41}
42
43// To keep track of reported warnings
44impl Hash for ShellWarning {
45    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
46        match self {
47            ShellWarning::Deprecated {
48                dep_type, label, ..
49            } => {
50                dep_type.hash(state);
51                label.hash(state);
52            }
53            // We always report config warnings, so no hash necessary
54            ShellWarning::InvalidConfig { .. } => (),
55        }
56    }
57}