roan_error/diagnostic.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
use crate::{error::RoanError, span::TextSpan};
use colored::Colorize;
use log::Level;
use std::io::{BufWriter, Stderr, Write};
/// Represents a diagnostic message, which includes information about an error or warning
/// and can be pretty-printed to the console.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
/// The title or summary of the diagnostic message.
pub title: String,
/// An optional detailed description of the diagnostic message.
pub text: Option<String>,
/// The severity level of the diagnostic message (e.g., Error, Warning).
pub level: Level,
/// The location in the source code where the error or warning occurred, represented as a `TextSpan`.
pub location: Option<TextSpan>,
/// An optional hint that provides additional guidance on resolving the issue.
pub hint: Option<String>,
/// The content of the source code related to the diagnostic.
pub content: Option<String>,
}
impl Diagnostic {
/// Logs the diagnostic in a human-readable format to the provided buffer.
///
/// The message is colored according to its severity level, and the source code around
/// the error location (if available) is highlighted.
///
/// # Arguments
///
/// * `buff` - A mutable reference to a `BufWriter` that writes to `stderr`.
///
/// # Example
///
/// ```rust ignore
/// use std::io::BufWriter;
/// use log::Level;
/// use roan_error::{Diagnostic, Position, TextSpan};
/// let diagnostic = Diagnostic {
/// title: "Syntax Error".to_string(),
/// text: None,
/// level: Level::Error,
/// location: Some(TextSpan::new(Position::new(1, 1, 0), Position::new(1, 5, 4), "test".to_string())),
/// hint: None,
/// content: Some("let x = ;".to_string()),
/// };
///
/// let mut buff = BufWriter::new(std::io::stderr());
/// diagnostic.log_pretty(&mut buff);
/// ```
pub fn log_pretty(&self, buff: &mut BufWriter<Stderr>) {
writeln!(
buff,
"{}{}{}",
self.level.to_string().to_lowercase().bright_red(),
": ".dimmed(),
self.title
)
.expect("Error writing level");
if let Some(location) = &self.location {
if let Some(content) = &self.content {
let line_number = location.start.line;
let line = content
.lines()
.nth((line_number - 1) as usize)
.unwrap_or("");
let column = location.start.column;
let line_content = line.trim_end();
let decoration =
"^".repeat(location.end.column as usize - location.start.column as usize);
writeln!(buff, "{} {}:{}", "--->".cyan(), line_number, column)
.expect("Error writing line number");
if line_number > 1 {
let line_before = format!("{} |", line_number - 1);
writeln!(buff, "{}", line_before.cyan()).expect("Error writing line number");
}
let line_current = format!("{} |", line_number);
write!(buff, "{}", line_current.cyan()).expect("Error writing line number");
writeln!(buff, " {}", line_content).expect("Error writing content");
let padding_left =
" ".repeat((column + 6 + line_number.to_string().len() as u32) as usize);
writeln!(buff, "{}{}", padding_left, decoration.bright_red())
.expect("Error writing decoration");
if line_number > 1 {
let line_after = format!("{} |", line_number + 1);
writeln!(buff, "{}", line_after.cyan()).expect("Error writing line number");
}
}
}
if let Some(text) = &self.text {
writeln!(buff, "{}", text).expect("Error writing text");
}
self.print_hint(buff);
}
/// Prints a hint message (if available) to the provided buffer.
///
/// # Arguments
///
/// * `buff` - A mutable reference to a `BufWriter` that writes to `stderr`.
pub fn print_hint(&self, buff: &mut BufWriter<Stderr>) {
if let Some(hint) = &self.hint {
writeln!(buff, "{}{}", "Hint: ".bright_cyan(), hint.bright_cyan())
.expect("Error writing hint");
}
}
}
/// Prints a diagnostic message based on the provided error. The function matches
/// the error type with corresponding diagnostics and logs it prettily.
///
/// # Arguments
///
/// * `err` - An `anyhow::Error` object that encapsulates the actual error.
/// * `content` - An optional string slice containing the source code related to the error.
///
/// # Example
///
/// ```rust ignore
/// use roan_error::error::PulseError;
/// use roan_error::print_diagnostic;
/// let err = PulseError::SemanticError("Unexpected token".to_string(), span);
/// print_diagnostic(anyhow::Error::new(err), Some(source_code));
/// ```
pub fn print_diagnostic(err: anyhow::Error, content: Option<String>) {
let pulse_error = err.downcast_ref::<RoanError>();
if let Some(err) = pulse_error {
let err_str = err.to_string();
if let RoanError::Throw(content, frames) = err {
let mut buff = BufWriter::new(std::io::stderr());
write!(buff, "{}{}", "error".bright_red(), ": ".dimmed()).expect("Error writing level");
writeln!(buff, "{}", content).expect("Error writing text");
for frame in frames {
writeln!(buff, "{:?}", frame).expect("Error writing text");
}
return;
}
let diagnostic = match err {
RoanError::Io(_) => Diagnostic {
title: "IO error".to_string(),
text: Some(err_str),
level: Level::Error,
location: None,
hint: None,
content: None,
},
RoanError::RestParameterNotLast(span)
| RoanError::RestParameterNotLastPosition(span)
| RoanError::MultipleRestParameters(span)
| RoanError::SelfParameterCannotBeRest(span)
| RoanError::SelfParameterNotFirst(span)
| RoanError::MultipleSelfParameters(span)
| RoanError::StaticContext(span)
| RoanError::StaticMemberAccess(span)
| RoanError::StaticMemberAssignment(span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
RoanError::InvalidToken(_, span)
| RoanError::SemanticError(_, span)
| RoanError::UnexpectedToken(_, span)
| RoanError::InvalidEscapeSequence(_, span)
| RoanError::NonBooleanCondition(_, span)
| RoanError::StructNotFoundError(_, span)
| RoanError::TraitNotFoundError(_, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
RoanError::TraitMethodNotImplemented(name, methods, span) => Diagnostic {
title: format!(
"Trait {name} doesn't implement these methods: {}",
methods.join(", ")
),
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some("Method not implemented".to_string()),
content,
},
RoanError::StructAlreadyImplementsTrait(_, _, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some("Struct already implements this trait".to_string()),
content,
},
RoanError::ExpectedToken(expected, hint, span) => Diagnostic {
title: format!("Expected {}", expected),
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some(hint.clone()),
content,
},
RoanError::FailedToImportModule(_, _, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
RoanError::InvalidType(_, _, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
RoanError::ResolverError(_) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: None,
hint: None,
content: None,
},
RoanError::ModuleError(_) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: None,
hint: None,
content: None,
},
| RoanError::UndefinedFunctionError(_, span)
| RoanError::VariableNotFoundError(_, span)
| RoanError::ImportError(_, span)
| RoanError::PropertyNotFoundError(_, span)
| RoanError::TypeMismatch(_, span)
| RoanError::InvalidAssignment(_, span)
| RoanError::MissingParameter(_, span)
| RoanError::InvalidUnaryOperation(_, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
RoanError::InvalidBreakOrContinue(span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some(
"Break and continue statements can only be used inside loops".to_string(),
),
content,
},
RoanError::LoopBreak(span) | RoanError::LoopContinue(span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some(
"Break and continue statements can only be used inside loops".to_string(),
),
content,
},
RoanError::InvalidSpread(span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some(
"Spread operator can only be used in function calls or vectors".to_string(),
),
content,
},
RoanError::InvalidPropertyAccess(span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: Some("Only string literals or call expressions are allowed".to_string()),
content,
},
RoanError::IndexOutOfBounds(_, _, span) => Diagnostic {
title: err_str,
text: None,
level: Level::Error,
location: Some(span.clone()),
hint: None,
content,
},
_ => {
log::error!("{:?}", err);
return;
}
};
let mut buff = BufWriter::new(std::io::stderr());
diagnostic.log_pretty(&mut buff);
} else {
log::error!("{:?}", err);
}
}