| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Typed view of the `RawCppBundle` v1 JSON emitted by `anneal-clang-dump`. | |
| //! | |
| //! Parsing is strict about the fields lowering relies on and tolerant of | |
| //! additive fields it does not use. Constructs the extractor could not | |
| //! describe are kept as `Unsupported` nodes so lowering fails closed only if | |
| //! they are actually reachable. | |
| use serde_json::{Map, Value}; | |
| use crate::error::{schema, Result}; | |
| mod bundle; | |
| mod expr; | |
| mod expr_types; | |
| mod stmt; | |
| mod types; | |
| pub use self::{ | |
| bundle::{parse_bundle, parse_type}, | |
| expr::parse_expr, | |
| expr_types::{ArithOp, BinaryOp, CastKind, RawExpr, RawExprKind, UnaryOp}, | |
| stmt::{parse_stmt, RawStmt}, | |
| types::{ | |
| MethodKind, RawAbiWitness, RawBundle, RawCtorInit, RawField, RawFunction, RawLangPolicy, | |
| RawParam, RawPassing, RawRecord, RawSubobjectDtor, RawType, | |
| }, | |
| }; | |
| // ------------------------------------------------------------------------- | |
| // JSON accessors | |
| // ------------------------------------------------------------------------- | |
| fn obj<'a>(v: &'a Value, ctx: &str) -> Result<&'a Map<String, Value>> { | |
| match v.as_object() { | |
| Some(o) => Ok(o), | |
| None => schema(format!("{ctx}: expected object")), | |
| } | |
| } | |
| fn field<'a>(o: &'a Map<String, Value>, key: &str, ctx: &str) -> Result<&'a Value> { | |
| match o.get(key) { | |
| Some(v) => Ok(v), | |
| None => schema(format!("{ctx}: missing field `{key}`")), | |
| } | |
| } | |
| fn get_str(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<String> { | |
| match field(o, key, ctx)?.as_str() { | |
| Some(s) => Ok(s.to_owned()), | |
| None => schema(format!("{ctx}: field `{key}` must be a string")), | |
| } | |
| } | |
| fn get_bool(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<bool> { | |
| match field(o, key, ctx)?.as_bool() { | |
| Some(b) => Ok(b), | |
| None => schema(format!("{ctx}: field `{key}` must be a bool")), | |
| } | |
| } | |
| fn get_bool_or(o: &Map<String, Value>, key: &str, default: bool, ctx: &str) -> Result<bool> { | |
| match o.get(key) { | |
| None => Ok(default), | |
| Some(v) => match v.as_bool() { | |
| Some(b) => Ok(b), | |
| None => schema(format!("{ctx}: field `{key}` must be a bool")), | |
| }, | |
| } | |
| } | |
| fn get_u64(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<u64> { | |
| match field(o, key, ctx)?.as_u64() { | |
| Some(n) => Ok(n), | |
| None => schema(format!("{ctx}: field `{key}` must be a non-negative integer")), | |
| } | |
| } | |
| fn get_u32(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<u32> { | |
| let n = get_u64(o, key, ctx)?; | |
| match u32::try_from(n) { | |
| Ok(n) => Ok(n), | |
| Err(_) => schema(format!("{ctx}: field `{key}` exceeds u32")), | |
| } | |
| } | |
| fn get_arr<'a>(o: &'a Map<String, Value>, key: &str, ctx: &str) -> Result<&'a Vec<Value>> { | |
| match field(o, key, ctx)?.as_array() { | |
| Some(a) => Ok(a), | |
| None => schema(format!("{ctx}: field `{key}` must be an array")), | |
| } | |
| } | |
| /// Returns `None` if the field is absent or JSON `null`. | |
| fn get_opt<'a>(o: &'a Map<String, Value>, key: &str) -> Option<&'a Value> { | |
| match o.get(key) { | |
| None | Some(Value::Null) => None, | |
| Some(v) => Some(v), | |
| } | |
| } | |
| fn parse_i128(s: &str, ctx: &str) -> Result<i128> { | |
| match s.parse::<i128>() { | |
| Ok(n) => Ok(n), | |
| Err(_) => schema(format!("{ctx}: `{s}` is not a decimal integer in i128 range")), | |
| } | |
| } | |
| fn get_opt_str(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<Option<String>> { | |
| match get_opt(o, key) { | |
| None => Ok(None), | |
| Some(v) => match v.as_str() { | |
| Some(s) => Ok(Some(s.to_owned())), | |
| None => schema(format!("{ctx}: field `{key}` must be a string or null")), | |
| }, | |
| } | |
| } | |
| fn get_bool_list(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<Vec<bool>> { | |
| match get_opt(o, key) { | |
| None => Ok(Vec::new()), | |
| Some(v) => match v.as_array() { | |
| Some(a) => { | |
| let mut out = Vec::with_capacity(a.len()); | |
| for b in a { | |
| match b.as_bool() { | |
| Some(b) => out.push(b), | |
| None => return schema(format!("{ctx}: `{key}` must hold booleans")), | |
| } | |
| } | |
| Ok(out) | |
| } | |
| None => schema(format!("{ctx}: field `{key}` must be an array")), | |
| }, | |
| } | |
| } |