./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
2 / 2
100.0%
Branches:
22 / 28
78.6%
Functions:
-
-%

include/terraingen/config/json.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Configurations as JSON: parsing, loading, writing, catalog and schema.
5 #pragma once
6
7 #include "terraingen/config/config.h"
8
9 #include <filesystem>
10 #include <stdexcept>
11 #include <string>
12 #include <string_view>
13
14 namespace terraingen {
15
16 /// A problem with a configuration, located by the JSON path of the offending value, such as
17 /// "layers[1].density.age_gyr" or "layers[0].stack[1].octaves".
18 class ConfigError : public std::runtime_error {
19 public:
20 /// An error at a path; the message is "path: message", or the message alone at the root.
21 /// @param path The JSON path of the value; empty for the document as a whole.
22 ConfigError(std::string path, const std::string& message);
23
24 /// The JSON path of the value; empty for the document as a whole.
25 393 const std::string& Path() const {
26
22/28
✓ Branch 1 taken 129 times.
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 20 taken 1 time.
✗ Branch 22 not taken.
✓ Branch 23 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 26 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 29 taken 5 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 200 times.
✓ Branch 34 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✓ Branch 46 taken 1 time.
✓ Branch 49 taken 1 time.
✓ Branch 52 taken 1 time.
✗ Branch 55 not taken.
✗ Branch 58 not taken.
393 return m_path;
27 }
28
29 private:
30 std::string m_path;
31 };
32
33 /// Reads a configuration from JSON text.
34 ///
35 /// {"schema_version": 1,
36 /// "layers": [{"type": "noise", "stack": [{"wavelength_m": 500, "amplitude_m": 50}]},
37 /// {"type": "craters",
38 /// "density": {"type": "production_surface", "age_gyr": 3.5},
39 /// "apply": {"rim_smoothing": {"sigma_radii": 0.05}}}],
40 /// "outputs": [{"type": "heightmap", "path": "terrain.tif",
41 /// "pixel_size_m": 2,
42 /// "extent_m": {"center": [0, 0], "size": [1026, 1026]}}]}
43 ///
44 /// Keys are snake_case versions of the C++ fields; bodies, sources, layers, outputs and models are
45 /// objects with a "type". Omitted keys take their defaults and omitted lists are empty.
46 /// @throws ConfigError for malformed JSON, a missing or unsupported "schema_version", an unknown
47 /// key, or a value of the wrong type.
48 TerrainGenConfig ParseConfig(std::string_view json);
49
50 /// `ParseConfig` on the contents of a file, remembering its folder in `directory`.
51 /// @throws ConfigError as `ParseConfig`, or when the file cannot be read.
52 TerrainGenConfig LoadConfig(const std::filesystem::path& path);
53
54 /// A configuration as JSON with every value written out but an unset source and albedo; parsing it
55 /// gives back an equal configuration.
56 std::string ConfigToJson(const TerrainGenConfig& config, int indent = 2);
57
58 /// Every configuration type with its defaults, as JSON: lists "body", "source", "layers",
59 /// "outputs" and "models" whose entries each parse where their kind belongs, and the "albedo".
60 std::string ConfigCatalog(int indent = 2);
61
62 /// The structure of a configuration as JSON, for editors, from the field lists the parser reads:
63 /// "root" and "definitions" of schemas, each with a "kind" (number, integer, boolean, string, enum,
64 /// optional, array, object, choice or ref) and, for fields, their description, unit, default and
65 /// bounds.
66 std::string ConfigSchema(int indent = 2);
67
68 } // namespace terraingen
69