Arrays belong to scratch; strings borrow the immutable JSON document. * The caller keeps both alive until validation/copy/comparison is finished. */
| 186 | /* Arrays belong to scratch; strings borrow the immutable JSON document. |
| 187 | * The caller keeps both alive until validation/copy/comparison is finished. */ |
| 188 | static int surface_python_namespace_parse(CBMArena *scratch, yyjson_val *root, |
| 189 | cbm_lsp_python_namespace_t *out) { |
| 190 | memset(out, 0, sizeof(*out)); |
| 191 | uint32_t version = 0, file_language = 0; |
| 192 | yyjson_val *py = surface_unique_field(root, "py"); |
| 193 | if (!surface_py_uint(surface_unique_field(root, "v"), UINT32_MAX, &version) || |
| 194 | version != SURFACE_CODEC_VERSION || !py || |
| 195 | !surface_py_uint(surface_unique_field(root, "lang"), CBM_LANG_COUNT - 1, &file_language) || |
| 196 | !yyjson_is_arr(surface_unique_field(root, "lsp"))) { |
| 197 | return -1; |
| 198 | } |
| 199 | if (yyjson_is_null(py)) { |
| 200 | return file_language == CBM_LANG_PYTHON ? -1 : 0; |
| 201 | } |
| 202 | if (file_language != CBM_LANG_PYTHON || !yyjson_is_obj(py) || yyjson_obj_size(py) != 7) { |
| 203 | return -1; |
| 204 | } |
| 205 | CBMPyNamespaceFacts *facts = &out->facts; |
| 206 | uint32_t language, status, failure; |
| 207 | if (!surface_py_uint(surface_unique_field(py, "v"), UINT32_MAX, &facts->version) || |
| 208 | !surface_py_uint(surface_unique_field(py, "lang"), INT_MAX, &language) || |
| 209 | !surface_py_uint(surface_unique_field(py, "status"), INT_MAX, &status) || |
| 210 | !surface_py_uint(surface_unique_field(py, "failure"), INT_MAX, &failure) || |
| 211 | !surface_py_string(surface_unique_field(py, "module"), false, &out->module_qn) || |
| 212 | !surface_py_string(surface_unique_field(py, "path"), false, &out->rel_path) || |
| 213 | !out->module_qn[0] || !out->rel_path[0] || language != CBM_LANG_PYTHON || |
| 214 | status != CBM_PY_NS_COMPLETE || failure != CBM_PY_NS_FAILURE_NONE) { |
| 215 | return -1; |
| 216 | } |
| 217 | facts->language = (CBMLanguage)language; |
| 218 | facts->status = (CBMPyNamespaceStatus)status; |
| 219 | facts->failure = (CBMPyNamespaceFailure)failure; |
| 220 | yyjson_val *events = surface_unique_field(py, "events"); |
| 221 | if (!yyjson_is_arr(events)) { |
| 222 | return -1; |
| 223 | } |
| 224 | size_t count = yyjson_arr_size(events); |
| 225 | if (count > INT_MAX || count > SIZE_MAX / sizeof(*facts->items)) { |
| 226 | return -1; |
| 227 | } |
| 228 | facts->count = (int)count; |
| 229 | facts->cap = (int)count; |
| 230 | if (count) { |
| 231 | facts->items = cbm_arena_alloc(scratch, count * sizeof(*facts->items)); |
| 232 | if (!facts->items) { |
| 233 | return -1; |
| 234 | } |
| 235 | memset(facts->items, 0, count * sizeof(*facts->items)); |
| 236 | } |
| 237 | yyjson_arr_iter it = yyjson_arr_iter_with(events); |
| 238 | yyjson_val *event; |
| 239 | int i = 0; |
| 240 | while ((event = yyjson_arr_iter_next(&it))) { |
| 241 | if (!yyjson_is_obj(event) || yyjson_obj_size(event) != 10) { |
| 242 | return -1; |
| 243 | } |
| 244 | CBMPyNamespaceFact *f = &facts->items[i++]; |
| 245 | uint32_t kind, def_kind, sequence_kind, reason; |
no test coverage detected