Initial Commit
This commit is contained in:
36 files changed
+8436
No files matched your search
@@ -0,0 +1,161 @@
|
||||
#! 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}.".span);
|
||||
}
|
||||
|
||||
# A member of an object, where it has it, and it isn't null.
|
||||
def member_of(v: &json::value, key: strview): maybe<json::value> {
|
||||
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<maybe<json::value>, gltf_error> {
|
||||
var none: maybe<json::value> = nothing{};
|
||||
val found := member_of(v, key) else { return none; };
|
||||
return wrong(site, $"its {key} isn't an object".span) if found.kind != json::kind::object;
|
||||
val held: maybe<json::value> = 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<maybe<json::value>, gltf_error> {
|
||||
var none: maybe<json::value> = nothing{};
|
||||
val found := member_of(v, key) else { return none; };
|
||||
return wrong(site, $"its {key} isn't an array".span) if found.kind != json::kind::array;
|
||||
val held: maybe<json::value> = 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<f64, gltf_error> {
|
||||
val found := member_of(v, key) else { return given; };
|
||||
val n := found.as_f64() else (_) { return wrong(site, $"its {key} isn't a number".span); };
|
||||
return n;
|
||||
}
|
||||
|
||||
# A number as an `f32`, or its default.
|
||||
def float_member(v: &json::value, key: strview, site: strview, given: f32): result<f32, gltf_error> {
|
||||
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<maybe<f32>, gltf_error> {
|
||||
var none: maybe<f32> = 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".span); };
|
||||
val held: maybe<f32> = 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<u64, gltf_error> {
|
||||
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".span); };
|
||||
return n;
|
||||
}
|
||||
|
||||
# A whole number that must be there.
|
||||
def needed_count(v: &json::value, key: strview, site: strview): result<u64, gltf_error> {
|
||||
return wrong(site, $"it has no {key}".span) 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<maybe<u64>, gltf_error> {
|
||||
var none: maybe<u64> = 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".span) if n >= count;
|
||||
val held: maybe<u64> = n;
|
||||
return held;
|
||||
}
|
||||
|
||||
# An index that must be there.
|
||||
def needed_index(v: &json::value, key: strview, site: strview, count: u64, list: strview): result<u64, gltf_error> {
|
||||
val found := index_member(v, key, site, count, list) else (e) { return e; };
|
||||
val n := found else { return wrong(site, $"it has no {key}".span); };
|
||||
return n;
|
||||
}
|
||||
|
||||
# Text, or empty.
|
||||
def text_member(v: &json::value, key: strview, site: strview): result<string, gltf_error> {
|
||||
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".span); };
|
||||
return string::from(text, nothing{});
|
||||
}
|
||||
|
||||
# `true` or `false`, or its default.
|
||||
def bool_member(v: &json::value, key: strview, site: strview, given: bool): result<bool, gltf_error> {
|
||||
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".span); };
|
||||
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<vector<f32>, gltf_error> {
|
||||
var made := vector::<f32>::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}".span) 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".span); };
|
||||
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<vector<f64>, gltf_error> {
|
||||
var made := vector::<f64>::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".span); };
|
||||
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<vector<u64>, gltf_error> {
|
||||
var made := vector::<u64>::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".span); };
|
||||
return wrong(site, $"its {key} names {n}, of the {count} {list} there are".span) 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<json::value, gltf_error> {
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user