1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: Apache-2.0
//! Typed machine-output contracts for embeddable Heddle reports.
use schemars::JsonSchema;
use serde::Serialize;
use serde_json::Value;
/// Stable contract metadata carried beside a typed Heddle report.
#[derive(Debug, Clone, Copy)]
pub struct ReportContract {
/// Stable schema identifier for this report shape.
pub schema_name: &'static str,
/// Machine-readable stream/container kind emitted for this report.
pub machine_output_kind: MachineOutputKind,
/// Stable output discriminator emitted in the report payload, when the
/// report shape has one. Legacy discriminator-less reports keep this absent
/// so the contract does not force a JSON shape change.
pub output_discriminator: Option<OutputDiscriminator>,
/// Generate the JSON Schema for this report shape.
pub schema: fn() -> Value,
}
/// A stable field/value discriminator in a machine-readable report payload.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutputDiscriminator {
pub field: &'static str,
pub value: &'static str,
}
/// The machine-readable output container a report is serialized into.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MachineOutputKind {
/// A single JSON object.
Json,
/// Newline-delimited JSON records.
JsonLines,
/// Either a single JSON object or newline-delimited JSON records.
JsonOrJsonLines,
}
/// A typed report that exposes its stable machine-output contract.
pub trait HeddleReport: Serialize + JsonSchema {
const CONTRACT: ReportContract;
}
/// Generate a JSON Schema value for a report type.
pub fn schema_for_report<T>() -> Value
where
T: JsonSchema,
{
serde_json::to_value(schemars::schema_for!(T))
.unwrap_or_else(|_| serde_json::json!({ "type": "object" }))
}