Skip to main content

omena_query/
sdk_diagnostics.rs

1use crate::{
2    OmenaError, OmenaErrorClassV0, OmenaErrorContextV0, OmenaErrorRecoverabilityV0,
3    OmenaErrorSeverityV0, OmenaSdkDiagnosticsDebugReportV0, OmenaSdkDiagnosticsRequestV0,
4    OmenaSdkDiagnosticsResponseV0, OmenaSdkDiagnosticsSummaryV0, OmenaSdkResponsePartitionV0,
5    OmenaWorkspaceSnapshotIdV0, summarize_omena_query_consumer_check_style_source,
6};
7
8pub fn execute_omena_sdk_diagnostics_workflow(
9    request: OmenaSdkDiagnosticsRequestV0,
10    read_snapshot_id: OmenaWorkspaceSnapshotIdV0,
11) -> Result<OmenaSdkDiagnosticsResponseV0, OmenaError> {
12    if request.snapshot_id != read_snapshot_id {
13        return Err(OmenaError::new(
14            OmenaErrorClassV0::Workspace,
15            "diagnostics request does not match the current workspace snapshot",
16            OmenaErrorContextV0 {
17                code: "workspace.snapshot-mismatch".to_string(),
18                severity: OmenaErrorSeverityV0::Error,
19                recoverability: OmenaErrorRecoverabilityV0::Retry,
20                evidence: Vec::new(),
21            },
22        ));
23    }
24
25    let summary = summarize_omena_query_consumer_check_style_source(
26        &request.style_path,
27        &request.style_source,
28    );
29    Ok(OmenaSdkDiagnosticsResponseV0 {
30        snapshot_id: read_snapshot_id,
31        partition: OmenaSdkResponsePartitionV0::Public,
32        summary: OmenaSdkDiagnosticsSummaryV0 {
33            schema_version: summary.schema_version.to_string(),
34            product: summary.product.to_string(),
35            style_path: summary.style_path,
36            dialect: summary.dialect.to_string(),
37            token_count: count_to_u64(summary.token_count)?,
38            parser_error_count: count_to_u64(summary.parser_error_count)?,
39            class_selector_count: count_to_u64(summary.class_selector_count)?,
40            custom_property_count: count_to_u64(summary.custom_property_count)?,
41            keyframe_count: count_to_u64(summary.keyframe_count)?,
42            ready_surfaces: summary
43                .ready_surfaces
44                .into_iter()
45                .map(str::to_string)
46                .collect(),
47        },
48    })
49}
50
51pub fn execute_omena_sdk_diagnostics_debug_workflow(
52    request: OmenaSdkDiagnosticsRequestV0,
53    read_snapshot_id: OmenaWorkspaceSnapshotIdV0,
54) -> Result<OmenaSdkDiagnosticsDebugReportV0, OmenaError> {
55    let response = execute_omena_sdk_diagnostics_workflow(request, read_snapshot_id)?;
56    let analysis = serde_json::json!({
57        "parserErrorCount": response.summary.parser_error_count,
58        "readySurfaces": response.summary.ready_surfaces,
59    });
60    Ok(OmenaSdkDiagnosticsDebugReportV0 {
61        snapshot_id: response.snapshot_id,
62        partition: OmenaSdkResponsePartitionV0::Debug,
63        public_response: response,
64        analysis,
65    })
66}
67
68fn count_to_u64(value: usize) -> Result<u64, OmenaError> {
69    u64::try_from(value).map_err(|_| {
70        OmenaError::new(
71            OmenaErrorClassV0::Internal,
72            "diagnostics count exceeds the workflow contract range",
73            OmenaErrorContextV0 {
74                code: "diagnostics.count-range".to_string(),
75                severity: OmenaErrorSeverityV0::Error,
76                recoverability: OmenaErrorRecoverabilityV0::NotRecoverable,
77                evidence: Vec::new(),
78            },
79        )
80    })
81}