pub(crate) mod limits;
mod parse;
mod parts;
mod surface;
mod wire;
mod write;
mod write_error;
use std::{collections::BTreeMap, io::Write};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::{Code, CodeNumber};
pub(crate) use limits::artifact_limits;
pub(crate) use parts::DiagnosticArtifactParts;
pub(crate) use surface::{DiagnosticSurfaces, HealthSurface, HttpSurface, OperationSurface};
pub(crate) use wire::CatalogDiagnosticWire;
pub use write_error::ArtifactWriteError;
pub const MAX_CATALOG_ARTIFACT_BYTES: usize = 8 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CatalogArtifact {
schema_version: u32,
catalog: CatalogIdentity,
diagnostics: Vec<CatalogDiagnostic>,
problem_sets: BTreeMap<String, Vec<Code>>,
}
impl CatalogArtifact {
pub(crate) fn new(
identity: CatalogIdentity,
diagnostics: Vec<CatalogDiagnostic>,
problem_sets: BTreeMap<String, Vec<Code>>,
) -> Self {
Self {
schema_version: 1,
catalog: identity,
diagnostics,
problem_sets,
}
}
pub const fn schema_version(&self) -> u32 {
self.schema_version
}
pub fn name(&self) -> &str {
&self.catalog.name
}
pub fn prefix(&self) -> &str {
&self.catalog.prefix
}
pub fn type_base(&self) -> &str {
&self.catalog.type_base
}
pub fn diagnostics(&self) -> &[CatalogDiagnostic] {
&self.diagnostics
}
pub const fn problem_sets(&self) -> &BTreeMap<String, Vec<Code>> {
&self.problem_sets
}
pub fn from_slice(body: &[u8]) -> Result<Self, ArtifactParseError> {
parse::parse_artifact(body)
}
pub fn write_pretty<W: Write>(&self, mut writer: W) -> Result<(), ArtifactWriteError> {
let body = write::pretty(self)?;
writer.write_all(&body).map_err(ArtifactWriteError::Write)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct CatalogIdentity {
name: String,
prefix: String,
type_base: String,
}
impl CatalogIdentity {
pub(crate) fn new(name: &str, prefix: &str, type_base: &str) -> Self {
Self {
name: name.to_owned(),
prefix: prefix.to_owned(),
type_base: type_base.to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CatalogDiagnostic {
number: CodeNumber,
code: Code,
#[serde(rename = "type")]
type_uri: String,
title: String,
detail: String,
suggestions: Vec<String>,
documentation_markdown: String,
evidence_schema: Value,
surfaces: DiagnosticSurfaces,
}
pub use parse::ArtifactParseError;
impl CatalogDiagnostic {
pub const fn number(&self) -> CodeNumber {
self.number
}
pub fn code(&self) -> &Code {
&self.code
}
pub fn type_uri(&self) -> &str {
&self.type_uri
}
pub fn title(&self) -> &str {
&self.title
}
pub fn detail(&self) -> &str {
&self.detail
}
pub fn suggestions(&self) -> &[String] {
&self.suggestions
}
pub fn documentation_markdown(&self) -> &str {
&self.documentation_markdown
}
pub const fn evidence_schema(&self) -> &Value {
&self.evidence_schema
}
pub(crate) fn evidence_schema_mut(&mut self) -> &mut Value {
&mut self.evidence_schema
}
pub const fn http_status(&self) -> Option<u16> {
self.surfaces.http_status()
}
pub fn http_policy(&self) -> Option<&str> {
self.surfaces.http_policy()
}
pub fn required_headers(&self) -> Option<&[String]> {
self.surfaces.required_headers()
}
pub const fn impact_schema(&self) -> Option<&Value> {
self.surfaces.impact_schema()
}
pub(crate) fn impact_schema_mut(&mut self) -> Option<&mut Value> {
self.surfaces.impact_schema_mut()
}
pub const fn supports_health(&self) -> bool {
self.surfaces.supports_health()
}
}