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`](crate::ParseWarning::Deprecated) if this is a deprecation
14    /// which is detectable at parse-time.
15    #[error("{dep_type} deprecated.")]
16    #[diagnostic(code(nu::shell::deprecated))]
17    Deprecated {
18        dep_type: String,
19        label: String,
20        #[label("{label}")]
21        span: Span,
22        #[help]
23        help: Option<String>,
24        report_mode: ReportMode,
25    },
26    /// Warnings reported while updating the config
27    #[error("Encountered {} warnings(s) when updating config", warnings.len())]
28    #[diagnostic(code(nu::shell::invalid_config))]
29    InvalidConfig {
30        #[related]
31        warnings: Vec<ConfigWarning>,
32    },
33}
34
35impl Reportable for ShellWarning {
36    fn report_mode(&self) -> ReportMode {
37        match self {
38            ShellWarning::Deprecated { report_mode, .. } => *report_mode,
39            ShellWarning::InvalidConfig { .. } => ReportMode::FirstUse,
40        }
41    }
42}
43
44// To keep track of reported warnings
45impl Hash for ShellWarning {
46    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
47        match self {
48            ShellWarning::Deprecated {
49                dep_type, label, ..
50            } => {
51                dep_type.hash(state);
52                label.hash(state);
53            }
54            // We always report config warnings, so no hash necessary
55            ShellWarning::InvalidConfig { .. } => (),
56        }
57    }
58}