use crate::event_hints::{engine_failure_hint, push_hint_field};
pub const SCHEMA_VERSION: u32 = 1;
pub const LEGACY_CONFIG_IGNORED_CODE: &str = "legacy-config-ignored";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EngineFailureCode {
ConfigInvalid,
CoreIncomplete,
ContentUnavailable,
PolicyIncomplete,
PatchInvalid,
PatchConflict,
FixCycle,
FixPassLimit,
TransactionFailed,
}
pub fn engine_failure_code_name(code: EngineFailureCode) -> &'static str {
match code {
EngineFailureCode::ConfigInvalid => return "config-invalid",
EngineFailureCode::CoreIncomplete => return "core-incomplete",
EngineFailureCode::ContentUnavailable => return "content-unavailable",
EngineFailureCode::PolicyIncomplete => return "policy-incomplete",
EngineFailureCode::PatchInvalid => return "patch-invalid",
EngineFailureCode::PatchConflict => return "patch-conflict",
EngineFailureCode::FixCycle => return "fix-cycle",
EngineFailureCode::FixPassLimit => return "fix-pass-limit",
EngineFailureCode::TransactionFailed => return "transaction-failed",
}
}
pub fn json_string(text: &str) -> String {
let mut result: String = String::with_capacity(text.len() + 2);
result.push('"');
for character in text.chars() {
if character == '"' {
result.push_str("\\\"");
} else if character == '\\' {
result.push_str("\\\\");
} else if character == '\n' {
result.push_str("\\n");
} else if character == '\r' {
result.push_str("\\r");
} else if character == '\t' {
result.push_str("\\t");
} else if character == '\u{8}' {
result.push_str("\\b");
} else if character == '\u{c}' {
result.push_str("\\f");
} else if character < ' ' {
result.push_str(format!("\\u{:04x}", u32::from(character)).as_str());
} else {
result.push(character);
}
}
result.push('"');
return result;
}
pub fn render_engine_failure(sequence: u64, code: EngineFailureCode, message: &str) -> String {
return render_engine_failure_with_hint(sequence, code, message, engine_failure_hint(engine_failure_code_name(code)));
}
pub fn render_engine_failure_with_hint(
sequence: u64,
code: EngineFailureCode,
message: &str,
hint: Option<&str>,
) -> String {
let name: &str = engine_failure_code_name(code);
let mut line: String = format!(
"{{\"schemaVersion\":{SCHEMA_VERSION},\"sequence\":{sequence},\"type\":\"engine-failure\",\"code\":{},\"message\":{}",
json_string(name),
json_string(message)
);
push_hint_field(&mut line, hint);
line.push_str("}\n");
return line;
}
pub fn render_configuration_warning(
sequence: u64,
code: &str,
message: &str,
path: &str,
) -> String {
let mut line: String = format!(
"{{\"schemaVersion\":{SCHEMA_VERSION},\"sequence\":{sequence},\"type\":\"configuration-warning\",\"code\":{},\"message\":{},\"path\":{}",
json_string(code),
json_string(message),
json_string(path)
);
push_hint_field(&mut line, engine_failure_hint(code));
line.push_str("}\n");
return line;
}
#[cfg(test)]
#[path = "diagnostics_tests.rs"]
mod tests;