quarb 0.33.0

Query engine for arbors (arboreal graphs - tree-spanned or tree-dominated graphs)
Documentation
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Month and weekday names by locale, for `strptime(fmt; LOCALE)`
//! and `strftime(fmt; LOCALE)`: the `%B`, `%b`, `%A`, `%a` names
//! read and write a locale's forms. The tables are CLDR's, in
//! every form the data lists — a date's form (`октября`, the
//! genitive after a day number) and the stand-alone (`октябрь`),
//! wide and abbreviated — and all of them parse; writing uses the
//! form a date takes. Without a locale the fixed English names,
//! as before. An unknown locale has no table. The locales shipped,
//! by size of language: en, zh, es, ar, pt, ru, ja, de, fr, tr, it.

/// One locale's names. Of each entry's spellings the first is the
/// date form (what `strftime` writes where the format carries a
/// day), the second the stand-alone form (written where it does
/// not; the same word where the language makes no distinction),
/// and the rest further spellings; every spelling parses.
#[derive(Debug)]
pub struct Names {
    /// `%B`: a month's wide forms, the date form first.
    pub months: [&'static [&'static str]; 12],
    /// `%b`: the abbreviations, the date form first.
    pub months_abbr: [&'static [&'static str]; 12],
    /// `%A`: the weekdays from Monday.
    pub days: [&'static [&'static str]; 7],
    /// `%a`.
    pub days_abbr: [&'static [&'static str]; 7],
}

macro_rules! names {
    ($($entry:expr),* $(,)?) => { [$(&$entry),*] };
}

pub static EN: Names = Names {
    months: names![
        ["January"],
        ["February"],
        ["March"],
        ["April"],
        ["May"],
        ["June"],
        ["July"],
        ["August"],
        ["September"],
        ["October"],
        ["November"],
        ["December"],
    ],
    months_abbr: names![
        ["Jan"],
        ["Feb"],
        ["Mar"],
        ["Apr"],
        ["May"],
        ["Jun"],
        ["Jul"],
        ["Aug"],
        ["Sep", "Sep", "Sept"],
        ["Oct"],
        ["Nov"],
        ["Dec"],
    ],
    days: names![
        ["Monday"],
        ["Tuesday"],
        ["Wednesday"],
        ["Thursday"],
        ["Friday"],
        ["Saturday"],
        ["Sunday"],
    ],
    days_abbr: names![
        ["Mon"],
        ["Tue"],
        ["Wed"],
        ["Thu"],
        ["Fri"],
        ["Sat"],
        ["Sun"]
    ],
};

pub static RU: Names = Names {
    months: names![
        ["января", "январь"],
        ["февраля", "февраль"],
        ["марта", "март"],
        ["апреля", "апрель"],
        ["мая", "май"],
        ["июня", "июнь"],
        ["июля", "июль"],
        ["августа", "август"],
        ["сентября", "сентябрь"],
        ["октября", "октябрь"],
        ["ноября", "ноябрь"],
        ["декабря", "декабрь"],
    ],
    months_abbr: names![
        ["янв.", "янв.", "янв"],
        ["февр.", "февр.", "февр", "фев"],
        ["мар.", "март", "мар"],
        ["апр.", "апр.", "апр"],
        ["мая", "май"],
        ["июн.", "июнь", "июн"],
        ["июл.", "июль", "июл"],
        ["авг.", "авг.", "авг"],
        ["сент.", "сент.", "сент", "сен"],
        ["окт.", "окт.", "окт"],
        ["нояб.", "нояб.", "нояб", "ноя"],
        ["дек.", "дек.", "дек"],
    ],
    days: names![
        ["понедельник"],
        ["вторник"],
        ["среда"],
        ["четверг"],
        ["пятница"],
        ["суббота"],
        ["воскресенье"],
    ],
    days_abbr: names![["пн"], ["вт"], ["ср"], ["чт"], ["пт"], ["сб"], ["вс"]],
};

pub static DE: Names = Names {
    months: names![
        ["Januar"],
        ["Februar"],
        ["März"],
        ["April"],
        ["Mai"],
        ["Juni"],
        ["Juli"],
        ["August"],
        ["September"],
        ["Oktober"],
        ["November"],
        ["Dezember"],
    ],
    months_abbr: names![
        ["Jan.", "Jan.", "Jan"],
        ["Feb.", "Feb.", "Feb"],
        ["März", "März", "Mär."],
        ["Apr.", "Apr.", "Apr"],
        ["Mai"],
        ["Juni", "Juni", "Jun."],
        ["Juli", "Juli", "Jul."],
        ["Aug.", "Aug.", "Aug"],
        ["Sept.", "Sept.", "Sep."],
        ["Okt.", "Okt.", "Okt"],
        ["Nov.", "Nov.", "Nov"],
        ["Dez.", "Dez.", "Dez"],
    ],
    days: names![
        ["Montag"],
        ["Dienstag"],
        ["Mittwoch"],
        ["Donnerstag"],
        ["Freitag"],
        ["Samstag", "Sonnabend"],
        ["Sonntag"],
    ],
    days_abbr: names![
        ["Mo.", "Mo"],
        ["Di.", "Di"],
        ["Mi.", "Mi"],
        ["Do.", "Do"],
        ["Fr.", "Fr"],
        ["Sa.", "Sa"],
        ["So.", "So"]
    ],
};

pub static FR: Names = Names {
    months: names![
        ["janvier"],
        ["février"],
        ["mars"],
        ["avril"],
        ["mai"],
        ["juin"],
        ["juillet"],
        ["août"],
        ["septembre"],
        ["octobre"],
        ["novembre"],
        ["décembre"],
    ],
    months_abbr: names![
        ["janv.", "janv.", "janv"],
        ["févr.", "févr.", "févr"],
        ["mars"],
        ["avr.", "avr.", "avr"],
        ["mai"],
        ["juin"],
        ["juil.", "juil.", "juil"],
        ["août"],
        ["sept.", "sept.", "sept"],
        ["oct.", "oct.", "oct"],
        ["nov.", "nov.", "nov"],
        ["déc.", "déc.", "déc"],
    ],
    days: names![
        ["lundi"],
        ["mardi"],
        ["mercredi"],
        ["jeudi"],
        ["vendredi"],
        ["samedi"],
        ["dimanche"],
    ],
    days_abbr: names![
        ["lun.", "lun"],
        ["mar.", "mar"],
        ["mer.", "mer"],
        ["jeu.", "jeu"],
        ["ven.", "ven"],
        ["sam.", "sam"],
        ["dim.", "dim"]
    ],
};

pub static ES: Names = Names {
    months: names![
        ["enero"],
        ["febrero"],
        ["marzo"],
        ["abril"],
        ["mayo"],
        ["junio"],
        ["julio"],
        ["agosto"],
        ["septiembre", "septiembre", "setiembre"],
        ["octubre"],
        ["noviembre"],
        ["diciembre"],
    ],
    months_abbr: names![
        ["ene"],
        ["feb"],
        ["mar"],
        ["abr"],
        ["may"],
        ["jun"],
        ["jul"],
        ["ago"],
        ["sept", "sept", "sep"],
        ["oct"],
        ["nov"],
        ["dic"],
    ],
    days: names![
        ["lunes"],
        ["martes"],
        ["miércoles"],
        ["jueves"],
        ["viernes"],
        ["sábado"],
        ["domingo"],
    ],
    days_abbr: names![
        ["lun"],
        ["mar"],
        ["mié"],
        ["jue"],
        ["vie"],
        ["sáb"],
        ["dom"]
    ],
};

pub static IT: Names = Names {
    months: names![
        ["gennaio"],
        ["febbraio"],
        ["marzo"],
        ["aprile"],
        ["maggio"],
        ["giugno"],
        ["luglio"],
        ["agosto"],
        ["settembre"],
        ["ottobre"],
        ["novembre"],
        ["dicembre"],
    ],
    months_abbr: names![
        ["gen"],
        ["feb"],
        ["mar"],
        ["apr"],
        ["mag"],
        ["giu"],
        ["lug"],
        ["ago"],
        ["set"],
        ["ott"],
        ["nov"],
        ["dic"],
    ],
    days: names![
        ["lunedì"],
        ["martedì"],
        ["mercoledì"],
        ["giovedì"],
        ["venerdì"],
        ["sabato"],
        ["domenica"],
    ],
    days_abbr: names![
        ["lun"],
        ["mar"],
        ["mer"],
        ["gio"],
        ["ven"],
        ["sab"],
        ["dom"]
    ],
};

pub static PT: Names = Names {
    months: names![
        ["janeiro"],
        ["fevereiro"],
        ["março"],
        ["abril"],
        ["maio"],
        ["junho"],
        ["julho"],
        ["agosto"],
        ["setembro"],
        ["outubro"],
        ["novembro"],
        ["dezembro"],
    ],
    months_abbr: names![
        ["jan.", "jan.", "jan"],
        ["fev.", "fev.", "fev"],
        ["mar.", "mar.", "mar"],
        ["abr.", "abr.", "abr"],
        ["mai.", "mai.", "mai"],
        ["jun.", "jun.", "jun"],
        ["jul.", "jul.", "jul"],
        ["ago.", "ago.", "ago"],
        ["set.", "set.", "set"],
        ["out.", "out.", "out"],
        ["nov.", "nov.", "nov"],
        ["dez.", "dez.", "dez"],
    ],
    days: names![
        ["segunda-feira"],
        ["terça-feira"],
        ["quarta-feira"],
        ["quinta-feira"],
        ["sexta-feira"],
        ["sábado"],
        ["domingo"],
    ],
    days_abbr: names![
        ["seg.", "seg.", "seg"],
        ["ter.", "ter.", "ter"],
        ["qua.", "qua.", "qua"],
        ["qui.", "qui.", "qui"],
        ["sex.", "sex.", "sex"],
        ["sáb.", "sáb.", "sáb"],
        ["dom.", "dom.", "dom"]
    ],
};

/// Chinese: a month is its number and 月 (the numeral form wide,
/// the digit form abbreviated); weekdays 星期一 … 星期日.
pub static ZH: Names = Names {
    months: names![
        ["一月", "一月", "1月"],
        ["二月", "二月", "2月"],
        ["三月", "三月", "3月"],
        ["四月", "四月", "4月"],
        ["五月", "五月", "5月"],
        ["六月", "六月", "6月"],
        ["七月", "七月", "7月"],
        ["八月", "八月", "8月"],
        ["九月", "九月", "9月"],
        ["十月", "十月", "10月"],
        ["十一月", "十一月", "11月"],
        ["十二月", "十二月", "12月"],
    ],
    months_abbr: names![
        ["1月"],
        ["2月"],
        ["3月"],
        ["4月"],
        ["5月"],
        ["6月"],
        ["7月"],
        ["8月"],
        ["9月"],
        ["10月"],
        ["11月"],
        ["12月"],
    ],
    days: names![
        ["星期一"],
        ["星期二"],
        ["星期三"],
        ["星期四"],
        ["星期五"],
        ["星期六"],
        ["星期日", "星期天"],
    ],
    days_abbr: names![
        ["周一"],
        ["周二"],
        ["周三"],
        ["周四"],
        ["周五"],
        ["周六"],
        ["周日"]
    ],
};

/// Japanese: a month is its number and 月, one form; weekdays
/// 月曜日 … 日曜日, abbreviated to the one character.
pub static JA: Names = Names {
    months: names![
        ["1月"],
        ["2月"],
        ["3月"],
        ["4月"],
        ["5月"],
        ["6月"],
        ["7月"],
        ["8月"],
        ["9月"],
        ["10月"],
        ["11月"],
        ["12月"],
    ],
    months_abbr: names![
        ["1月"],
        ["2月"],
        ["3月"],
        ["4月"],
        ["5月"],
        ["6月"],
        ["7月"],
        ["8月"],
        ["9月"],
        ["10月"],
        ["11月"],
        ["12月"],
    ],
    days: names![
        ["月曜日"],
        ["火曜日"],
        ["水曜日"],
        ["木曜日"],
        ["金曜日"],
        ["土曜日"],
        ["日曜日"],
    ],
    days_abbr: names![["月"], ["火"], ["水"], ["木"], ["金"], ["土"], ["日"]],
};

/// Arabic: CLDR's `ar` month names (the set the Gulf and Egypt
/// write), with the Levantine names as further spellings, so
/// both parse; writing uses the default set.
pub static AR: Names = Names {
    months: names![
        ["يناير", "يناير", "كانون الثاني"],
        ["فبراير", "فبراير", "شباط"],
        ["مارس", "مارس", "آذار"],
        ["أبريل", "أبريل", "نيسان", "ابريل"],
        ["مايو", "مايو", "أيار"],
        ["يونيو", "يونيو", "حزيران"],
        ["يوليو", "يوليو", "تموز"],
        ["أغسطس", "أغسطس", "آب", "اغسطس"],
        ["سبتمبر", "سبتمبر", "أيلول"],
        ["أكتوبر", "أكتوبر", "تشرين الأول", "اكتوبر"],
        ["نوفمبر", "نوفمبر", "تشرين الثاني"],
        ["ديسمبر", "ديسمبر", "كانون الأول"],
    ],
    months_abbr: names![
        ["يناير"],
        ["فبراير"],
        ["مارس"],
        ["أبريل"],
        ["مايو"],
        ["يونيو"],
        ["يوليو"],
        ["أغسطس"],
        ["سبتمبر"],
        ["أكتوبر"],
        ["نوفمبر"],
        ["ديسمبر"],
    ],
    days: names![
        ["الاثنين"],
        ["الثلاثاء"],
        ["الأربعاء"],
        ["الخميس"],
        ["الجمعة"],
        ["السبت"],
        ["الأحد"],
    ],
    days_abbr: names![
        ["الاثنين"],
        ["الثلاثاء"],
        ["الأربعاء"],
        ["الخميس"],
        ["الجمعة"],
        ["السبت"],
        ["الأحد"]
    ],
};

/// Turkish: months one form, lowercase as CLDR has them; the
/// abbreviations capitalized as CLDR has those.
pub static TR: Names = Names {
    months: names![
        ["ocak"],
        ["şubat"],
        ["mart"],
        ["nisan"],
        ["mayıs"],
        ["haziran"],
        ["temmuz"],
        ["ağustos"],
        ["eylül"],
        ["ekim"],
        ["kasım"],
        ["aralık"],
    ],
    months_abbr: names![
        ["Oca"],
        ["Şub"],
        ["Mar"],
        ["Nis"],
        ["May"],
        ["Haz"],
        ["Tem"],
        ["Ağu"],
        ["Eyl"],
        ["Eki"],
        ["Kas"],
        ["Ara"],
    ],
    days: names![
        ["pazartesi"],
        ["salı"],
        ["çarşamba"],
        ["perşembe"],
        ["cuma"],
        ["cumartesi"],
        ["pazar"],
    ],
    days_abbr: names![
        ["Pzt"],
        ["Sal"],
        ["Çar"],
        ["Per"],
        ["Cum"],
        ["Cmt"],
        ["Paz"]
    ],
};

/// The table for a BCP 47 locale: the language decides (`ru-RU`,
/// `ru`, `ru_RU` alike). The locales shipped, by size of language:
/// en, zh, es, ar, pt, ru, ja, de, fr, tr, it.
pub fn names(locale: &str) -> Option<&'static Names> {
    let lang = locale
        .trim()
        .split(['-', '_'])
        .next()
        .unwrap_or("")
        .to_ascii_lowercase();
    Some(match lang.as_str() {
        "en" | "" => &EN,
        "zh" => &ZH,
        "es" => &ES,
        "ar" => &AR,
        "pt" => &PT,
        "ru" => &RU,
        "ja" => &JA,
        "de" => &DE,
        "fr" => &FR,
        "tr" => &TR,
        "it" => &IT,
        _ => return None,
    })
}

/// Match one of `entries`' spellings at the start of `rest`, case
/// folded, the longest spelling first (so `Sept` beats `Sep`).
/// Returns the entry's index and the matched byte length in
/// `rest`.
pub fn match_name(rest: &str, entries: &[&[&str]]) -> Option<(usize, usize)> {
    let lower = rest.to_lowercase();
    let mut best: Option<(usize, usize, usize)> = None; // (entry, chars, bytes)
    for (k, spellings) in entries.iter().enumerate() {
        for spelling in spellings.iter() {
            let s = spelling.to_lowercase();
            if lower.starts_with(&s) {
                let chars = s.chars().count();
                if best.is_none_or(|(_, c, _)| chars > c) {
                    // Case folding keeps the char count for these
                    // tables, so the match's byte length in the
                    // original is that many chars in.
                    let bytes = rest
                        .char_indices()
                        .nth(chars)
                        .map_or(rest.len(), |(i, _)| i);
                    best = Some((k, chars, bytes));
                }
            }
        }
    }
    best.map(|(k, _, bytes)| (k, bytes))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn forms_parse_and_the_longest_wins() {
        let ru = names("ru-RU").unwrap();
        assert_eq!(
            match_name("октября 1899", &ru.months),
            Some((9, "октября".len()))
        );
        assert_eq!(
            match_name("Октябрь", &ru.months),
            Some((9, "Октябрь".len()))
        );
        assert_eq!(match_name("мая", &ru.months), Some((4, "мая".len())));
        let en = names("en").unwrap();
        assert_eq!(match_name("Sept 3", &en.months_abbr), Some((8, 4)));
        assert_eq!(match_name("sep 3", &en.months_abbr), Some((8, 3)));
        assert!(names("xx").is_none());
        assert!(names("de_DE").is_some());
    }
}