@@ -7,7 +7,7 @@ export module mcppide.settings;
77
88import std;
99import nlohmann.json;
10- import mcppide.generated. spec;
10+ import mcppide.spec;
1111
1212export namespace mcppide ::settings {
1313
@@ -19,9 +19,9 @@ struct Checked {
1919 std::string expectation; // what the setting takes
2020};
2121
22- const generated ::Setting* find (std::string_view key);
23- Json default_of (const generated ::Setting& s);
24- Checked check (const generated ::Setting& s, const Json& given);
22+ const spec ::Setting* find (std::string_view key);
23+ Json default_of (const spec ::Setting& s);
24+ Checked check (const spec ::Setting& s, const Json& given);
2525// Every setting: the values to use, the rejections, and the renames an
2626// earlier key asks for (`aliases`).
2727Json resolve (const Json& given);
@@ -33,32 +33,32 @@ std::optional<Json> preset(std::string_view id);
3333
3434namespace mcppide ::settings {
3535
36- const generated ::Setting* find (std::string_view key) {
37- for (const auto & s : generated:: kSettings ) if (s.key == key) return &s;
36+ const spec ::Setting* find (std::string_view key) {
37+ for (const auto & s : spec::settings () ) if (s.key == key) return &s;
3838 return nullptr ;
3939}
4040
41- Json default_of (const generated::Setting& s) {
42- auto v = Json::parse (s.defaultJson , nullptr , false );
43- return v.is_discarded () ? Json (nullptr ) : v;
44- }
41+ Json default_of (const spec::Setting& s) { return s.defaultValue ; }
4542
4643namespace {
47- std::string expectation_of (const generated::Setting& s) {
48- if (!s.enumJson .empty ()) {
49- auto e = Json::parse (s.enumJson , nullptr , false );
44+ std::string number_text (double d) {
45+ auto t = std::format (" {}" , d);
46+ return t;
47+ }
48+
49+ std::string expectation_of (const spec::Setting& s) {
50+ if (s.allowed .is_array ()) {
5051 std::string out;
51- for (const auto & v : e ) { if (!out.empty ()) out += " | " ; out += v.get <std::string>(); }
52+ for (const auto & v : s. allowed ) { if (!out.empty ()) out += " | " ; out += v.is_string () ? v. get <std::string>() : v. dump (); }
5253 return out;
5354 }
54- if (s.type == " number" && (!s.minimum .empty () || !s.maximum .empty ()))
55- return std::format (" {} … {}" , s.minimum .empty () ? " -∞" : std::string (s.minimum ),
56- s.maximum .empty () ? " ∞" : std::string (s.maximum ));
57- return s.type == " array" ? " array of strings" : std::string (s.type );
55+ if (s.type == " number" && (s.minimum || s.maximum ))
56+ return std::format (" {} … {}" , s.minimum ? number_text (*s.minimum ) : " -∞" , s.maximum ? number_text (*s.maximum ) : " ∞" );
57+ return s.type == " array" ? " array of strings" : s.type ;
5858}
5959} // namespace
6060
61- Checked check (const generated ::Setting& s, const Json& v) {
61+ Checked check (const spec ::Setting& s, const Json& v) {
6262 Checked out{default_of (s), std::nullopt , expectation_of (s)};
6363 auto reject = [&](std::string why) { out.reason = std::move (why); return out; };
6464 if (v.is_null ()) return out;
@@ -67,14 +67,11 @@ Checked check(const generated::Setting& s, const Json& v) {
6767 } else if (s.type == " number" ) {
6868 if (!v.is_number ()) return reject (" type" );
6969 const double d = v.get <double >();
70- if (! s.minimum . empty () && d < std::stod ( std::string ( s.minimum )) ) return reject (" minimum" );
71- if (! s.maximum . empty () && d > std::stod ( std::string ( s.maximum )) ) return reject (" maximum" );
70+ if (s.minimum && d < * s.minimum ) return reject (" minimum" );
71+ if (s.maximum && d > * s.maximum ) return reject (" maximum" );
7272 } else if (s.type == " string" ) {
7373 if (!v.is_string ()) return reject (" type" );
74- if (!s.enumJson .empty ()) {
75- auto e = Json::parse (s.enumJson , nullptr , false );
76- if (std::ranges::find (e, v) == e.end ()) return reject (" enum" );
77- }
74+ if (s.allowed .is_array () && std::ranges::find (s.allowed , v) == s.allowed .end ()) return reject (" enum" );
7875 } else if (s.type == " array" ) {
7976 if (!v.is_array ()) return reject (" type" );
8077 for (const auto & item : v) if (!item.is_string ()) return reject (" items" );
@@ -85,44 +82,42 @@ Checked check(const generated::Setting& s, const Json& v) {
8582
8683Json resolve (const Json& given) {
8784 Json values = Json::object (), rejected = Json::array (), renames = Json::array (), deprecated = Json::array ();
88- for (const auto & s : generated:: kSettings ) {
89- Json v = given.is_object () && given.contains (std::string ( s.key )) ? given[std::string ( s.key ) ] : Json (nullptr );
85+ for (const auto & s : spec::settings () ) {
86+ Json v = given.is_object () && given.contains (s.key ) ? given[s.key ] : Json (nullptr );
9087 if (v.is_null ()) {
9188 // An earlier spelling of this setting still in the user's file.
92- auto aliases = Json::parse (s.aliasesJson , nullptr , false );
93- if (!aliases.is_discarded ())
94- for (const auto & a : aliases)
95- if (a.is_string () && given.is_object () && given.contains (a.get <std::string>())) {
96- v = given[a.get <std::string>()];
97- renames.push_back (Json{{" from" , a}, {" to" , s.key }, {" value" , v}});
98- break ;
99- }
89+ for (const auto & a : s.aliases )
90+ if (given.is_object () && given.contains (a)) {
91+ v = given[a];
92+ renames.push_back (Json{{" from" , a}, {" to" , s.key }, {" value" , v}});
93+ break ;
94+ }
10095 }
10196 auto c = check (s, v);
10297 if (c.reason ) rejected.push_back (Json{{" key" , s.key }, {" reason" , *c.reason }, {" expectation" , c.expectation },
10398 {" given" , v}});
10499 if (s.deprecated && !v.is_null ()) deprecated.push_back (s.key );
105- values[std::string ( s.key ) ] = c.value ;
100+ values[s.key ] = c.value ;
106101 }
107102 return Json{{" values" , values}, {" rejected" , rejected}, {" renames" , renames}, {" deprecated" , deprecated}};
108103}
109104
110105Json registry () {
111106 Json entries = Json::array ();
112- for (const auto & s : generated:: kSettings ) {
107+ for (const auto & s : spec::settings () ) {
113108 Json e{{" key" , s.key }, {" type" , s.type }, {" default" , default_of (s)}, {" group" , s.group },
114109 {" scope" , s.scope }, {" applies" , s.applies }, {" tier" , s.tier }, {" deprecated" , s.deprecated },
115110 {" title" , s.title }, {" description" , s.description }, {" expectation" , expectation_of (s)}};
116- if (!s. enumJson . empty ()) e[" enum" ] = Json::parse (s. enumJson ) ;
117- if (! s.minimum . empty ()) e[" minimum" ] = std::stod ( std::string ( s.minimum )) ;
118- if (! s.maximum . empty ()) e[" maximum" ] = std::stod ( std::string ( s.maximum )) ;
111+ if (s. allowed . is_array ()) e[" enum" ] = s. allowed ;
112+ if (s.minimum ) e[" minimum" ] = * s.minimum ;
113+ if (s.maximum ) e[" maximum" ] = * s.maximum ;
119114 entries.push_back (std::move (e));
120115 }
121- return Json{{" settings" , entries}, {" presets" , Json::parse (generated:: kPresetsJson )[ " presets " ] }};
116+ return Json{{" settings" , entries}, {" presets" , spec::presets () }};
122117}
123118
124119std::optional<Json> preset (std::string_view id) {
125- for (const auto & p : Json::parse (generated:: kPresetsJson )[ " presets " ] )
120+ for (const auto & p : spec::presets () )
126121 if (p.value (" id" , " " ) == id) return p;
127122 return std::nullopt ;
128123}
0 commit comments