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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
use std::borrow::Borrow;
use std::borrow::Cow;

use super::util::coalesce_whitespace_escaped;
use super::util::coalesce_whitespace_if_line_break;
use super::util::remove_line_break;
use super::util::remove_whitespace_if_line_break;
use super::util::to_lowercase;
use super::PostBlank;
use super::StandardProperties;

#[derive(Debug)]
pub enum Object<'s> {
    Bold(Bold<'s>),
    Italic(Italic<'s>),
    Underline(Underline<'s>),
    StrikeThrough(StrikeThrough<'s>),
    Code(Code<'s>),
    Verbatim(Verbatim<'s>),
    PlainText(PlainText<'s>),
    RegularLink(RegularLink<'s>),
    RadioLink(RadioLink<'s>),
    RadioTarget(RadioTarget<'s>),
    PlainLink(PlainLink<'s>),
    AngleLink(AngleLink<'s>),
    OrgMacro(OrgMacro<'s>),
    Entity(Entity<'s>),
    LatexFragment(LatexFragment<'s>),
    ExportSnippet(ExportSnippet<'s>),
    FootnoteReference(FootnoteReference<'s>),
    Citation(Citation<'s>),
    CitationReference(CitationReference<'s>),
    InlineBabelCall(InlineBabelCall<'s>),
    InlineSourceBlock(InlineSourceBlock<'s>),
    LineBreak(LineBreak<'s>),
    Target(Target<'s>),
    StatisticsCookie(StatisticsCookie<'s>),
    Subscript(Subscript<'s>),
    Superscript(Superscript<'s>),
    Timestamp(Timestamp<'s>),
}

#[derive(Debug)]
pub struct Bold<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct Italic<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct Underline<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct StrikeThrough<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct Code<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct Verbatim<'s> {
    pub source: &'s str,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct PlainText<'s> {
    pub source: &'s str,
}

#[derive(Debug)]
pub struct RegularLink<'s> {
    pub source: &'s str,
    pub link_type: LinkType<'s>,
    /// The path after templates have been applied.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_raw_link` for an equivalent value.
    pub path: Cow<'s, str>,

    /// The raw link after templates have been applied.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_raw_link` for an equivalent value.
    pub raw_link: Cow<'s, str>,

    /// The search_option after templates have been applied.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_search_option` for an equivalent value.
    pub search_option: Option<Cow<'s, str>>,

    pub contents: Option<&'s str>,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
    pub application: Option<Cow<'s, str>>,
}

#[derive(Debug)]
pub struct RadioTarget<'s> {
    pub source: &'s str,
    pub value: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct RadioLink<'s> {
    pub source: &'s str,
    pub path: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct PlainLink<'s> {
    pub source: &'s str,
    pub link_type: LinkType<'s>,
    pub path: &'s str,
    pub raw_link: &'s str,
    pub search_option: Option<&'s str>,
    pub application: Option<&'s str>,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct AngleLink<'s> {
    pub source: &'s str,
    pub link_type: LinkType<'s>,

    /// The path from the source.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_raw_link` for an equivalent value.
    pub path: &'s str,
    pub raw_link: &'s str,

    /// The search_option from the source.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_search_option` for an equivalent value.
    pub search_option: Option<&'s str>,
    pub application: Option<&'s str>,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct OrgMacro<'s> {
    pub source: &'s str,

    /// The key from the source.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_key` for an equivalent value.
    pub key: &'s str,

    /// The args from the source.
    ///
    /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_args` for an equivalent value.
    pub args: Vec<&'s str>,

    pub value: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct Entity<'s> {
    pub source: &'s str,
    pub name: &'s str,
    pub latex_math_mode: bool,
    pub latex: &'s str,
    pub html: &'s str,
    pub ascii: &'s str,
    // Skipping latin1 because it is detrimental to the future. If anyone out there is using latin1, take a long look in the mirror and change your ways.
    pub utf8: &'s str,
    pub use_brackets: bool,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct LatexFragment<'s> {
    pub source: &'s str,
    pub value: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct ExportSnippet<'s> {
    pub source: &'s str,
    pub backend: &'s str,
    pub contents: Option<&'s str>,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct FootnoteReference<'s> {
    pub source: &'s str,
    pub contents: Option<&'s str>,
    pub post_blank: Option<&'s str>,
    pub label: Option<&'s str>,
    pub definition: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct Citation<'s> {
    pub source: &'s str,
    pub style: Option<&'s str>,
    pub prefix: Vec<Object<'s>>,
    pub suffix: Vec<Object<'s>>,
    pub children: Vec<CitationReference<'s>>,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct CitationReference<'s> {
    pub source: &'s str,
    pub key: &'s str,
    pub prefix: Vec<Object<'s>>,
    pub suffix: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct InlineBabelCall<'s> {
    pub source: &'s str,
    pub value: &'s str,
    pub call: &'s str,
    pub inside_header: Option<&'s str>,
    pub arguments: Option<&'s str>,
    pub end_header: Option<&'s str>,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct InlineSourceBlock<'s> {
    pub source: &'s str,
    pub language: &'s str,
    pub parameters: Option<&'s str>,
    pub value: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct LineBreak<'s> {
    pub source: &'s str,
}

#[derive(Debug)]
pub struct Target<'s> {
    pub source: &'s str,
    pub value: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct StatisticsCookie<'s> {
    pub source: &'s str,
    pub value: &'s str,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug)]
pub struct Subscript<'s> {
    pub source: &'s str,
    pub use_brackets: bool,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

#[derive(Debug)]
pub struct Superscript<'s> {
    pub source: &'s str,
    pub use_brackets: bool,
    pub contents: &'s str,
    pub post_blank: Option<&'s str>,
    pub children: Vec<Object<'s>>,
}

// TODO: Perhaps there is an optimization of converting to unix time we can do to shrink this struct. (ref: clippy::large_enum_variant on Element)
#[derive(Debug, Clone)]
pub struct Timestamp<'s> {
    pub source: &'s str,
    pub timestamp_type: TimestampType,
    pub range_type: TimestampRangeType,
    pub start: Option<Date<'s>>,
    pub end: Option<Date<'s>>,
    pub start_time: Option<Time<'s>>,
    pub end_time: Option<Time<'s>>,
    pub repeater: Option<Repeater>,
    pub warning_delay: Option<WarningDelay>,
    pub post_blank: Option<&'s str>,
}

#[derive(Debug, Clone)]
pub enum TimestampType {
    Diary,
    Active,
    Inactive,
    ActiveRange,
    InactiveRange,
}

#[derive(Debug, Clone)]
pub enum TimestampRangeType {
    None,
    DateRange,
    TimeRange,
}

pub type YearInner = u16;
pub type MonthInner = u8;
pub type DayOfMonthInner = u8;
pub type HourInner = u8;
pub type MinuteInner = u8;

#[derive(Debug, Clone)]
pub struct Year(pub YearInner);

#[derive(Debug, Clone)]
pub struct Month(pub MonthInner);

#[derive(Debug, Clone)]
pub struct DayOfMonth(pub DayOfMonthInner);

#[derive(Debug, Clone)]
pub struct Hour(pub HourInner);

#[derive(Debug, Clone)]
pub struct Minute(pub MinuteInner);

impl Year {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(source: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let year = source.parse::<YearInner>()?;
        Ok(Year(year))
    }

    pub fn get_value(&self) -> YearInner {
        self.0
    }
}

impl Month {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(source: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let month = source.parse::<MonthInner>()?;
        if !(1..=12).contains(&month) {
            Err("Month exceeds possible range.")?;
        }
        Ok(Month(month))
    }

    pub fn get_value(&self) -> MonthInner {
        self.0
    }
}

impl DayOfMonth {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(source: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let day_of_month = source.parse::<DayOfMonthInner>()?;
        if !(1..=31).contains(&day_of_month) {
            Err("Day of month exceeds possible range.")?;
        }
        Ok(DayOfMonth(day_of_month))
    }

    pub fn get_value(&self) -> DayOfMonthInner {
        self.0
    }
}

impl Hour {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(source: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let hour = source.parse::<HourInner>()?;
        if hour > 23 {
            Err("Hour exceeds possible range.")?;
        }
        Ok(Hour(hour))
    }

    pub fn get_value(&self) -> HourInner {
        self.0
    }
}

impl Minute {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(source: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let minute = source.parse::<MinuteInner>()?;
        if minute > 59 {
            Err("Minute exceeds possible range.")?;
        }
        Ok(Minute(minute))
    }

    pub fn get_value(&self) -> MinuteInner {
        self.0
    }
}

#[derive(Debug, Clone)]
pub struct Date<'s> {
    year: Year,
    month: Month,
    day_of_month: DayOfMonth,
    day_name: Option<&'s str>,
}

impl<'s> Date<'s> {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(
        year: Year,
        month: Month,
        day_of_month: DayOfMonth,
        day_name: Option<&'s str>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        // TODO: Does org-mode support non-gregorian calendars?
        // TODO: Do I want to validate leap year?
        match (month.get_value(), day_of_month.get_value()) {
            (1, 1..=31) => {}
            (2, 1..=29) => {}
            (3, 1..=31) => {}
            (4, 1..=30) => {}
            (5, 1..=31) => {}
            (6, 1..=30) => {}
            (7, 1..=31) => {}
            (8, 1..=31) => {}
            (9, 1..=30) => {}
            (10, 1..=31) => {}
            (11, 1..=30) => {}
            (12, 1..=31) => {}
            _ => Err("Invalid day of month for the month.")?,
        };
        Ok(Date {
            year,
            month,
            day_of_month,
            day_name,
        })
    }

    pub fn get_year(&self) -> &Year {
        &self.year
    }

    pub fn get_month(&self) -> &Month {
        &self.month
    }

    pub fn get_day_of_month(&self) -> &DayOfMonth {
        &self.day_of_month
    }

    pub fn get_day_name(&self) -> Option<&'s str> {
        self.day_name
    }
}

#[derive(Debug, Clone)]
pub struct Time<'s> {
    hour: Hour,
    minute: Minute,
    postfix: Option<&'s str>,
}

impl<'s> Time<'s> {
    // TODO: Make a real error type instead of a boxed any error.
    pub fn new(
        hour: Hour,
        minute: Minute,
        postfix: Option<&'s str>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Time {
            hour,
            minute,
            postfix,
        })
    }

    pub fn get_hour(&self) -> &Hour {
        &self.hour
    }

    pub fn get_minute(&self) -> &Minute {
        &self.minute
    }

    pub fn get_postfix(&self) -> Option<&'s str> {
        self.postfix
    }
}

#[derive(Debug, Clone)]
pub enum RepeaterType {
    Cumulative,
    CatchUp,
    Restart,
}

#[derive(Debug, Clone)]
pub enum WarningDelayType {
    All,
    First,
}

#[derive(Debug, Clone)]
pub enum TimeUnit {
    Hour,
    Day,
    Week,
    Month,
    Year,
}

pub type RepeaterWarningDelayValueType = u16;

#[derive(Debug, Clone)]
pub struct Repeater {
    pub repeater_type: RepeaterType,
    pub value: RepeaterWarningDelayValueType,
    pub unit: TimeUnit,
}

#[derive(Debug, Clone)]
pub struct WarningDelay {
    pub warning_delay_type: WarningDelayType,
    pub value: RepeaterWarningDelayValueType,
    pub unit: TimeUnit,
}

impl<'s> StandardProperties<'s> for Bold<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Italic<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Underline<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for StrikeThrough<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Code<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Verbatim<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for RegularLink<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        self.contents
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for RadioLink<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.path)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for RadioTarget<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.value)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for PlainLink<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for AngleLink<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for OrgMacro<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Entity<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for LatexFragment<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for ExportSnippet<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for FootnoteReference<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        self.contents
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Citation<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for CitationReference<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        0
    }
}

impl<'s> StandardProperties<'s> for InlineBabelCall<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for InlineSourceBlock<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for LineBreak<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        0
    }
}

impl<'s> StandardProperties<'s> for Target<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for StatisticsCookie<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|post_blank| post_blank.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Subscript<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Superscript<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        Some(self.contents)
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for Timestamp<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        None
    }

    fn get_post_blank(&self) -> PostBlank {
        self.post_blank
            .map(|text| text.chars().count())
            .unwrap_or(0)
            .try_into()
            .expect("Too much post-blank to fit into a PostBlank.")
    }
}

impl<'s> StandardProperties<'s> for PlainText<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        self.source
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        // This field does not actually exist in emacs for plaintext
        Some(self.source)
    }

    fn get_post_blank(&self) -> PostBlank {
        // This field does not actually exist in emacs for plaintext
        0
    }
}

impl<'s> Timestamp<'s> {
    pub fn get_raw_value(&self) -> &'s str {
        self.source.trim_end()
    }
}

#[derive(Debug)]
pub enum LinkType<'s> {
    File,
    Protocol(Cow<'s, str>),
    Id,
    CustomId,
    CodeRef,
    Fuzzy,
}

impl<'s> RegularLink<'s> {
    /// Coalesce whitespace if the raw_link contains line breaks.
    ///
    /// This corresponds to the output you would get from the upstream emacs org-mode AST.
    pub fn get_raw_link(&self) -> Cow<'_, str> {
        coalesce_whitespace_if_line_break(&self.raw_link)
    }

    /// Coalesce whitespace if the path contains line breaks.
    ///
    /// This corresponds to the output you would get from the upstream emacs org-mode AST.
    pub fn get_path(&self) -> Cow<'_, str> {
        coalesce_whitespace_if_line_break(&self.path)
    }

    /// Coalesce whitespace if the search_option contains line breaks.
    ///
    /// This corresponds to the output you would get from the upstream emacs org-mode AST.
    pub fn get_search_option(&self) -> Option<Cow<'_, str>> {
        self.search_option
            .as_ref()
            .map(|search_option| coalesce_whitespace_if_line_break(search_option.borrow()))
    }
}

impl<'s> RadioLink<'s> {
    pub fn get_raw_link(&self) -> &'s str {
        self.path
    }
}

impl<'s> AngleLink<'s> {
    /// Remove line breaks but preserve multiple consecutive spaces.
    ///
    /// This corresponds to the output you would get from the upstream emacs org-mode AST.
    pub fn get_path(&self) -> Cow<'s, str> {
        remove_line_break(self.path)
    }

    /// Remove all whitespace but only if search_option contains a line break.
    ///
    /// This corresponds to the output you would get from the upstream emacs org-mode AST.
    pub fn get_search_option(&self) -> Option<Cow<'s, str>> {
        self.search_option.map(remove_whitespace_if_line_break)
    }
}

impl<'s> OrgMacro<'s> {
    pub fn get_key<'b>(&'b self) -> Cow<'s, str> {
        to_lowercase(self.key)
    }

    pub fn get_args<'b>(&'b self) -> impl Iterator<Item = Cow<'s, str>> + 'b {
        self.args
            .iter()
            .map(|arg| coalesce_whitespace_escaped('\\', |c| ",".contains(c))(arg))
    }
}

#[derive(Debug)]
pub enum FootnoteReferenceType {
    Standard,
    Inline,
}

impl<'s> FootnoteReference<'s> {
    pub fn get_type(&self) -> FootnoteReferenceType {
        if self.definition.is_empty() {
            FootnoteReferenceType::Standard
        } else {
            FootnoteReferenceType::Inline
        }
    }
}

impl<'s> StandardProperties<'s> for Object<'s> {
    fn get_source<'b>(&'b self) -> &'s str {
        match self {
            Object::Bold(inner) => inner.get_source(),
            Object::Italic(inner) => inner.get_source(),
            Object::Underline(inner) => inner.get_source(),
            Object::StrikeThrough(inner) => inner.get_source(),
            Object::Code(inner) => inner.get_source(),
            Object::Verbatim(inner) => inner.get_source(),
            Object::PlainText(inner) => inner.get_source(),
            Object::RegularLink(inner) => inner.get_source(),
            Object::RadioLink(inner) => inner.get_source(),
            Object::RadioTarget(inner) => inner.get_source(),
            Object::PlainLink(inner) => inner.get_source(),
            Object::AngleLink(inner) => inner.get_source(),
            Object::OrgMacro(inner) => inner.get_source(),
            Object::Entity(inner) => inner.get_source(),
            Object::LatexFragment(inner) => inner.get_source(),
            Object::ExportSnippet(inner) => inner.get_source(),
            Object::FootnoteReference(inner) => inner.get_source(),
            Object::Citation(inner) => inner.get_source(),
            Object::CitationReference(inner) => inner.get_source(),
            Object::InlineBabelCall(inner) => inner.get_source(),
            Object::InlineSourceBlock(inner) => inner.get_source(),
            Object::LineBreak(inner) => inner.get_source(),
            Object::Target(inner) => inner.get_source(),
            Object::StatisticsCookie(inner) => inner.get_source(),
            Object::Subscript(inner) => inner.get_source(),
            Object::Superscript(inner) => inner.get_source(),
            Object::Timestamp(inner) => inner.get_source(),
        }
    }

    fn get_contents<'b>(&'b self) -> Option<&'s str> {
        match self {
            Object::Bold(inner) => inner.get_contents(),
            Object::Italic(inner) => inner.get_contents(),
            Object::Underline(inner) => inner.get_contents(),
            Object::StrikeThrough(inner) => inner.get_contents(),
            Object::Code(inner) => inner.get_contents(),
            Object::Verbatim(inner) => inner.get_contents(),
            Object::PlainText(inner) => inner.get_contents(),
            Object::RegularLink(inner) => inner.get_contents(),
            Object::RadioLink(inner) => inner.get_contents(),
            Object::RadioTarget(inner) => inner.get_contents(),
            Object::PlainLink(inner) => inner.get_contents(),
            Object::AngleLink(inner) => inner.get_contents(),
            Object::OrgMacro(inner) => inner.get_contents(),
            Object::Entity(inner) => inner.get_contents(),
            Object::LatexFragment(inner) => inner.get_contents(),
            Object::ExportSnippet(inner) => inner.get_contents(),
            Object::FootnoteReference(inner) => inner.get_contents(),
            Object::Citation(inner) => inner.get_contents(),
            Object::CitationReference(inner) => inner.get_contents(),
            Object::InlineBabelCall(inner) => inner.get_contents(),
            Object::InlineSourceBlock(inner) => inner.get_contents(),
            Object::LineBreak(inner) => inner.get_contents(),
            Object::Target(inner) => inner.get_contents(),
            Object::StatisticsCookie(inner) => inner.get_contents(),
            Object::Subscript(inner) => inner.get_contents(),
            Object::Superscript(inner) => inner.get_contents(),
            Object::Timestamp(inner) => inner.get_contents(),
        }
    }

    fn get_post_blank(&self) -> PostBlank {
        match self {
            Object::Bold(inner) => inner.get_post_blank(),
            Object::Italic(inner) => inner.get_post_blank(),
            Object::Underline(inner) => inner.get_post_blank(),
            Object::StrikeThrough(inner) => inner.get_post_blank(),
            Object::Code(inner) => inner.get_post_blank(),
            Object::Verbatim(inner) => inner.get_post_blank(),
            Object::PlainText(inner) => inner.get_post_blank(),
            Object::RegularLink(inner) => inner.get_post_blank(),
            Object::RadioLink(inner) => inner.get_post_blank(),
            Object::RadioTarget(inner) => inner.get_post_blank(),
            Object::PlainLink(inner) => inner.get_post_blank(),
            Object::AngleLink(inner) => inner.get_post_blank(),
            Object::OrgMacro(inner) => inner.get_post_blank(),
            Object::Entity(inner) => inner.get_post_blank(),
            Object::LatexFragment(inner) => inner.get_post_blank(),
            Object::ExportSnippet(inner) => inner.get_post_blank(),
            Object::FootnoteReference(inner) => inner.get_post_blank(),
            Object::Citation(inner) => inner.get_post_blank(),
            Object::CitationReference(inner) => inner.get_post_blank(),
            Object::InlineBabelCall(inner) => inner.get_post_blank(),
            Object::InlineSourceBlock(inner) => inner.get_post_blank(),
            Object::LineBreak(inner) => inner.get_post_blank(),
            Object::Target(inner) => inner.get_post_blank(),
            Object::StatisticsCookie(inner) => inner.get_post_blank(),
            Object::Subscript(inner) => inner.get_post_blank(),
            Object::Superscript(inner) => inner.get_post_blank(),
            Object::Timestamp(inner) => inner.get_post_blank(),
        }
    }
}