use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DiagnosticKind {
Warning,
Error,
Collision,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CollisionResource {
Extension,
Skill,
Prompt,
Theme,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceCollision {
pub resource_type: CollisionResource,
pub name: String,
pub winner_path: PathBuf,
pub loser_path: PathBuf,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResourceDiagnostic {
#[serde(rename = "type")]
pub kind: DiagnosticKind,
pub message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<PathBuf>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub collision: Option<ResourceCollision>,
}
impl ResourceDiagnostic {
#[must_use]
pub fn warning(message: impl Into<String>, path: impl Into<PathBuf>) -> Self {
Self {
kind: DiagnosticKind::Warning,
message: message.into(),
path: Some(path.into()),
collision: None,
}
}
#[must_use]
pub fn error(message: impl Into<String>, path: impl Into<PathBuf>) -> Self {
Self {
kind: DiagnosticKind::Error,
message: message.into(),
path: Some(path.into()),
collision: None,
}
}
#[must_use]
pub fn collision(
message: impl Into<String>,
resource_type: CollisionResource,
name: &str,
winner: impl Into<PathBuf>,
loser: impl Into<PathBuf>,
) -> Self {
let loser = loser.into();
Self {
kind: DiagnosticKind::Collision,
message: message.into(),
path: Some(loser.clone()),
collision: Some(ResourceCollision {
resource_type,
name: name.to_owned(),
winner_path: winner.into(),
loser_path: loser,
}),
}
}
}