psml 0.1.2

A library for serialising to, and deserialising from, PSML.
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
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
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::HashMap};

use crate::text::CharacterStyle;

use super::text::{Alignment, Heading, Image, Para};

// XRef
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum XRefDisplayKind {
    #[serde(rename = "document")]
    Document,
    #[serde(rename = "document+manual")]
    DocumentManual,
    #[serde(rename = "document+fragment")]
    DocumentFragment,
    #[serde(rename = "manual")]
    Manual,
    #[serde(rename = "template")]
    Template,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum XRefKind {
    None,
    Alternate,
    Math,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum BlockXRefKind {
    None,
    Alternate,
    Math,
    Embed,
    Transclude,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A PSML xref.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-xref.html
pub struct XRef {
    #[serde(rename = "@uriid", skip_serializing_if = "Option::is_none")]
    /// Destination uriid.
    pub uriid: Option<String>,
    #[serde(rename = "@docid", skip_serializing_if = "Option::is_none")]
    /// Destination docid.
    pub docid: Option<String>,
    #[serde(rename = "@href", skip_serializing_if = "Option::is_none")]
    /// Destination href.#
    pub href: Option<String>,
    #[serde(rename = "$text")]
    /// Text content to display instead of xref.
    pub content: String,
    #[serde(rename = "@config", skip_serializing_if = "Option::is_none")]
    /// XRef config name.
    pub config: Option<String>,
    #[serde(rename = "@display")]
    /// How target link text should be displayed.
    pub display: XRefDisplayKind,
    #[serde(rename = "@frag")]
    /// ID of fragment to link to.
    pub frag_id: String,
    #[serde(rename = "@labels", skip_serializing_if = "Option::is_none")]
    /// Comma separated xref labels.
    pub labels: Option<String>,
    #[serde(rename = "@level", skip_serializing_if = "Option::is_none")]
    /// Level for heading numbering of target document (1-5).
    pub level: Option<String>,
    #[serde(rename = "@reverselink")]
    /// Whether xref is bidirectional.
    pub reverselink: bool,
    #[serde(rename = "@reversetitle", skip_serializing_if = "Option::is_none")]
    /// Manually entered title for reverse xref.
    pub reversetitle: Option<String>,
    #[serde(rename = "@title", skip_serializing_if = "Option::is_none")]
    /// Manually entered title for xref.
    pub title: Option<String>,
    #[serde(rename = "@type", skip_serializing_if = "Option::is_none")]
    /// XRef type
    pub xref_type: Option<XRefKind>,
}

impl XRef {
    /// Returns a default xref to the given uriid.
    pub fn uriid(uriid: String) -> XRef {
        XRef {
            uriid: None,
            docid: None,
            href: Some(uriid),
            content: String::new(),
            config: None,
            display: XRefDisplayKind::Document,
            frag_id: "default".to_string(),
            labels: None,
            level: None,
            reverselink: true,
            reversetitle: None,
            title: None,
            xref_type: None,
        }
    }

    /// Returns a default xref to the given docid.
    pub fn docid(docid: String) -> XRef {
        XRef {
            uriid: None,
            docid: Some(docid),
            href: None,
            content: String::new(),
            config: None,
            display: XRefDisplayKind::Document,
            frag_id: "default".to_string(),
            labels: None,
            level: None,
            reverselink: true,
            reversetitle: None,
            title: None,
            xref_type: None,
        }
    }

    /// Returns a default xref to the given href.
    pub fn href(href: String) -> XRef {
        XRef {
            uriid: None,
            docid: None,
            href: Some(href),
            content: String::new(),
            config: None,
            display: XRefDisplayKind::Document,
            frag_id: "default".to_string(),
            labels: None,
            level: None,
            reverselink: true,
            reversetitle: None,
            title: None,
            xref_type: None,
        }
    }

    /// Adds the specified content to the xref and returns it.
    pub fn with_content(self, content: String) -> XRef {
        XRef { content, ..self }
    }

    /// Sets the title on the xref and returns it.
    pub fn with_title(self, title: Option<String>) -> XRef {
        XRef { title, ..self }
    }

    /// Sets the display mode on the xref and returns it.
    pub fn with_display(self, display: XRefDisplayKind) -> XRef {
        XRef { display, ..self }
    }
}

// Property

/// Property datatype attribute values.
/// Does not support custom datatypes - they will be converted to "string".
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PropertyDatatype {
    String,
    Date,
    Datetime,
    XRef,
    Link,
    Markdown,
    Markup,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PropertyValue {
    XRef(Box<XRef>),
    Link(String),
    Markdown(String),
    Markup(String),
    Value(String),
}

impl From<String> for PropertyValue {
    fn from(value: String) -> Self {
        Self::Value(value)
    }
}

impl PropertyValue {
    pub fn datatype(&self) -> PropertyDatatype {
        match self {
            Self::XRef(_) => PropertyDatatype::XRef,
            Self::Link(_) => PropertyDatatype::Link,
            Self::Markdown(_) => PropertyDatatype::Markdown,
            Self::Markup(_) => PropertyDatatype::Markup,
            Self::Value(_) => PropertyDatatype::String,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename = "property")]
/// A PSML property.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-property.html
pub struct Property {
    #[serde(rename = "@name")]
    pub name: String,
    #[serde(rename = "@title", skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(rename = "@datatype", skip_serializing_if = "Option::is_none")]
    pub datatype: Option<PropertyDatatype>,
    #[serde(rename = "@multiple", skip_serializing_if = "Option::is_none")]
    pub multiple: Option<bool>,
    #[serde(rename = "@value", skip_serializing_if = "Option::is_none")]
    pub attr_value: Option<String>,
    #[serde(rename = "$value", default)]
    pub values: Vec<PropertyValue>,
}

lazy_static! {
    /// Matches a leading '-' or a char not in [a-zA-Z0-9_-].
    static ref PROPERTY_BAD_NAME: Regex = Regex::new(r"(^-|[^a-zA-Z0-9_-]+)").unwrap();
}

impl Property {
    pub fn with_value(name: String, title: String, value: PropertyValue) -> Self {
        let datatype = value.datatype();
        Property {
            name,
            title: Some(title),
            values: vec![value],
            datatype: Some(datatype),
            multiple: None,
            attr_value: None,
        }
    }

    /// Replaces characters in `name` that are illegal for a PSML Property name.
    pub fn sanitize_name<'a>(name: &'a str, repl: &str) -> Cow<'a, str> {
        PROPERTY_BAD_NAME.replace_all(name, repl)
    }
}

// Fragments

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A PSML properties fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-properties-fragment.html
pub struct PropertiesFragment {
    #[serde(rename = "@id")]
    /// ID of the fragment.
    pub id: String,
    #[serde(rename = "@type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Template type for the fragment.
    pub frag_type: Option<String>,
    #[serde(rename = "@labels")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Labels on this fragment.
    pub labels: Option<String>,
    #[serde(rename = "property", default)]
    /// Properties in this fragment.
    pub properties: Vec<Property>,
    #[serde(flatten)]
    /// Other attributes on this fragment.
    pub attrs: HashMap<String, String>,
}

impl PropertiesFragment {
    /// Creates a new empty fragment with the given id.
    pub fn new(id: String) -> PropertiesFragment {
        PropertiesFragment {
            id,
            frag_type: None,
            labels: None,
            properties: vec![],
            attrs: HashMap::new(),
        }
    }

    /// Adds the properties to the fragment and returns it.
    pub fn with_properties(self, properties: Vec<Property>) -> PropertiesFragment {
        PropertiesFragment {
            id: self.id,
            frag_type: self.frag_type,
            labels: self.labels,
            properties: vec![self.properties, properties]
                .into_iter()
                .flatten()
                .collect::<Vec<Property>>(),
            attrs: self.attrs,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
pub struct BlockXRef {
    #[serde(rename = "@docid", skip_serializing_if = "Option::is_none")]
    pub docid: Option<String>,
    #[serde(rename = "@href", skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
    #[serde(rename = "@uriid", skip_serializing_if = "Option::is_none")]
    pub uriid: Option<String>,

    #[serde(rename = "@archived")]
    pub archived: Option<bool>,
    #[serde(rename = "@config", skip_serializing_if = "Option::is_none")]
    pub config: Option<String>,
    #[serde(rename = "@display", skip_serializing_if = "Option::is_none")]
    pub display: Option<XRefDisplayKind>,
    #[serde(rename = "@documenttype", skip_serializing_if = "Option::is_none")]
    pub documenttype: Option<String>,
    #[serde(rename = "@external", skip_serializing_if = "Option::is_none")]
    pub external: Option<bool>,
    #[serde(rename = "@frag")]
    pub frag: String,
    #[serde(rename = "@id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(rename = "@labels", skip_serializing_if = "Option::is_none")]
    pub labels: Option<String>,
    #[serde(rename = "@level", skip_serializing_if = "Option::is_none")]
    pub level: Option<u8>,
    #[serde(rename = "@mediatype", skip_serializing_if = "Option::is_none")]
    pub mediatype: Option<String>,
    #[serde(rename = "@reversetitle", skip_serializing_if = "Option::is_none")]
    pub reversetitle: Option<String>,
    #[serde(rename = "@reverselink", skip_serializing_if = "Option::is_none")]
    pub reverselink: Option<bool>,
    #[serde(rename = "@reversefrag", skip_serializing_if = "Option::is_none")]
    pub reversefrag: Option<String>,
    #[serde(rename = "@title", skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(rename = "@type", skip_serializing_if = "Option::is_none")]
    pub xref_type: Option<BlockXRefKind>,
    #[serde(rename = "@unresolved", skip_serializing_if = "Option::is_none")]
    pub unresolved: Option<bool>,
    #[serde(rename = "@urititle", skip_serializing_if = "Option::is_none")]
    pub urititle: Option<String>,
    #[serde(rename = "@urilabels", skip_serializing_if = "Option::is_none")]
    pub urilabels: Option<String>,
}

impl BlockXRef {
    pub fn docid(docid: String) -> Self {
        Self {
            docid: Some(docid),
            ..Default::default()
        }
    }

    pub fn uriid(uriid: String) -> Self {
        Self {
            uriid: Some(uriid),
            ..Default::default()
        }
    }

    pub fn href(href: String) -> Self {
        Self {
            href: Some(href),
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A PSML xref fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-xref-fragment.html
pub struct XRefFragment {
    #[serde(rename = "@id")]
    /// ID of the fragment.
    pub id: String,
    #[serde(rename = "@type")]
    /// Template type for the fragment.
    pub frag_type: Option<String>,
    #[serde(rename = "@labels")]
    /// Labels on this fragment.
    pub labels: String,
    #[serde(rename = "blockxref", default)]
    pub xrefs: Vec<BlockXRef>,
    #[serde(flatten)]
    /// Other attributes on this fragment.
    pub attrs: HashMap<String, String>,
}

impl XRefFragment {
    /// Creates a new empty fragment with the given id.
    pub fn new(id: String) -> XRefFragment {
        XRefFragment {
            id,
            frag_type: None,
            labels: String::new(),
            xrefs: Vec::new(),
            attrs: HashMap::new(),
        }
    }

    /// Adds the xrefs to the fragment and returns it.
    pub fn with_xrefs(self, xrefs: Vec<BlockXRef>) -> XRefFragment {
        XRefFragment {
            id: self.id,
            frag_type: self.frag_type,
            labels: self.labels,
            xrefs: vec![self.xrefs, xrefs]
                .into_iter()
                .flatten()
                .collect::<Vec<BlockXRef>>(),
            attrs: self.attrs,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct TableCaption {
    #[serde(rename = "$text", default)]
    caption: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TablePart {
    Header,
    Body,
    Footer,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
#[serde(rename = "col")]
pub struct TableColumn {
    #[serde(rename = "@align", skip_serializing_if = "Option::is_none")]
    pub align: Option<Alignment>,
    #[serde(rename = "@part", skip_serializing_if = "Option::is_none")]
    pub part: Option<TablePart>,
    #[serde(rename = "@role", skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(rename = "@width", skip_serializing_if = "Option::is_none")]
    pub width: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
#[serde(rename = "row")]
pub struct TableRow {
    #[serde(rename = "@align", skip_serializing_if = "Option::is_none")]
    pub align: Option<Alignment>,
    #[serde(rename = "@part", skip_serializing_if = "Option::is_none")]
    pub part: Option<TablePart>,
    #[serde(rename = "@role", skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(rename = "cell", default)]
    pub cells: Vec<TableCell>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
#[serde(rename = "cell")]
pub struct TableCell {
    #[serde(rename = "@align", skip_serializing_if = "Option::is_none")]
    pub align: Option<Alignment>,
    #[serde(rename = "@role", skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(rename = "@colspan", skip_serializing_if = "Option::is_none")]
    pub colspan: Option<u64>,
    #[serde(rename = "@rowspan", skip_serializing_if = "Option::is_none")]
    pub rowspan: Option<u64>,
    #[serde(rename = "$value", default)]
    pub content: Vec<CharacterStyle>,
    // TODO extend content to include images etc.
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Table {
    pub caption: Option<TableCaption>,
    #[serde(rename = "@role", skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(rename = "@summary", skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    #[serde(rename = "@height", skip_serializing_if = "Option::is_none")]
    pub height: Option<String>,
    #[serde(rename = "@width", skip_serializing_if = "Option::is_none")]
    pub width: Option<String>,
    #[serde(rename = "col", default)]
    pub cols: Vec<TableColumn>,
    #[serde(rename = "row", default)]
    pub rows: Vec<TableRow>,
}

impl Table {
    pub fn basic(cols: usize, cells: Vec<Vec<String>>, caption: String) -> Self {
        Table {
            caption: Some(TableCaption { caption }),
            role: None,
            summary: None,
            height: None,
            width: None,
            cols: (0..cols).map(|_| TableColumn::default()).collect(),
            rows: cells
                .into_iter()
                .map(|row| TableRow {
                    cells: row
                        .into_iter()
                        .map(|cell| TableCell {
                            content: vec![CharacterStyle::Text(cell)],
                            ..Default::default()
                        })
                        .collect(),
                    ..Default::default()
                })
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum FragmentContent {
    #[serde(rename = "$text")]
    Text(String),
    Heading(Heading),
    Block {
        #[serde(rename = "$value", default)]
        child: Vec<FragmentContent>,
    },
    BlockXRef(BlockXRef),
    Para(Para),
    Preformat {
        #[serde(rename = "$value", default)]
        child: Vec<CharacterStyle>,
    },
    Image(Image),
    Table(Table),
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A PSML fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-fragment.html
pub struct Fragment {
    #[serde(rename = "@id")]
    /// ID of the fragment.
    pub id: String,
    #[serde(rename = "@type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Template type for the fragment.
    pub frag_type: Option<String>,
    #[serde(rename = "@labels")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Labels on this fragment.
    pub labels: Option<String>,
    #[serde(rename = "$value", default)]
    /// Contents of the fragment.
    pub content: Vec<FragmentContent>,
    // #[serde(flatten)]
    // /// Other attributes.
    // pub attrs: HashMap<String, String>,

    // TODO ^ wait till $value and flatten can be used together ^
    // https://github.com/tafia/quick-xml/issues/326
}

impl Fragment {
    /// Creates a new empty fragment with the given id.
    pub fn new(id: String) -> Fragment {
        Fragment {
            id,
            frag_type: None,
            labels: None,
            content: vec![],
            // attrs: HashMap::new(),
        }
    }

    /// Adds the content to the fragment and returns it.
    pub fn with_content(mut self, content: Vec<FragmentContent>) -> Fragment {
        self.content.extend(content);
        Fragment {
            id: self.id,
            frag_type: self.frag_type,
            labels: self.labels,
            content: self.content,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Fragments {
    #[serde(rename = "fragment")]
    Fragment(Fragment),
    #[serde(rename = "properties-fragment")]
    Properties(PropertiesFragment),
    #[serde(rename = "xref-fragment")]
    Xref(XRefFragment),
    #[serde(rename = "media-fragment")]
    Media(()),
}

// Section

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum SectionContent {
    #[serde(rename = "fragment")]
    Fragment(Fragment),
    #[serde(rename = "properties-fragment")]
    PropertiesFragment(PropertiesFragment),
    #[serde(rename = "xref-fragment")]
    XRefFragment(XRefFragment),
    #[serde(rename = "media-fragment")]
    Media(()),
    #[serde(rename = "title")]
    Title {
        #[serde(rename = "$text", default)]
        text: String,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A PSML Section.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-section.html
pub struct Section {
    #[serde(rename = "@id")]
    /// ID of the section.
    pub id: String,
    #[serde(rename = "@title")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Title of the section in the UI.
    pub title: Option<String>,
    #[serde(rename = "title")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Title of the content.
    pub content_title: Option<String>,
    #[serde(rename = "@edit")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Whether fragments in this section can be edited in the UI.
    pub edit: Option<bool>,
    #[serde(rename = "@lockstructure")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Whether the structure of this section can be modified.
    pub lockstructure: Option<bool>,
    #[serde(rename = "@overwrite")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Whether the existing section/fragments are to be overwritten by these during upload.
    pub overwrite: Option<bool>,
    #[serde(rename = "@fragmenttype")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Fragment types this section is allowed to contain.
    pub fragment_types: Option<String>,
    #[serde(rename = "$value", default)]
    /// Fragments in this section.
    pub content: Vec<SectionContent>,
}

impl Section {
    /// Creates a new empty fragment with the given id.
    pub fn new(id: String) -> Section {
        Section {
            id,
            title: None,
            content_title: None,
            edit: Some(true),
            lockstructure: Some(false),
            overwrite: Some(true),
            fragment_types: None,
            content: Vec::new(),
        }
    }

    pub fn add_fragment(&mut self, fragment: Fragments) {
        self.content.push(match fragment {
            Fragments::Fragment(frag) => SectionContent::Fragment(frag),
            Fragments::Media(frag) => SectionContent::Media(frag),
            Fragments::Properties(frag) => SectionContent::PropertiesFragment(frag),
            Fragments::Xref(frag) => SectionContent::XRefFragment(frag),
        });
    }

    pub fn with_fragments(mut self, fragments: Vec<Fragments>) -> Self {
        for frag in fragments {
            self.add_fragment(frag);
        }

        self
    }
}

// Document

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// Describes the publication.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-publication.html
pub struct Publication {
    // #[serde(rename = "@id")]
    /// Publication ID.
    pub id: String,
    #[serde(rename = "@type")]
    /// Publication type.
    pub pub_type: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// The <description> element is used to provide a short text-only description.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-description.html
pub struct Description {
    #[serde(rename = "$value")]
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
/// Metadata about this URI.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-uri.html
pub struct URIDescriptor {
    // Attributes
    #[serde(rename = "@docid", skip_serializing_if = "Option::is_none")]
    /// Docid of this document.
    pub docid: Option<String>,
    #[serde(rename = "@documenttype", skip_serializing_if = "Option::is_none")]
    /// Type of the document.
    pub doc_type: Option<String>,
    #[serde(rename = "@title", skip_serializing_if = "Option::is_none")]
    /// Title for the document.
    pub title: Option<String>,
    #[serde(rename = "@folder", skip_serializing_if = "Option::is_none")]
    /// If true, this is a folder.
    pub folder: Option<bool>,

    // Elements
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Description of the document.
    pub description: Option<Description>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Labels on the document.
    pub labels: Option<Labels>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
/// Wrapper for metadata about the document.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-documentinfo.html
pub struct DocumentInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    /// URI descriptor.
    pub uri: Option<URIDescriptor>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Publication descriptor.
    pub publication: Option<Publication>,
}

// TODO change this to vec of strings with custom deserializer.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// A comma-separated list of label values for a document, note or fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-labels.html
pub struct Labels {
    #[serde(rename = "$value", default)]
    pub value: String,
}

/// Previous document content if different from current (used when doing a compare).
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-content.html
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Content {
    #[serde(rename = "$value")]
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// The note on the last notification of the fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-note.html
pub struct Note {
    // #[serde(rename = "@id")]
    pub id: Option<String>,
    #[serde(rename = "@title")]
    /// Title of the note.
    pub title: Option<String>,
    #[serde(rename = "@modified")]
    /// Date and time this note was modified.
    pub modified: String,
    /// Labels on this note.
    pub labels: Labels,
    /// Content in this note.
    pub content: Content,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// The notes on the last notification of the fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-notes.html
pub struct Notes {
    pub notes: Vec<Note>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
/// Metadata relating to a fragment.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-locator.html
pub struct Locator {
    #[serde(rename = "fragment")]
    /// ID of the fragment.
    pub fragment_id: Option<String>,
    /// Notes on this fragment.
    pub notes: Option<Notes>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct FragmentInfo {
    pub value: Vec<Locator>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum DocumentLevel {
    Metadata,
    Portable,
    Processed,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename = "document")]
/// A PSML document.
/// For PSML definition see: https://dev.pageseeder.com/psml/element_reference/element-document.html
pub struct Document {
    #[serde(rename = "documentinfo")]
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Metadata about the document.
    pub doc_info: Option<DocumentInfo>,
    #[serde(rename = "fragmentinfo", default)]
    /// Fragment metadata
    pub frag_info: Vec<Locator>,
    #[serde(rename = "section")]
    /// Sections in the document.
    pub sections: Vec<Section>,
    #[serde(rename = "@type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doc_type: Option<String>,
    #[serde(rename = "@edit")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub edit: Option<bool>,
    #[serde(rename = "@level")]
    pub level: DocumentLevel,
    #[serde(rename = "@lockstructure")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lockstructure: Option<bool>,
}

// TODO impl toc index

impl Document {
    pub fn docid(&self) -> Option<&str> {
        match &self.doc_info {
            Some(doc_info) => match &doc_info.uri {
                Some(uri) => match &uri.docid {
                    Some(docid) => Some(docid),
                    None => None,
                },
                None => None,
            },
            None => None,
        }
    }

    pub fn get_section(&self, id: &str) -> Option<&Section> {
        self.sections.iter().find(|&section| section.id == id)
    }

    pub fn get_mut_section(&mut self, id: &str) -> Option<&mut Section> {
        self.sections.iter_mut().find(|s| s.id == id)
    }
}

impl Default for Document {
    fn default() -> Self {
        Self {
            doc_info: None,
            frag_info: vec![],
            sections: vec![],
            doc_type: None,
            edit: None,
            level: DocumentLevel::Portable,
            lockstructure: None,
        }
    }
}