use super::{AuthoredOutputContext, FragmentIdentity, OutputOrigin};
use crate::diagnostics::{
Diagnostic, DiagnosticCode, DiagnosticEntity, DiagnosticSource, Severity,
};
use crate::ir::SourceSpan;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DiagnosticAttribution {
pub source: Option<DiagnosticSource>,
pub cell: Option<usize>,
pub slot: Option<usize>,
pub fragment: Option<FragmentIdentity>,
pub span: Option<SourceSpan>,
pub related_spans: Vec<SourceSpan>,
}
impl DiagnosticAttribution {
pub fn output(
context: &AuthoredOutputContext,
origin: &OutputOrigin,
span: Option<SourceSpan>,
) -> Self {
Self {
source: origin
.fragment
.is_none()
.then(|| context.diagnostic_source()),
cell: Some(origin.cell),
slot: Some(origin.slot),
fragment: origin.fragment,
span: if origin.fragment.is_some() {
span
} else {
Some(origin.cell_span)
},
related_spans: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub enum HtmlRejectionReason {
Element,
Attribute,
Url,
RemoteImage,
Structure,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub enum MarkdownRejectionReason {
Url,
RemoteImage,
Structure,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub enum ExecutionDiagnostic {
KernelMessageIgnored {
attribution: DiagnosticAttribution,
},
UnknownDisplayUpdate {
attribution: DiagnosticAttribution,
},
NoSupportedRepresentation {
attribution: DiagnosticAttribution,
mime_types: Vec<String>,
},
UnsupportedMedia {
attribution: DiagnosticAttribution,
media_type: String,
},
InvalidMimeBundle {
attribution: DiagnosticAttribution,
},
InvalidTextPayload {
attribution: DiagnosticAttribution,
media_type: String,
},
InvalidImage {
attribution: DiagnosticAttribution,
media_type: String,
},
SvgRejected {
attribution: DiagnosticAttribution,
},
HtmlRejected {
attribution: DiagnosticAttribution,
reason: HtmlRejectionReason,
},
MarkdownRejected {
attribution: DiagnosticAttribution,
reason: MarkdownRejectionReason,
},
FragmentUnsupported {
attribution: DiagnosticAttribution,
source_kind: String,
},
}
impl ExecutionDiagnostic {
pub fn code(&self) -> DiagnosticCode {
match self {
Self::KernelMessageIgnored { .. } => DiagnosticCode::UnsupportedKernelMessage,
Self::UnknownDisplayUpdate { .. } => DiagnosticCode::UnsupportedCellOutput,
Self::NoSupportedRepresentation { .. } => DiagnosticCode::UnsupportedCellOutput,
Self::UnsupportedMedia { .. } => DiagnosticCode::UnsupportedCellOutput,
Self::InvalidMimeBundle { .. } => DiagnosticCode::InvalidCellOutput,
Self::InvalidTextPayload { .. } => DiagnosticCode::InvalidCellOutput,
Self::InvalidImage { .. } => DiagnosticCode::InvalidCellOutput,
Self::SvgRejected { .. } => DiagnosticCode::UnsafeKernelSvg,
Self::HtmlRejected { .. } => DiagnosticCode::UnsafeKernelHtml,
Self::MarkdownRejected { .. } => DiagnosticCode::InvalidCellOutput,
Self::FragmentUnsupported { .. } => DiagnosticCode::UnsupportedAuthoredSyntax,
}
}
pub fn attribution(&self) -> &DiagnosticAttribution {
match self {
Self::KernelMessageIgnored { attribution, .. } => attribution,
Self::UnknownDisplayUpdate { attribution, .. } => attribution,
Self::NoSupportedRepresentation { attribution, .. } => attribution,
Self::UnsupportedMedia { attribution, .. } => attribution,
Self::InvalidMimeBundle { attribution, .. } => attribution,
Self::InvalidTextPayload { attribution, .. } => attribution,
Self::InvalidImage { attribution, .. } => attribution,
Self::SvgRejected { attribution, .. } => attribution,
Self::HtmlRejected { attribution, .. } => attribution,
Self::MarkdownRejected { attribution, .. } => attribution,
Self::FragmentUnsupported { attribution, .. } => attribution,
}
}
pub fn to_diagnostic(&self, collection: &str) -> Diagnostic {
let message = match self {
Self::KernelMessageIgnored { .. } => "The kernel sent an unsupported message.",
Self::UnknownDisplayUpdate { .. } => "The display update has no surviving target.",
Self::NoSupportedRepresentation { .. } => {
"No supported output representation was accepted."
}
Self::UnsupportedMedia { .. } => "The output media type is unsupported.",
Self::InvalidMimeBundle { .. } => "The output MIME bundle is malformed.",
Self::InvalidTextPayload { .. } => "The text output payload is invalid.",
Self::InvalidImage { .. } => "The output image is invalid.",
Self::SvgRejected { .. } => "The SVG output violates the inert image policy.",
Self::HtmlRejected { .. } => "The HTML output violates the inert HTML policy.",
Self::MarkdownRejected { .. } => {
"The Markdown output violates the generated-content policy."
}
Self::FragmentUnsupported { .. } => {
"Unsupported generated Markdown syntax is retained as escaped text."
}
};
let attribution = self.attribution();
let mut diagnostic = Diagnostic::new(self.code(), Severity::Warning, message).with_entity(
DiagnosticEntity::Content {
id: collection.into(),
},
);
diagnostic.source = attribution.source.clone();
diagnostic.span = attribution.span;
diagnostic.related_spans = attribution.related_spans.clone();
diagnostic
}
}