#! Contains reading the members of the JSON's objects, each of the kind glTF #! says, with its default where it's left out or null, and an index checked to #! name one of the parts there are; what's wrong said with where it is, as #! `meshes[2].primitives[0]`. import std::json; module malunal::gltf; # What's wrong with a value of the JSON, and where it is. def wrong(site: strview, why: strview): gltf_error { return failed(failure::malformed, $"{site}: {why}.".view()); } # A member of an object, where it has it, and it isn't null. def member_of(v: &json::value, key: strview): maybe { if v.get(key) is some(found) { return found if !found.is_null; } return nothing{}; } # A member that must be an object, where there is one. def object_member(v: &json::value, key: strview, site: strview): result, gltf_error> { var none: maybe = nothing{}; val found := member_of(v, key) else { return none; }; return wrong(site, $"its {key} isn't an object".view()) if found.kind != json::kind::object; val held: maybe = found; return held; } # A member that must be an array, where there is one. def array_member(v: &json::value, key: strview, site: strview): result, gltf_error> { var none: maybe = nothing{}; val found := member_of(v, key) else { return none; }; return wrong(site, $"its {key} isn't an array".view()) if found.kind != json::kind::array; val held: maybe = found; return held; } # How many elements an array member holds, or none where it's missing. def length_of(v: &json::value, key: strview): u64 { if member_of(v, key) is some(found) { return found.length if found.kind == json::kind::array; } return 0; } # A number, or its default. def number_member(v: &json::value, key: strview, site: strview, given: f64): result { val found := member_of(v, key) else { return given; }; val n := found.as_f64() else (_) { return wrong(site, $"its {key} isn't a number".view()); }; return n; } # A number as an `f32`, or its default. def float_member(v: &json::value, key: strview, site: strview, given: f32): result { val n := number_member(v, key, site, given as f64) else (e) { return e; }; return n as f32; } # A number that may be left out. def maybe_number(v: &json::value, key: strview, site: strview): result, gltf_error> { var none: maybe = nothing{}; val found := member_of(v, key) else { return none; }; val n := found.as_f64() else (_) { return wrong(site, $"its {key} isn't a number".view()); }; val held: maybe = n as f32; return held; } # A whole number of 0 or more, or its default. def count_member(v: &json::value, key: strview, site: strview, given: u64): result { val found := member_of(v, key) else { return given; }; val n := found.as_u64() else (_) { return wrong(site, $"its {key} isn't a whole number of 0 or more".view()); }; return n; } # A whole number that must be there. def needed_count(v: &json::value, key: strview, site: strview): result { return wrong(site, $"it has no {key}".view()) if member_of(v, key) is none; return count_member(v, key, site, 0); } # An index of one of `count` parts of a list, as `meshes`, where it's there. def index_member(v: &json::value, key: strview, site: strview, count: u64, list: strview): result, gltf_error> { var none: maybe = nothing{}; return none if member_of(v, key) is none; val n := count_member(v, key, site, 0) else (e) { return e; }; return wrong(site, $"its {key} is {n}, of the {count} {list} there are".view()) if n >= count; val held: maybe = n; return held; } # An index that must be there. def needed_index(v: &json::value, key: strview, site: strview, count: u64, list: strview): result { val found := index_member(v, key, site, count, list) else (e) { return e; }; val n := found else { return wrong(site, $"it has no {key}".view()); }; return n; } # Text, or empty. def text_member(v: &json::value, key: strview, site: strview): result { val found := member_of(v, key) else { return string::new(nothing{}); }; val text := found.as_text() else (_) { return wrong(site, $"its {key} isn't text".view()); }; return string::from(text, nothing{}); } # `true` or `false`, or its default. def bool_member(v: &json::value, key: strview, site: strview, given: bool): result { val found := member_of(v, key) else { return given; }; val truth := found.as_bool() else (_) { return wrong(site, $"its {key} isn't true or false".view()); }; return truth; } # An array of numbers: exactly `wanted` of them where that isn't 0, or none # where it's left out. def floats_member(v: &json::value, key: strview, site: strview, wanted: u64): result, gltf_error> { var made := vector::::new(); val found := array_member(v, key, site) else (e) { return e; }; val list := found else { return made; }; return wrong(site, $"its {key} holds {list.length} numbers, not {wanted}".view()) if wanted != 0 && list.length != wanted; for i in 0..list.length { val each := list.at(i) else { break; }; val n := each.as_f64() else (_) { return wrong(site, $"its {key} holds what isn't a number".view()); }; made.append(n as f32); } return made; } # An array of numbers kept as `f64`, as an accessor's least and most. def reals_member(v: &json::value, key: strview, site: strview): result, gltf_error> { var made := vector::::new(); val found := array_member(v, key, site) else (e) { return e; }; val list := found else { return made; }; for i in 0..list.length { val each := list.at(i) else { break; }; val n := each.as_f64() else (_) { return wrong(site, $"its {key} holds what isn't a number".view()); }; made.append(n); } return made; } # An array of indices of one of `count` parts of a list. def indices_member(v: &json::value, key: strview, site: strview, count: u64, list: strview): result, gltf_error> { var made := vector::::new(); val found := array_member(v, key, site) else (e) { return e; }; val items := found else { return made; }; for i in 0..items.length { val each := items.at(i) else { break; }; val n := each.as_u64() else (_) { return wrong(site, $"its {key} holds what isn't an index".view()); }; return wrong(site, $"its {key} names {n}, of the {count} {list} there are".view()) if n >= count; made.append(n); } return made; } # An array's elements, each an object, as the parts of a list are. def object_at(list: &json::value, i: u64, site: strview): result { val each := list.at(i) else { return wrong(site, "it's missing"); }; return wrong(site, "it isn't an object") if each.kind != json::kind::object; return each; }