use std::fmt;
use crate::cop::Severity;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
pub line: usize,
pub column: usize,
pub length: usize,
}
impl Location {
pub fn new(line: usize, column: usize, length: usize) -> Self {
Self {
line,
column,
length,
}
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
#[derive(Debug, Clone)]
pub struct Offense {
pub cop_name: String,
pub message: String,
pub severity: Severity,
pub location: Location,
}
impl Offense {
pub fn new(
cop_name: impl Into<String>,
message: impl Into<String>,
severity: Severity,
location: Location,
) -> Self {
Self {
cop_name: cop_name.into(),
message: message.into(),
severity,
location,
}
}
}
impl fmt::Display for Offense {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {}: {} ({})",
self.location, self.severity, self.message, self.cop_name
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_location_display() {
let loc = Location::new(10, 5, 3);
assert_eq!(loc.to_string(), "10:5");
}
#[test]
fn test_offense_display() {
let offense = Offense::new(
"Layout/TrailingWhitespace",
"Trailing whitespace detected.",
Severity::Convention,
Location::new(1, 10, 2),
);
assert_eq!(
offense.to_string(),
"1:10: C: Trailing whitespace detected. (Layout/TrailingWhitespace)"
);
}
}