use crate::diagnostic::StyleDiagnostic;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReportRowKind {
CacheRead,
CacheRefresh,
}
impl ReportRowKind {
#[must_use]
pub const fn is_refresh(self) -> bool {
matches!(self, Self::CacheRefresh)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReportRow {
kind: ReportRowKind,
label: String,
}
impl ReportRow {
pub fn new(kind: ReportRowKind, label: impl Into<String>) -> Result<Self, StyleDiagnostic> {
let label = label.into();
let label = label.trim();
if label.is_empty() {
return Err(StyleDiagnostic::empty_report_label());
}
Ok(Self {
kind,
label: label.to_owned(),
})
}
#[must_use]
pub const fn kind(&self) -> ReportRowKind {
self.kind
}
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
}
#[cfg(test)]
mod tests {
use crate::{
diagnostic::StyleDiagnosticCode,
report::{ReportRow, ReportRowKind},
};
#[test]
fn report_labels_are_normalized() {
let row = ReportRow::new(ReportRowKind::CacheRead, " sns-neurons ")
.expect("trimmed report labels should be valid");
assert_eq!(row.label(), "sns-neurons");
assert!(!row.kind().is_refresh());
}
#[test]
fn empty_report_labels_return_typed_diagnostic() {
let err =
ReportRow::new(ReportRowKind::CacheRead, " ").expect_err("empty labels should fail");
assert_eq!(err.code(), StyleDiagnosticCode::EmptyReportLabel);
}
}