-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
516 lines (476 loc) · 15 KB
/
build.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
use std::string::ToString;
use const_gen::*;
use proc_macro2::Span;
use quote::quote;
use syn::parse_quote;
#[derive(CompileConst)]
#[allow(dead_code)]
enum FactorType {
Scale {
name: Option<&'static str>,
tag: &'static str,
},
Taxonomy {
name: Option<&'static str>,
tag: &'static str,
},
}
impl CompileConst for &'static FactorType {
fn const_type() -> String {
"&'static FactorType".to_string()
}
fn const_val(&self) -> String {
format!("&{}", self.to_owned().const_val())
}
}
#[derive(CompileConst)]
#[allow(dead_code)]
enum Factor {
SingleValue {
name: &'static str,
types: &'static [&'static FactorType],
tag: &'static str,
},
MultipleValue {
name: &'static str,
types: &'static [&'static FactorType],
tag: &'static str,
},
}
#[derive(CompileConst)]
#[allow(dead_code)]
struct MoodData {
pub name: String,
pub level: u8,
pub label: String,
pub label_short: String,
}
#[derive(Clone, Debug)]
struct MoodDataRaw(
Cow<'static, str>,
u8,
Cow<'static, str>,
Cow<'static, str>,
Cow<'static, str>,
);
impl From<MoodDataRaw> for MoodData {
fn from(
MoodDataRaw(name, numeric_mood, mood_label, mood_label_short, ..): MoodDataRaw,
) -> Self {
Self {
name: name.into(),
level: numeric_mood,
label: mood_label.into(),
label_short: mood_label_short.into(),
}
}
}
static BASE_MOODS: [MoodDataRaw; 6] = [
MoodDataRaw(
Cow::Borrowed("Really good"),
60,
Cow::Borrowed("ReallyGood"),
Cow::Borrowed("sehr gute Stimmung"),
Cow::Borrowed("sehr gut"),
),
MoodDataRaw(
Cow::Borrowed("Good"),
50,
Cow::Borrowed("Good"),
Cow::Borrowed("gute Stimmung"),
Cow::Borrowed("gut"),
),
MoodDataRaw(
Cow::Borrowed("Medium"),
40,
Cow::Borrowed("Medium"),
Cow::Borrowed("mittelmässige Stimmung"),
Cow::Borrowed("mittelmässig/ok"),
),
MoodDataRaw(
Cow::Borrowed("Not so good"),
30,
Cow::Borrowed("NotSoGood"),
Cow::Borrowed("weniger gute Stimmung"),
Cow::Borrowed("weniger gut"),
),
MoodDataRaw(
Cow::Borrowed("Bad"),
20,
Cow::Borrowed("Bad"),
Cow::Borrowed("schlechte Stimmung"),
Cow::Borrowed("schlecht"),
),
MoodDataRaw(
Cow::Borrowed("Awful"),
10,
Cow::Borrowed("Awful"),
Cow::Borrowed("sehr schlechte Stimmung/unerträglich"),
Cow::Borrowed("sehr schlecht/unerträglich"),
),
];
static ACTIVITIES_MAP: [(&str, &str); 41] = [
("[143] journaling", "[143] Tagebuch schreiben"),
(
"[156] spend time w/parents",
"[156] Mit seinen Eltern zusammen sein",
),
(
"[189] mindful meditation",
"[189] Meditation oder Yoga betreiben/Achtsamkeit",
),
("[209] listening to music", "[209] Musik hören"),
("[253] take a walk", "[253] Einen Spaziergang machen"),
("[46] shower", "[46] Eine Dusche nehmen"),
(
"[55] think abt. self/probl",
"[55] Über sich selbst oder seine Probleme nachdenken",
),
("[69] taking a nap", "[69] Ein Nickerchen machen"),
(
"[264] think interesting st",
"[264] Über eine interessante Frage nachdenken",
),
(
"[43] Eating out w/ smbd",
"[43] Mit Freunden oder Bekannten zusammen essen",
),
("[118] being outside", "[118] Sich im Freien aufhalten"),
(
"[70] spend time w/ friends",
"[70] Mit Freunden zusammen sein",
),
(
"[94] sitting in the sun",
"[94] In der Sonne sitzen/aufhalten",
),
(
"[226] cooking a new meal",
"[226] Ein neues oder spezielles Gericht zubereiten",
),
("[12] crafts/be creative", "[12] Kreatives arbeiten/basteln"),
("[228] shopping", "[228] Shoppig/Einkaufen"),
("[128] helping someone", "[128] Jemandem helfen"),
("[135] eat well", "[135] Gut essen/Essen gehen"),
(
"[191] talk to work colleag",
"[191] Mit Arbeitskollegen (oder Klassenkamerden) sprechen",
),
("[158] phone call", "[158] Telefongespräche führen"),
("[167] cooking", "[167] Essen kochen"),
("[1] be in nature", "[1] Ins Grüne fahren/in der Natur sein"),
(
"[52] explore new surround.",
"[52] Erkundungen machen/unbekannte Gegenden erforschen",
),
// other activities
("Groceries", "Einkauf erledigen"),
("conversation", "Konversation"),
("eating", "Eine Mahlzeit einnehmen"),
("lay in bed", "Im Bett liegen (wach)"),
("make/drink coffee", "Kaffee machen/trinken"),
("meeting", "An einem Meeting teilnehmen"),
("selbsthilfe gruppe", "Selbsthilfegruppe"),
("sleep (night)", "Schlafen (in der Nacht)"),
("watch video", "Videos/Medien schauen/konsumieren"),
("working", "Arbeiten"),
("working (private)", "Privates Arbeiten"),
("social media", "Auf Social Media"),
("washing clothes", "Wäsche machen"),
("chat/write w/ smbdy", "Mit jemandem chatten (online)"),
("taking SSRI", "Antidepresiva einnehmen"),
("therapy", "Therapie/Psychiater"),
("cleaning", "Wohnung putzen"),
("visit bakery", "Zur Bäckerei gehen"),
];
#[derive(Clone, Debug)]
struct TimeOfDayInterval(&'static str, f32, f32, bool);
static TIME_OF_DAY_INTERVALS: [TimeOfDayInterval; 4] = [
TimeOfDayInterval("IsMorning", 6.0, 13.0, false),
TimeOfDayInterval("IsAfternoon", 13.0, 17.0, false),
TimeOfDayInterval("IsEvening", 17.0, 23.0, false),
TimeOfDayInterval("IsNight", 23.0, 6.0, true),
];
#[derive(CompileConst)]
#[allow(dead_code)]
#[inherit_doc]
enum TimeBlock {
SameDay {
name: &'static str,
from: f32,
to: f32,
},
#[inherit_doc]
/// The time block is across two days, e.g. 23:00 - 01:00
/// the selected time range is calculated as `time` < `before` on the next day & `time` >= `after` on the same day
AcrossDays {
name: &'static str,
before: f32,
after: f32,
},
}
impl From<TimeOfDayInterval> for TimeBlock {
fn from(value: TimeOfDayInterval) -> Self {
let TimeOfDayInterval(name, from, to, is_next_day) = value;
if is_next_day {
TimeBlock::AcrossDays {
name,
before: to,
after: from,
}
} else {
TimeBlock::SameDay { name, from, to }
}
}
}
fn main() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("consts-codegen.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
const MOOD_ENUM_NAME: &str = "Mood";
const DIMINISHED_ENUM_VARIANT_POSTFIX: &str = "Minus";
let mut base_mood_2_enum = phf_codegen::OrderedMap::new();
let activities_map = ACTIVITIES_MAP.into_iter().collect::<HashMap<&str, &str>>();
let max_mood_level = BASE_MOODS
.iter()
.map(|MoodDataRaw(_, numeric_mood, ..)| *numeric_mood)
.max()
.unwrap();
for m @ MoodDataRaw(mood, numeric_mood, enum_variant, mood_label, mood_label_short) in
&BASE_MOODS
{
base_mood_2_enum.entry(
mood.to_string(),
&format!(
"{MOOD_ENUM_NAME}::{enum_variant}({})",
MoodData::from(m.clone()).const_val()
),
);
let numeric_mood_new = numeric_mood - 5;
let m2 = MoodDataRaw(
format!("{mood} [-]").into(),
numeric_mood_new,
format!("{enum_variant}{DIMINISHED_ENUM_VARIANT_POSTFIX}").into(),
format!("{mood_label} (-)").into(),
format!("{mood_label_short} (-)").into(),
);
base_mood_2_enum.entry(
format!("{mood} [-]"),
&format!(
"{MOOD_ENUM_NAME}::{enum_variant}{DIMINISHED_ENUM_VARIANT_POSTFIX}({})",
MoodData::from(m2).const_val()
),
);
}
let time_blocks = TIME_OF_DAY_INTERVALS
.iter()
.map(|time_of_day_interval| {
(
time_of_day_interval.0,
TimeBlock::from(time_of_day_interval.clone()),
)
})
.collect::<HashMap<_, _>>();
writeln!(
&mut file,
"{}",
const_definition!(#[derive(PartialEq, PartialOrd, Debug, Clone, Copy)] #[non_exhaustive] pub(crate) TimeBlock)
)
.unwrap();
writeln!(
&mut file,
"{}",
const_declaration!(pub(crate) TIME_OF_DAY_INTERVALS = time_blocks)
)
.unwrap();
writeln!(
&mut file, "{}",
const_definition!(#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy, ::enum_access::EnumAccess)] #[non_exhaustive] #[enum_access(get(tag = "pub", name = "pub"))] pub FactorType)
).unwrap();
writeln!(
&mut file,
"{}",
const_definition!(#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy, ::enum_access::EnumAccess)] #[enum_access(get(name = "pub", tag = "pub", types = "pub"))] pub Factor)
)
.unwrap();
static FACTOR_TYPE_F: FactorType = FactorType::Scale {
name: None,
tag: "f",
};
static FACTOR_TYPE_Q: FactorType = FactorType::Scale {
name: Some("Qualität"),
tag: "q",
};
static FACTOR_TYPE_C: FactorType = FactorType::Taxonomy {
name: None,
tag: "c",
};
writeln!(
&mut file,
"{}\n{}\n{}",
const_declaration!(pub(crate) FACTOR_TYPE_F = FACTOR_TYPE_F),
const_declaration!(pub(crate) FACTOR_TYPE_Q = FACTOR_TYPE_Q),
const_declaration!(pub(crate) FACTOR_TYPE_C = FACTOR_TYPE_C),
)
.unwrap();
writeln!(
&mut file,
"pub const FACTOR_TYPES: ::phf::Map<&'static str, &'static FactorType> = {};",
phf_codegen::Map::new()
.entry("f", "&FACTOR_TYPE_F")
.entry("q", "&FACTOR_TYPE_Q")
.entry("c", "&FACTOR_TYPE_C")
.build()
)
.unwrap();
writeln!(
&mut file,
"pub const FACTORS: ::phf::Map<&'static str, Factor> = {};",
phf_codegen::Map::new()
.entry(
"sl",
"e!(Factor::SingleValue {
name: "Schlaf",
types: &[&FACTOR_TYPE_F, &FACTOR_TYPE_Q],
tag: "sl"
})
.to_string(),
)
.entry(
"f",
"e!(Factor::MultipleValue {
name: "Funktionsfähigkeit",
types: &[&FACTOR_TYPE_C],
tag: "f"
})
.to_string(),
)
.entry(
"st",
"e!(Factor::SingleValue {
name: "Stress",
types: &[&FACTOR_TYPE_F],
tag: "st"
})
.to_string(),
)
.entry(
"e",
"e!(Factor::MultipleValue {
name: "Emotionen",
types: &[&FACTOR_TYPE_C],
tag: "e"
})
.to_string(),
)
.entry(
"med",
"e!(Factor::MultipleValue {
name: "Medikation",
types: &[&FACTOR_TYPE_C],
tag: "med"
})
.to_string(),
)
.entry(
"sym",
"e!(Factor::MultipleValue {
name: "Mentale Symptome",
types: &[&FACTOR_TYPE_C],
tag: "sym"
})
.to_string(),
)
.entry(
"syp",
"e!(Factor::MultipleValue {
name: "Physische Symptome",
types: &[&FACTOR_TYPE_C],
tag: "syp"
})
.to_string(),
)
.entry(
"so",
"e!(Factor::MultipleValue {
name: "Soziale Situation",
types: &[&FACTOR_TYPE_C],
tag: "so"
})
.to_string(),
)
.build()
)
.unwrap();
writeln!(&mut file, "{}", const_definition!(#[derive(Default, PartialEq, PartialOrd, Debug, Clone, Copy)] #[non_exhaustive] pub MoodData)).unwrap();
let mood_enum_variants_tokens = BASE_MOODS
.iter()
.map(|MoodDataRaw(_, _, enum_variant, ..)| {
[
enum_variant.to_string(),
format!("{enum_variant}{DIMINISHED_ENUM_VARIANT_POSTFIX}"),
]
});
let mood_enum_ident = syn::Ident::new(MOOD_ENUM_NAME, Span::call_site());
let mood_enum_kind_ident =
syn::Ident::new(&format!("{}Kind", MOOD_ENUM_NAME), Span::call_site());
let mood_enum_variants_tokens2 =
mood_enum_variants_tokens
.flatten()
.map(|enum_variant| syn::Variant {
attrs: vec![],
ident: syn::Ident::new(&enum_variant, Span::call_site()),
fields: syn::Fields::Unnamed(syn::FieldsUnnamed {
paren_token: syn::token::Paren(Span::call_site()),
unnamed: {
let mut unnamed = syn::punctuated::Punctuated::new();
unnamed.push(syn::Field {
attrs: vec![parse_quote! {#[enum_alias(mood_data)]}],
vis: syn::Visibility::Inherited,
mutability: syn::FieldMutability::None,
ident: None,
colon_token: None,
ty: parse_quote!(MoodData),
});
unnamed
},
}),
discriminant: None,
});
let mood_enum_tokens = quote! {
#[derive(PartialEq, Debug, Clone, PartialOrd, Copy, ::enum_access::EnumAccess, ::enum_kinds::EnumKind)]
#[enum_kind(#mood_enum_kind_ident)]
#[non_exhaustive]
#[enum_access(get(mood_data = "pub"))]
pub enum #mood_enum_ident {
#(#mood_enum_variants_tokens2),*
}
};
writeln!(&mut file, "{}", mood_enum_tokens).unwrap();
writeln!(
&mut file,
"pub static MOOD_2_MOOD_ENUM: ::phf::OrderedMap<&'static str, {}> = {};",
MOOD_ENUM_NAME,
base_mood_2_enum.build()
)
.unwrap();
writeln!(
&mut file,
"{}\n{}",
const_declaration!(pub MAX_MOOD_LEVEL = max_mood_level),
const_declaration!(pub MIN_MOOD_LEVEL = 0u8),
)
.unwrap();
writeln!(
&mut file,
"{}",
static_declaration!(pub ACTIVITIES_MAP = activities_map)
)
.unwrap();
println!("cargo:rerun-if-changed=build.rs");
}