use crate::error::{ErrorCode, ErrorSeverity, FixSuggestion, SourceLocation};
use std::collections::HashSet;
#[derive(Debug, Clone, Default)]
pub struct WarningConfig {
pub disabled: HashSet<ErrorCode>,
pub errors: HashSet<ErrorCode>,
pub all: bool,
pub pedantic: bool,
}
impl WarningConfig {
pub fn all() -> Self {
Self {
all: true,
..Default::default()
}
}
pub fn disable(mut self, code: ErrorCode) -> Self {
self.disabled.insert(code);
self
}
pub fn as_error(mut self, code: ErrorCode) -> Self {
self.errors.insert(code);
self
}
pub fn is_enabled(&self, code: ErrorCode) -> bool {
!self.disabled.contains(&code)
}
pub fn severity(&self, code: ErrorCode) -> ErrorSeverity {
if self.errors.contains(&code) {
ErrorSeverity::Error
} else {
ErrorSeverity::Warning
}
}
}
#[derive(Debug, Clone)]
pub struct Warning {
pub code: ErrorCode,
pub message: String,
pub location: SourceLocation,
pub suggestion: Option<FixSuggestion>,
}
impl Warning {
pub fn new(code: ErrorCode, message: impl Into<String>, location: SourceLocation) -> Self {
Self {
code,
message: message.into(),
location,
suggestion: None,
}
}
pub fn with_suggestion(mut self, suggestion: FixSuggestion) -> Self {
self.suggestion = Some(suggestion);
self
}
}
#[derive(Debug, Default)]
pub struct WarningCollector {
warnings: Vec<Warning>,
config: WarningConfig,
}
impl WarningCollector {
pub fn new(config: WarningConfig) -> Self {
Self {
warnings: Vec::new(),
config,
}
}
pub fn add(&mut self, warning: Warning) {
if self.config.is_enabled(warning.code) {
self.warnings.push(warning);
}
}
pub fn warnings(&self) -> &[Warning] {
&self.warnings
}
pub fn has_errors(&self) -> bool {
self.warnings
.iter()
.any(|w| self.config.errors.contains(&w.code))
}
pub fn count(&self) -> usize {
self.warnings.len()
}
pub fn clear(&mut self) {
self.warnings.clear();
}
}