#![allow(unused_assignments)]
use crate::Span;
use miette::Diagnostic;
use std::hash::Hash;
use thiserror::Error;
use crate::{ConfigWarning, ReportMode, Reportable};
#[derive(Clone, Debug, Error, Diagnostic)]
#[diagnostic(severity(Warning))]
pub enum ShellWarning {
#[error("Running background jobs that won't be killed automatically: {job_count}.")]
#[diagnostic(
code(nu::shell::background_jobs_running),
help(
"Run `exit` again to kill all jobs.\nUse `job spawn --auto-kill` when spawning thread jobs to exclude them from this warning."
)
)]
BackgroundJobsRunning { job_count: usize },
#[error("{dep_type} deprecated.")]
#[diagnostic(code(nu::shell::deprecated))]
Deprecated {
dep_type: String,
label: String,
#[label("{label}")]
span: Span,
#[help]
help: Option<String>,
report_mode: ReportMode,
},
#[error("Encountered {} warnings(s) when updating config", warnings.len())]
#[diagnostic(code(nu::shell::invalid_config))]
InvalidConfig {
#[related]
warnings: Vec<ConfigWarning>,
},
#[error(
"Last result was truncated to fit $env.config.max_last_result_size ({limit_bytes} bytes by Value::memory_size)."
)]
#[diagnostic(code(nu::shell::last_result_truncated))]
LastResultTruncated {
span: Span,
limit_bytes: usize,
#[help]
help: Option<String>,
report_mode: ReportMode,
},
}
impl Reportable for ShellWarning {
fn report_mode(&self) -> ReportMode {
match self {
ShellWarning::Deprecated { report_mode, .. }
| ShellWarning::LastResultTruncated { report_mode, .. } => *report_mode,
ShellWarning::InvalidConfig { .. } => ReportMode::FirstUse,
ShellWarning::BackgroundJobsRunning { .. } => ReportMode::EveryUse,
}
}
}
impl Hash for ShellWarning {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
ShellWarning::BackgroundJobsRunning { job_count } => job_count.hash(state),
ShellWarning::Deprecated {
dep_type, label, ..
} => {
dep_type.hash(state);
label.hash(state);
}
ShellWarning::InvalidConfig { warnings } => warnings.hash(state),
ShellWarning::LastResultTruncated { limit_bytes, .. } => {
limit_bytes.hash(state);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ConfigWarning;
fn hash_of(warning: &ShellWarning) -> u64 {
let mut hasher = std::hash::DefaultHasher::new();
warning.hash(&mut hasher);
std::hash::Hasher::finish(&hasher)
}
fn incompatible_batch(label: &'static str) -> ShellWarning {
ShellWarning::InvalidConfig {
warnings: vec![ConfigWarning::IncompatibleOptions {
label,
span: Span::test_data(),
help: "",
}],
}
}
#[test]
fn different_config_warning_batches_hash_differently() {
assert_ne!(
hash_of(&incompatible_batch("history")),
hash_of(&incompatible_batch("other")),
);
}
#[test]
fn an_identical_config_warning_batch_hashes_the_same() {
assert_eq!(
hash_of(&incompatible_batch("history")),
hash_of(&incompatible_batch("history")),
);
}
}