use serde::{Deserialize, Serialize};
use super::detail::{error_detail, warning_detail};
use super::types::{ValidationError, ValidationResult, ValidationWarning};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, export_to = "bindings/"))]
pub struct ValidationWarningWithMeta {
#[serde(flatten)]
pub warning: ValidationWarning,
pub description: String,
pub detail: String,
#[cfg_attr(feature = "typescript", ts(optional))]
pub primary_path: Option<String>,
pub can_auto_fix: bool,
pub is_viewable: bool,
pub supports_parent_picker: bool,
pub inherits_to_parent: bool,
}
impl From<ValidationWarning> for ValidationWarningWithMeta {
fn from(warning: ValidationWarning) -> Self {
Self {
description: warning.description().to_string(),
detail: warning_detail(&warning),
primary_path: warning
.file_path()
.map(|p| p.to_string_lossy().into_owned()),
can_auto_fix: warning.can_auto_fix(),
is_viewable: warning.is_viewable(),
supports_parent_picker: warning.supports_parent_picker(),
inherits_to_parent: warning.inherits_to_parent(),
warning,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, export_to = "bindings/"))]
pub struct ValidationErrorWithMeta {
#[serde(flatten)]
pub error: ValidationError,
pub description: String,
pub detail: String,
pub primary_path: String,
}
impl From<ValidationError> for ValidationErrorWithMeta {
fn from(error: ValidationError) -> Self {
Self {
description: error.description().to_string(),
detail: error_detail(&error),
primary_path: error.file_path().to_string_lossy().into_owned(),
error,
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, export_to = "bindings/"))]
pub struct ValidationResultWithMeta {
pub errors: Vec<ValidationErrorWithMeta>,
pub warnings: Vec<ValidationWarningWithMeta>,
pub files_checked: usize,
}
impl From<ValidationResult> for ValidationResultWithMeta {
fn from(result: ValidationResult) -> Self {
result.with_metadata()
}
}