Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit c14be47

Browse files
committed
add: 設定ファイルパーザ(暫定のすがた)
1 parent 70e0b38 commit c14be47

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

lib/xys.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import "package:yaml/yaml.dart";
2+
import "package:timezone/timezone.dart" as tz;
3+
import "package:timezone/data/latest.dart" as tz;
4+
import "package:conscript_curator/xlib.dart";
5+
6+
typedef XYSStore = List<XYSGroup>;
7+
typedef XYSTransformer<T> = T Function(XYSValue<T>);
8+
9+
class XYSGroup {
10+
final String name;
11+
final Map<String, XYSValue> value;
12+
}
13+
14+
class XYSValue<T> {
15+
final XYSType<T> type;
16+
YamlNode node;
17+
T get value => this.type.transform(this);
18+
R valueAs<R extends T>() {
19+
return this.value as R;
20+
}
21+
}
22+
23+
class XYSType<T>{
24+
final String name;
25+
final XYSTransformer<T> transformer;
26+
27+
XYSType<T>(this.name, this.transformer);
28+
29+
T transform(XYSValue<T> value) => this.transformer(value);
30+
}
31+
32+
extension XYSFinder on XYSStore {
33+
Iterable<XYSValue> findMultiple({required String group, required String key, String? type}) {
34+
Iterable<XYSValue> cand = this
35+
.where((XYSGroup ge) => ge.name == group)
36+
.map<Iterable<XYSValue>>((XYSGroup ge) => ge.value.entries
37+
.where((MapEntry<String, XYSValue> mev) => mev.key == key)
38+
.map<XYSValue>((MapEntry<String, XYSValue> mev) => mev.value))
39+
.flatten();
40+
if(type != null){
41+
cand = cand.where((XYSValue v) => v.type.name == type);
42+
}
43+
}
44+
XYSValue find({required String group, required String key, String? type}) => this.findMultiple(group: group, key: key, type: type).single;
45+
}
46+
47+
XYSTransformer<Locale> localeTrans = (XYSValue<Locale> value) {
48+
String s = value.node.asConstructor<String>("Locale", asIdentical<String>);
49+
return Locale.fromSubtags(languageCode: "", countryCode: "");
50+
}
51+
XYSTransformer<Location> tzTrans = (XYSValue<Location> value) {
52+
tz.initializeTimeZones();
53+
return tz.getLocation(value.node.asConstructor<String>("TimeZone", asIdentical<String>));
54+
}
55+
XYSTransformer<Snowflake> snowflakeTrans = (XYSValue<Snowflake> value) => Snowflake(value.node.asConstructor<int>("Snowflake", int.parse));
56+
57+
58+
extension on YamlNode {
59+
T asConstructor<T>(String name, T Function(String) parser){
60+
if(this is YamlScalar){
61+
(this as YamlScalar)
62+
}
63+
}
64+
}
65+
T asIdentical<T>(T value) => value;

0 commit comments

Comments
 (0)