forked from sodiboo/niri-flake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-binds.nix
155 lines (151 loc) · 4.11 KB
/
parse-binds.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{
lib,
kdl,
...
}:
with lib; let
short-circuit = v: steps:
pipe v (map (step: x:
if x == null
then null
else step x)
steps);
coalesce = flip pipe [
(remove null)
head
];
ifilter = f:
flip pipe [
(imap0 (i: v: {inherit i v;}))
(filter ({i, ...}: f i))
(map ({v, ...}: v))
];
filter-prev = f: l:
if l == []
then []
else [(head l)] ++ (ifilter (flip pipe [(elemAt l) f]) (tail l));
kebaberize = flip pipe [
(replaceStrings strings.upperChars (map (c: "-${c}") strings.lowerChars))
(removePrefix "-")
];
in
flip short-circuit [
(src: "${src}/niri-config/src/lib.rs")
(path:
if builtins.pathExists path
then path
else null)
builtins.readFile
(strings.splitString "\n")
(lists.foldl (
{
state,
actions,
}: line:
{
leading = {
state =
if line == "pub enum Action {"
then "actions"
else "leading";
inherit actions;
};
actions =
if line == "}"
then {
state = "trailing";
inherit actions;
}
else {
state = "actions";
actions = actions ++ [line];
};
trailing = {
state = "trailing";
inherit actions;
};
}
.${state}
) {
state = "leading";
actions = [];
})
({
state,
actions,
}:
assert (state == "trailing"); actions)
(remove "")
(map (removePrefix " "))
(filter-prev (prev: prev != "#[knuffel(skip)]"))
(remove "#[knuffel(skip)]")
(map (
flip short-circuit [
(strings.match ''([A-Za-z]*)(\((.*)\))?,'')
(
m: let
raw-name = elemAt m 0;
raw = elemAt m 2;
name = kebaberize raw-name;
params =
if raw == null
then {
kind = "empty";
}
else
coalesce [
(short-circuit raw [
(strings.match ''#\[knuffel\(argument(, str)?\)] ([A-Za-z0-9]+)'')
(m: {
kind = "arg";
as-str = elemAt m 0 != null;
type = elemAt m 1;
})
])
(short-circuit raw [
(strings.match ''#\[knuffel\(arguments\)] Vec<([A-Za-z0-9]+)>'')
(m: {
kind = "list";
type = elemAt m 0;
})
])
(short-circuit raw [
(strings.match ''#\[knuffel\(property\(name = "([^"]*)"\)(, default)?\)] ([A-Za-z0-9]+)'')
(m: let
field = elemAt m 0;
use-default = elemAt m 1 != null;
type = elemAt m 2;
in {
kind = "prop";
none-important = false;
inherit field use-default type;
})
])
(short-circuit raw [
(strings.match ''#\[knuffel\(property\(name = "([^"]*)"\)\)] Option<([A-Za-z0-9]+)>'')
(m: let
field = elemAt m 0;
type = elemAt m 1;
in {
kind = "prop";
# Option<T> always has a default value.
use-default = true;
# And it is actively meaningful to omit.
none-important = true;
inherit field type;
})
])
{
kind = "unknown";
inherit raw-name raw;
}
];
in {
inherit name params;
fn = kdl.magic-leaf name;
}
)
]
))
(remove null)
]