|
| 1 | +//! Problem report utils. |
| 2 | +
|
| 3 | +use catalyst_types::problem_report::ProblemReport; |
| 4 | + |
| 5 | +/// Converts problem report to JSON. |
| 6 | +pub(crate) fn problem_report_to_json(report: &ProblemReport) -> Option<String> { |
| 7 | + if !report.is_problematic() { |
| 8 | + return None; |
| 9 | + } |
| 10 | + let mut obj = serde_json::json!({ |
| 11 | + "context": report.context(), |
| 12 | + }); |
| 13 | + |
| 14 | + let entries: Vec<_> = report |
| 15 | + .entries() |
| 16 | + .map(|elem| { |
| 17 | + elem.map(|entry| { |
| 18 | + let mut obj = serde_json::json!({ |
| 19 | + "msg": entry.context(), |
| 20 | + }); |
| 21 | + |
| 22 | + let map = obj.as_object_mut().unwrap(); |
| 23 | + |
| 24 | + match entry.kind() { |
| 25 | + catalyst_types::problem_report::Kind::MissingField { field } => { |
| 26 | + map.insert("kind".to_string(), serde_json::json!("MissingField")); |
| 27 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 28 | + }, |
| 29 | + catalyst_types::problem_report::Kind::UnknownField { field, value } => { |
| 30 | + map.insert("kind".to_string(), serde_json::json!("UnknownField")); |
| 31 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 32 | + map.insert("value".to_string(), serde_json::json!(value)); |
| 33 | + }, |
| 34 | + catalyst_types::problem_report::Kind::InvalidValue { |
| 35 | + field, |
| 36 | + value, |
| 37 | + constraint, |
| 38 | + } => { |
| 39 | + map.insert("kind".to_string(), serde_json::json!("InvalidValue")); |
| 40 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 41 | + map.insert("value".to_string(), serde_json::json!(value)); |
| 42 | + map.insert("constraint".to_string(), serde_json::json!(constraint)); |
| 43 | + }, |
| 44 | + catalyst_types::problem_report::Kind::InvalidEncoding { |
| 45 | + field, |
| 46 | + encoded, |
| 47 | + expected, |
| 48 | + } => { |
| 49 | + map.insert("kind".to_string(), serde_json::json!("InvalidEncoding")); |
| 50 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 51 | + map.insert("encoded".to_string(), serde_json::json!(encoded)); |
| 52 | + map.insert("expected".to_string(), serde_json::json!(expected)); |
| 53 | + }, |
| 54 | + catalyst_types::problem_report::Kind::FunctionalValidation { explanation } => { |
| 55 | + map.insert( |
| 56 | + "kind".to_string(), |
| 57 | + serde_json::json!("FunctionalValidation"), |
| 58 | + ); |
| 59 | + map.insert("explanation".to_string(), serde_json::json!(explanation)); |
| 60 | + }, |
| 61 | + catalyst_types::problem_report::Kind::DuplicateField { field, description } => { |
| 62 | + map.insert("kind".to_string(), serde_json::json!("DuplicateField")); |
| 63 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 64 | + map.insert("description".to_string(), serde_json::json!(description)); |
| 65 | + }, |
| 66 | + catalyst_types::problem_report::Kind::ConversionError { |
| 67 | + field, |
| 68 | + value, |
| 69 | + expected_type, |
| 70 | + } => { |
| 71 | + map.insert("kind".to_string(), serde_json::json!("ConversionError")); |
| 72 | + map.insert("field".to_string(), serde_json::json!(field)); |
| 73 | + map.insert("value".to_string(), serde_json::json!(value)); |
| 74 | + map.insert( |
| 75 | + "expected_type".to_string(), |
| 76 | + serde_json::json!(expected_type), |
| 77 | + ); |
| 78 | + }, |
| 79 | + catalyst_types::problem_report::Kind::Other { description } => { |
| 80 | + map.insert("kind".to_string(), serde_json::json!("Other")); |
| 81 | + map.insert("description".to_string(), serde_json::json!(description)); |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + obj |
| 86 | + }) |
| 87 | + }) |
| 88 | + .collect(); |
| 89 | + |
| 90 | + let report_json = serde_json::json!({ |
| 91 | + "context": report.context(), |
| 92 | + "entries": entries |
| 93 | + }); |
| 94 | + |
| 95 | + serde_json::to_string(&report_json).ok() |
| 96 | +} |
0 commit comments