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
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
use indexmap::IndexMap;
use quick_xml::{Writer, events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event}};
use rio_api::model::{BlankNode, Literal, NamedNode, NamedOrBlankNode, Term, Triple};
use std::{cmp::Ordering, collections::{HashMap, HashSet, VecDeque}, fmt::{Debug, Formatter}};
use std::{self, cell::RefCell, fmt,
          hash::{Hash,Hasher},
          io::{self, Write}};

// Utilities
pub fn is_name_start_char(c: char) -> bool {
    // ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
    matches!(c,
        ':'
        | 'A'..='Z'
        | '_'
        | 'a'..='z'
        | '\u{C0}'..='\u{D6}'
        | '\u{D8}'..='\u{F6}'
        | '\u{F8}'..='\u{2FF}'
        | '\u{370}'..='\u{37D}'
        | '\u{37F}'..='\u{1FFF}'
        | '\u{200C}'..='\u{200D}'
        | '\u{2070}'..='\u{218F}'
        | '\u{2C00}'..='\u{2FEF}'
        | '\u{3001}'..='\u{D7FF}'
        | '\u{F900}'..='\u{FDCF}'
        | '\u{FDF0}'..='\u{FFFD}'
        | '\u{10000}'..='\u{EFFFF}')
}

pub fn is_name_char(c: char) -> bool {
    // NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
    is_name_start_char(c)
        || matches!(c,  '-' | '.' | '0'..='9' | '\u{B7}' | '\u{0300}'..='\u{036F}' | '\u{203F}'..='\u{2040}')
}

fn map_err(error: quick_xml::Error) -> io::Error {
    if let quick_xml::Error::Io(error) = error {
        error
    } else {
        io::Error::new(io::ErrorKind::Other, error)
    }
}


// We need a complete copy of the while data model because we need to
// be able to copy and cache items without worrying too much about
// lifetimes and without allocation. AsRef<str> supports both of
// this, assuming that there is an Rc in the way somewhere

#[derive(Ord, PartialOrd, Clone)]
pub struct PNamedNode<A:AsRef<str>> {
    pub iri: A,
    position_cache: RefCell<bool>,
    position_base: RefCell<Option<usize>>,
    position_add: RefCell<Option<usize>>,
}

impl<A:AsRef<str>> PNamedNode<A> {
    pub fn new(iri: A) -> Self {
        PNamedNode{iri, position_cache: RefCell::new(false),
                       position_base: RefCell::new(None),
                       position_add: RefCell::new(None)}
    }
}

impl <A:Debug + AsRef<str>> Debug for PNamedNode<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> ::core::fmt::Result {
        match *self {
            PNamedNode {
                ref iri,
                position_cache: _,
                position_base: _,
                position_add:_
            } => {
                let mut debug_trait_builder =
                    f.debug_struct("PNamedNode");
                let _ = debug_trait_builder.field("iri", &&(*iri));
                debug_trait_builder.finish()
            }
        }
    }
}

impl<A:AsRef<str>> Hash for PNamedNode<A> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.iri.as_ref().hash(state);
    }
}

impl<A:AsRef<str>> PartialEq for PNamedNode<A> {
    fn eq(&self, other: &Self) -> bool {
        self.iri.as_ref() == other.iri.as_ref()
    }
}

impl<A:AsRef<str>> Eq for PNamedNode<A> {}


impl<A:AsRef<str>> PNamedNode<A> {
    fn split_iri(&self) -> (&str, &str) {
        let iri = self.iri.as_ref();

        let mut position_cache = self.position_cache.borrow_mut();
        let mut position_base = self.position_base.borrow_mut();
        let mut position_add = self.position_add.borrow_mut();

        if !*position_cache {
            *position_cache = true;
            *position_base = iri.rfind(|c| !is_name_char(c) || c == ':');
            if let Some(position_base) = *position_base {
                *position_add = iri[position_base..].find(|c| is_name_start_char(c) && c != ':')
            }
        }

        if let Some(position_base) = *position_base {
            if let Some(position_add) = *position_add {
                (
                    &iri[..position_base + position_add],
                    &iri[position_base + position_add..],
                )
            } else {
                (iri, "")
            }
        } else {
            (iri, "")
        }
    }
}

impl<A:AsRef<str>> fmt::Display for PNamedNode<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let nn:NamedNode<'_> = self.into();
        write!(f, "{}", nn)
    }
}

impl<'a, A:AsRef<str>> From<&'a PNamedNode<A>> for NamedNode<'a> {
    fn from(arnn: &'a PNamedNode<A>) -> Self {
        NamedNode{iri: arnn.iri.as_ref()}
    }
}

impl From<NamedNode<'_>> for PNamedNode<String> {
    fn from(nn: NamedNode<'_>) -> Self {
        let iri: String = nn.iri.to_string();
        PNamedNode::new(iri)
    }
}

impl<A:AsRef<str>> AsRef<str> for PNamedNode<A> {
    fn as_ref(&self) -> &str {
        self.iri.as_ref()
    }
}


#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct PBlankNode<A:AsRef<str>> {
    pub id: A,
}

impl<A:AsRef<str>> PBlankNode<A> {
    pub fn new(id: A) -> Self {
        PBlankNode{id}
    }
}

impl<A:AsRef<str>> fmt::Display for PBlankNode<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let nn:BlankNode<'_> = self.into();
        write!(f, "{}", nn)
    }
}

impl<'a, A:AsRef<str>> From<&'a PBlankNode<A>> for BlankNode<'a> {
    fn from(arbn: &'a PBlankNode<A>) -> Self {
        BlankNode{id: arbn.id.as_ref()}
    }
}

impl From<BlankNode<'_>> for PBlankNode<String> {
    fn from(bn: BlankNode<'_>) -> Self {
        PBlankNode{id:bn.id.to_string()}
    }
}

impl<A:AsRef<str>> AsRef<str> for PBlankNode<A> {
    fn as_ref(&self) -> &str {
        self.id.as_ref()
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum PLiteral<A:AsRef<str>> {
    Simple {
        value: A,
    },
    LanguageTaggedString {
        value: A,
        language: A,
    },
    Typed {
        value: A,
        datatype: PNamedNode<A>,
    },
}

impl<A:AsRef<str>> fmt::Display for PLiteral<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let nn:Literal<'_> = self.into();
        write!(f, "{}", nn)
    }
}

impl<'a, A:AsRef<str>> From<&'a PLiteral<A>> for Literal<'a> {
    fn from(l: &'a PLiteral<A>) -> Self {
        match l {
            PLiteral::Simple { value } =>
                Literal::Simple{value:value.as_ref()},
            PLiteral::LanguageTaggedString { value, language } =>
                Literal::LanguageTaggedString{
                    value: value.as_ref(),
                    language: language.as_ref(),
                },
            PLiteral::Typed { value, datatype } =>
                Literal::Typed {
                    value: value.as_ref(),
                    datatype: datatype.into(),
                },
        }
    }
}


impl From<Literal<'_>> for PLiteral<String> {
    fn from(l: Literal<'_>) -> Self {
        match l {
            Literal::Simple { value } =>
                PLiteral::Simple{value:value.to_string()},
            Literal::LanguageTaggedString { value, language } =>
                PLiteral::LanguageTaggedString{
                    value: value.to_string(),
                    language: language.to_string(),
                },
            Literal::Typed { value, datatype } =>
                PLiteral::Typed {
                    value: value.to_string(),
                    datatype: datatype.into(),
                },
        }
    }
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum PNamedOrBlankNode<A:AsRef<str>> {
    NamedNode(PNamedNode<A>),
    BlankNode(PBlankNode<A>),
}

impl<A:AsRef<str>> fmt::Display for PNamedOrBlankNode<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let nn:NamedOrBlankNode<'_> = self.into();
        write!(f, "{}", nn)
    }
}

impl<'a, A:AsRef<str>> From<&'a PNamedOrBlankNode<A>> for NamedOrBlankNode<'a> {
    fn from(anbn: &'a PNamedOrBlankNode<A>) -> Self {
        match anbn {
            PNamedOrBlankNode::NamedNode(nn) =>
                NamedOrBlankNode::NamedNode(nn.into()),
            PNamedOrBlankNode::BlankNode(bn) =>
                NamedOrBlankNode::BlankNode(bn.into())
        }
    }
}

impl From<NamedOrBlankNode<'_>> for PNamedOrBlankNode<String> {
    fn from(nbn: NamedOrBlankNode<'_>) -> Self {
        match nbn {
            NamedOrBlankNode::NamedNode(nn) =>
                PNamedOrBlankNode::NamedNode(nn.into()),
            NamedOrBlankNode::BlankNode(bn) =>
                PNamedOrBlankNode::BlankNode(bn.into()),
        }
    }
}

impl<A:AsRef<str>> From<PNamedNode<A>> for PNamedOrBlankNode<A> {
    fn from(nn: PNamedNode<A>) -> Self {
        PNamedOrBlankNode::NamedNode(nn)
    }
}

impl<A:AsRef<str>> From<PBlankNode<A>> for PNamedOrBlankNode<A> {
    fn from(nn: PBlankNode<A>) -> Self {
        PNamedOrBlankNode::BlankNode(nn)
    }
}

impl<A:AsRef<str>> AsRef<str> for PNamedOrBlankNode<A> {
    fn as_ref(&self) -> &str {
        match self {
            PNamedOrBlankNode::NamedNode(nn) => nn.as_ref(),
            PNamedOrBlankNode::BlankNode(bn) => bn.as_ref(),
        }
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum PTerm<A:AsRef<str>> {
    NamedNode(PNamedNode<A>),
    BlankNode(PBlankNode<A>),
    Literal(PLiteral<A>),
}

impl<A:AsRef<str>> PartialEq<PNamedOrBlankNode<A>> for PTerm<A> {
    fn eq(&self, other: &PNamedOrBlankNode<A>) -> bool {
        match (self, other) {
            (Self::NamedNode(nn), PNamedOrBlankNode::NamedNode(onn))
                => nn.iri.as_ref() == onn.iri.as_ref(),
            (Self::BlankNode(bn), PNamedOrBlankNode::BlankNode(obn))
                => bn.id.as_ref() == obn.id.as_ref(),
            _ => false
        }
    }
}

impl<A:AsRef<str>> From<PNamedOrBlankNode<A>> for PTerm<A> {
    fn from(nbn: PNamedOrBlankNode<A>) -> Self {
        match nbn {
            PNamedOrBlankNode::NamedNode(nn) => nn.into(),
            PNamedOrBlankNode::BlankNode(bn) => bn.into(),
        }
    }
}

impl<A:AsRef<str>> fmt::Display for PTerm<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let t:Term<'_> = self.into();
        write!(f, "{}", t)
    }
}

impl<'a, A:AsRef<str>> From<&'a PTerm<A>> for Term<'a> {
    fn from(t: &'a PTerm<A>) -> Self {
        match t {
            PTerm::NamedNode(nn) =>
                Term::NamedNode(nn.into()),
            PTerm::BlankNode(bn) =>
                Term::BlankNode(bn.into()),
            PTerm::Literal(l) =>
                Term::Literal(l.into()),
        }
    }
}

impl From<Term<'_>> for PTerm<String> {
    fn from(t: Term<'_>) -> Self {
        match t {
            Term::NamedNode(nn) =>
                PTerm::NamedNode(nn.into()),
            Term::BlankNode(bn) =>
                PTerm::BlankNode(bn.into()),
            Term::Literal(l) =>
                PTerm::Literal(l.into()),
        }
    }
}

impl<A:AsRef<str>> From<PBlankNode<A>> for PTerm<A> {
    fn from(nn: PBlankNode<A>) -> Self {
        PTerm::BlankNode(nn)
    }
}

impl<A:AsRef<str>> From<PNamedNode<A>> for PTerm<A> {
    fn from(nn: PNamedNode<A>) -> Self {
        PTerm::NamedNode(nn)
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PTriple<A: AsRef<str>> {
    pub subject: PNamedOrBlankNode<A>,
    pub predicate: PNamedNode<A>,
    pub object: PTerm<A>
}

impl<A:AsRef<str>> PTriple<A> {
    pub fn new(subject:PNamedOrBlankNode<A>,
               predicate:PNamedNode<A>,
               object:PTerm<A>) -> PTriple<A> {
        PTriple{subject, predicate, object}
    }

    pub fn is_type(&self) -> bool {
        self.predicate.iri.as_ref() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
    }

    pub fn is_collection(&self) -> bool {
        self.is_collection_first() || self.is_collection_rest()
    }

    pub fn is_collection_first(&self) -> bool {
        &self.predicate.iri.as_ref() == &"http://www.w3.org/1999/02/22-rdf-syntax-ns#first"
    }

    pub fn is_collection_rest(&self) -> bool {
        &self.predicate.iri.as_ref() == &"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"
    }

    pub fn is_collection_end(&self) -> bool {
        if let PTerm::NamedNode(nn) = &self.object {
            nn.iri.as_ref() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"
        } else {
            false
        }
    }

    pub fn printable(&self) -> String {
        format!(
            "{}\n\t{}\n\t{}",
            self.subject,
            self.predicate,
            self.object
        )
    }
}

impl<A:AsRef<str>> fmt::Display for PTriple<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let t:Triple<'_> = self.into();
        write!(f, "{}", t)
    }
}

impl<'a, A:AsRef<str>> From<&'a PTriple<A>> for Triple<'a>{
    fn from(t: &'a PTriple<A>) -> Self {
        Triple {
            subject: (&t.subject).into(),
            predicate: (&t.predicate).into(),
            object: (&t.object).into()
        }
    }
}

impl From<Triple<'_>> for PTriple<String> {
    fn from(t: Triple<'_>) -> Self {
        PTriple {
            subject: t.subject.into(),
            predicate: t.predicate.into(),
            object: t.object.into()
        }
    }
}

trait TripleLike<A>
    where A:AsRef<str> + Clone
{
    /// Can a new Triple be accepted onto this TripleLike.
    fn accept(&mut self, t:PTriple<A>) -> Option<PTriple<A>>;

    /// What is the subject of the triple like
    fn subject(&self) -> &PNamedOrBlankNode<A>;

    fn literal_objects(&self) -> Vec<&PTriple<A>>;

    fn find_typed(&self) -> Option<&PTriple<A>>;
}

// A set of triples with a shared subject
// All the triples in `vec` should start with `subject`.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PMultiTriple<A:AsRef<str>> {
    vec: Vec<PTriple<A>>,
}

impl<A> PMultiTriple<A>
where A: AsRef<str> + PartialEq
{
    #[allow(dead_code)]
    pub (crate) fn empty() -> PMultiTriple<A> {
        PMultiTriple{vec:vec![]}
    }

    pub fn new(vec:Vec<PTriple<A>>) -> PMultiTriple<A> {
        PMultiTriple{vec}
    }

    pub fn len(&self) -> usize {
        self.vec.len()
    }
}

impl<A> TripleLike<A> for PMultiTriple<A>
where A: AsRef<str> + Clone + PartialEq
{
    fn accept(&mut self, t:PTriple<A>) -> Option<PTriple<A>> {
        if self.subject().as_ref() == t.subject.as_ref() {
            self.vec.push(t);
            None
        } else {
            Some(t)
        }
    }

    fn subject(&self) -> &PNamedOrBlankNode<A> {
        // There should be no empty instances, so this should be safe
        &self.vec[0].subject
    }

    fn literal_objects(&self) -> Vec<&PTriple<A>> {
        self.vec.iter().filter(|t| matches!(t.object, PTerm::Literal(_))).collect()
    }

    fn find_typed(&self) -> Option<&PTriple<A>> {
        self.vec.iter().find(
            |et| et.is_type()
        )
    }
}


// A set of terms that should be rendered as a RDF list, using first
// as a the subject of the first node
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PTripleSeq<A:AsRef<str>> {
    list_seq: VecDeque<(PNamedOrBlankNode<A>, Option<PTriple<A>>, PTriple<A>)>,
}

impl<A:AsRef<str> + Eq> From<PTripleSeq<A>> for Vec<PMultiTriple<A>> {
    fn from(seq: PTripleSeq<A>) -> Self {
        let mut v = vec![];
        for tup in seq.list_seq {
            let mut items = vec![];
            if let Some(t) = tup.1 {
                items.push(t);
            }
            items.push(tup.2);
            v.push(PMultiTriple::new(items));
        }
        v
    }
}

impl<A:AsRef<str> + Clone> PTripleSeq<A> {
    #[allow(dead_code)]
    pub (crate) fn empty() -> PTripleSeq<A> {
        PTripleSeq{list_seq: VecDeque::new()}
    }

    pub fn from_end(t:PTriple<A>) -> PTripleSeq<A> {
        let mut seq = PTripleSeq{list_seq: vec![].into()};
        if let PNamedOrBlankNode::BlankNode(_) = &t.subject {
            seq.list_seq.push_front((t.subject.clone(), None, t));
        } else {
            todo!("This shouldn't happen")
        }
        seq
    }

    pub fn has_literal(&self) -> bool {
        self.list_seq.iter().any (
            |(_, t, _)|
            matches!(t,
                     Some(
                         PTriple {
                             subject:_,
                             predicate:_,
                             object: PTerm::Literal(_)
                         }
                     )
            )
        )
    }
}

impl<A> TripleLike<A> for PTripleSeq<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq
{
    fn accept(&mut self, t:PTriple<A>) -> Option<PTriple<A>> {
        if t.is_collection_first() {
            if let Some(pos) = self.list_seq.iter().position(
                |tup| &tup.0 == &t.subject
            ){
                if let Some(tuple) = self.list_seq.get_mut(pos){
                    (*tuple).1 = Some(t)
                }

                return None;
            }
        }

        if let PTerm::BlankNode(bn) = &t.object {
            if let &PNamedOrBlankNode::BlankNode(ref snn) = self.subject() {
                if t.is_collection_rest() && snn == bn {
                    self.list_seq.push_front((t.subject.clone(), None, t));
                    return None;
                }
            }
        }

        Some(t)
    }

    fn subject(&self) -> &PNamedOrBlankNode<A> {
        &self.list_seq[0].0
    }

    fn literal_objects(&self) -> Vec<&PTriple<A>> {
        vec![]
    }

    fn find_typed(&self) -> Option<&PTriple<A>> {
        None
    }
}

// All the different forms of RDF subgraph
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum PEpandedTriple<A:AsRef<str>> {
    PMultiTriple(PMultiTriple<A>),
    PTripleSeq(PTripleSeq<A>),
}

impl<A> From<PTriple<A>> for PMultiTriple<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq
{
    fn from(t: PTriple<A>) -> Self {
        PMultiTriple{
            vec: vec![t]
        }
    }
}

impl<A> From<PTriple<A>> for PEpandedTriple<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq
{
    fn from(t: PTriple<A>) -> Self {
        let t:PMultiTriple<A> = t.into();
        t.into()
    }
}

impl<A> From<PMultiTriple<A>> for PEpandedTriple<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq
{
    fn from(t: PMultiTriple<A>) -> Self {
        PEpandedTriple::PMultiTriple(t)
    }
}

impl<A> From<PTripleSeq<A>> for PEpandedTriple<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq
{
    fn from(t: PTripleSeq<A>) -> Self {
        PEpandedTriple::PTripleSeq(t)
    }
}

impl<A> TripleLike<A> for PEpandedTriple<A>
where A: AsRef<str> + Clone + Debug + Eq + PartialEq {
    fn accept(&mut self, triple: PTriple<A>) -> Option<PTriple<A>> {
        match self {
            Self::PMultiTriple(mt) => mt.accept(triple),
            Self::PTripleSeq(seq) => seq.accept(triple),
        }
    }

    fn subject(&self) -> &PNamedOrBlankNode<A> {
        match self {
            Self::PMultiTriple(mt) => mt.subject(),
            Self::PTripleSeq(seq) => seq.subject(),
        }
    }

    fn literal_objects(&self) -> Vec<&PTriple<A>> {
        match self {
            Self::PMultiTriple(mt) => mt.literal_objects(),
            Self::PTripleSeq(seq) => seq.literal_objects(),
        }
    }

    fn find_typed(&self) -> Option<&PTriple<A>> {
        match self {
            Self::PMultiTriple(mt) => mt.find_typed(),
            Self::PTripleSeq(seq) => seq.find_typed(),
        }
    }
}



/// A chunk of RDF that should that should be coherent.
/// Current invariants:
///   - each subject should appear only once (i.e. all subjects are
///   grouped in PMultiTriple, or AsRefSeq)
///   - if a subject appears it represents all appearances of the node
///   as a subject in the document of which this is a chunk
///   - if BNodes appear as subjects, they appear after any
///   apperance as an object (TODO: Not implemented yet!)
#[derive(Debug)]
pub struct PChunk<A:AsRef<str>>{
    v: VecDeque<PEpandedTriple<A>>,
    r: HashSet<PEpandedTriple<A>>,
    by_sub: HashMap<PBlankNode<A>, PEpandedTriple<A>>
}


impl<A> PChunk<A>
where A: AsRef<str> + Clone + Debug + Eq + Hash + PartialEq
{
    pub fn normalize(v:Vec<PTriple<A>>) -> Self {
        let mut etv:IndexMap<PNamedOrBlankNode<A>, PMultiTriple<A>> = Default::default();
        let mut seq:Vec<PTripleSeq<A>> = vec![];
        let mut seq_rest:HashMap<PNamedOrBlankNode<A>, PTriple<A>> = Default::default();
        let mut seq_first:HashMap<PNamedOrBlankNode<A>, PTriple<A>> = Default::default();

        'top: for t in v {

            // We have a collection add. Create a new seq and store it
            if t.is_collection_end() {
                seq.push(PTripleSeq::from_end(t));
                continue 'top;
            }

            // We have a collection part. Remember for later
            if t.is_collection_rest() {
                if let PTerm::BlankNode(ref bn) = &t.object {
                    seq_rest.insert(PNamedOrBlankNode::BlankNode(bn.clone()), t);
                }
                continue 'top;
            }
            if t.is_collection_first() {
                seq_first.insert(t.subject.clone(), t);
                continue 'top;
            }

            // We have something else. Combine it with existing multi
            // triples
            if let Some(multi) = etv.get_mut(&t.subject) {
                multi.accept(t);
            } else {
                // We have an orphan triple, store it a new multi
                etv.insert(t.subject.clone(), t.into());
            }
        }

        for s in seq.iter_mut() {
            loop {
                if let Some(t) = seq_first.remove(s.subject()) {
                    s.accept(t);
                }

                if let Some(t) = seq_rest.remove(s.subject()) {
                    s.accept(t);
                } else {
                    break;
                }
            }
        }

        let etv:VecDeque<PEpandedTriple<A>> = etv.into_iter()
            .map(|(_k, v)| v.into())
            .chain(
                seq.into_iter()
                    .map(|s| PEpandedTriple::PTripleSeq(s))
            )
            .collect();

        PChunk{v:etv, r:HashSet::new(), by_sub:HashMap::new()}
    }

    pub fn from_raw(vec:Vec<PEpandedTriple<A>>) -> Self {
        PChunk{v:vec.into(), r:HashSet::new(), by_sub:HashMap::new()}
    }

    pub fn empty() -> Self {
        PChunk{v:vec![].into(), r:HashSet::new(), by_sub:HashMap::new()}
    }

    pub fn sort(&mut self) {
        &self.v.make_contiguous()
            .sort_by(
                |a, b| {
                    match (a, b) {
                        (
                            PEpandedTriple::PMultiTriple(_),
                            PEpandedTriple::PTripleSeq(_)
                        ) => Ordering::Less,
                        (
                            PEpandedTriple::PTripleSeq(_),
                            PEpandedTriple::PMultiTriple(_)
                        ) => Ordering::Greater,
                        (
                            PEpandedTriple::PMultiTriple(
                                amt
                            ),
                            PEpandedTriple::PMultiTriple(
                                bmt
                            )
                        ) => {
                            match (
                                amt.subject(), bmt.subject()
                            ) {
                                (
                                    PNamedOrBlankNode::NamedNode(_),
                                    PNamedOrBlankNode::BlankNode(_),
                                ) => Ordering::Less,
                                (
                                    PNamedOrBlankNode::BlankNode(_),
                                    PNamedOrBlankNode::NamedNode(_),
                                ) => Ordering::Greater,
                                _ => Ordering::Equal
                            }
                        }
                        _ => Ordering::Equal
                    }
                }
            );
    }

    pub fn insert(&mut self, et:PEpandedTriple<A>){
        self.v.push_back(et);
        self.by_sub.clear()
    }

    pub fn next(&mut self) -> Option<PEpandedTriple<A>> {
        loop {
            if let Some(front) = self.v.pop_front() {
                if !self.r.contains(&front) {
                    return Some(front)
                }
            } else {
                return None;
            }
        }
    }

    fn remove_et(&mut self, et: &PEpandedTriple<A>) {
        self.r.insert(et.clone());
    }

    fn find_subject(&mut self, bn:&PBlankNode<A>) -> Option<PEpandedTriple<A>> {
        if self.by_sub.is_empty() {
            for v in self.v.iter() {
                if let PNamedOrBlankNode::BlankNode(n) = v.subject() {
                    &self.by_sub.insert(n.clone(), v.clone());
                }
            }
        }
        if let Some(sub) = self.by_sub.get(bn).cloned() {
            if !self.r.contains(&sub) {
                return Some(sub);
            }
        }
        None
    }
}


#[derive(Clone, Debug, Default)]
pub struct ChunkedRdfXmlFormatterConfig {
    indent: usize,
    prefix: IndexMap<String, String>,
}

impl ChunkedRdfXmlFormatterConfig {
    pub fn none() -> Self {
        ChunkedRdfXmlFormatterConfig {
            indent: 0,
            prefix: IndexMap::new(),
        }
    }
    pub fn all() -> Self {
        ChunkedRdfXmlFormatterConfig {
            indent:4,
            prefix:IndexMap::new(),
        }
    }

    pub fn prefix(mut self, indexmap:IndexMap<String, String>) -> Self {
        self.prefix = indexmap;
        self
    }

    pub fn indent(mut self, indent:usize) -> Self{
        self.indent = indent;
        self
    }
}

pub struct ChunkedRdfXmlFormatter<A:AsRef<str>, W: Write> {
    writer: Writer<W>,
    config: ChunkedRdfXmlFormatterConfig,
    pub (crate) open_tag_stack: Vec<Vec<u8>>,
    last_open_tag: Option<BytesStart<'static>>,
    chunk: PChunk<A>,
}

impl<A, W> ChunkedRdfXmlFormatter<A, W>
where A: AsRef<str> + Clone + Debug + Eq + Hash + PartialEq,
      W: Write,
{
    pub fn new(write: W, mut config: ChunkedRdfXmlFormatterConfig) -> Result<Self, io::Error> {

        config.prefix.insert("http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
                             "rdf".to_string());

        Self {
            writer: Writer::new_with_indent(write, b' ', config.indent),
            config,
            open_tag_stack: Default::default(),
            last_open_tag: None,
            chunk: PChunk::empty(),
        }
        .write_declaration()
    }

    fn write_declaration(mut self) -> Result<Self, io::Error> {
         self.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"UTF-8"), None)))
            .map_err(map_err)?;
        let mut rdf_open = BytesStart::borrowed_name(b"rdf:RDF");
        self.write_prefix(&mut rdf_open)?;
        self.write_event(Event::Start(rdf_open))
            .map_err(map_err)?;
        Ok(self)
    }

    fn write_prefix(&mut self, rdf_open: &mut BytesStart<'_>) -> Result<(), io::Error> {
        for i in &self.config.prefix {
            let ns = format!("xmlns:{}", &i.1);
            rdf_open.push_attribute((&ns[..],
                                     &i.0[..]));
        }

        Ok(())
    }

    fn write_complete_open(&mut self) -> Result<(), quick_xml::Error> {
        if let Some(bs) = self.last_open_tag.take() {
            self.writer.write_event(Event::Start(bs))?;
        }
        self.last_open_tag = None;
        Ok(())
    }

    // Write a single event here.
    fn write_event(&mut self, event: Event<'_>) -> Result<(), quick_xml::Error> {
        self.write_complete_open()?;

        // If this is a start event, capture it, and hold it till the
        // next event. If the next event is a cognate close, send a Empty.
        self.writer.write_event(event)
    }

    fn write_start(&mut self, event: Event<'_>) -> Result<(), quick_xml::Error> {
        self.write_complete_open()?;
        match event {
            Event::Start(bs) => {
                self.open_tag_stack.push(bs.name().to_vec());
                self.last_open_tag = Some(bs.to_owned());
            }
            _ => panic!("Only pass a start event to write start"),
        }
        Ok(())
    }

    fn write_close(&mut self) -> Result<(), io::Error> {
        let close = self.open_tag_stack.pop().ok_or(
            io::Error::new(io::ErrorKind::Other, "close when no close is available")
        ).unwrap();

        //  println!("\nwrite_close:");
        if let Some(empty) = self.last_open_tag.take() {
            self.write_event(Event::Empty(empty)).map_err(map_err)
        } else {
            self.write_event(Event::End(BytesEnd::owned(close))).map_err(map_err)
        }
    }

    fn bytes_start_iri<'a>(&mut self, nn:&'a PNamedNode<A>) -> BytesStart<'a> {
        let (iri_protocol_and_host, iri_qname) = nn.split_iri();
        if let Some(iri_ns_prefix) = &self.config.prefix.get(iri_protocol_and_host) {
            BytesStart::owned_name(
                format!("{}:{}", &iri_ns_prefix, &iri_qname)
            )
        } else {
            let mut bs = BytesStart::owned_name(iri_qname.as_bytes());
            bs.push_attribute(("xmlns", iri_protocol_and_host));
            bs
        }
    }

    fn format_head<'a, T:TripleLike<A> + Debug>(&mut self, triple_like:&'a T, _chunk:&PChunk<A>)
                                        -> Result<Vec<&'a PTriple<A>>, io::Error> {
        let mut triples_rendered = vec![];
        let mut description_open =
            match triple_like.find_typed() {
                Some(t) => {
                    if let PTerm::NamedNode(nn) = &t.object {
                        triples_rendered.push(t);
                        self.bytes_start_iri(nn)
                    } else {
                        panic!("BNodes cannot be typed, I think")
                    }
                },
                None => {
                    BytesStart::borrowed_name(b"rdf:Description")
                }
            };

        match triple_like.subject() {
            PNamedOrBlankNode::NamedNode(ref n) => {
                description_open.push_attribute(("rdf:about", n.iri.as_ref()))
            }
            PNamedOrBlankNode::BlankNode(_) => {
                // Empty
            }
        }

        // TODO: Shares lots of code with format_property
        // TODO: check all properties unique!!
        for literal_t in triple_like.literal_objects() {
            if let PTerm::Literal(l) = &literal_t.object {
                match l {
                    PLiteral::Simple {value} => {
                        let (iri_protocol_and_host, iri_qname) = literal_t.predicate.split_iri();

                        if let Some(iri_ns_prefix) = &self.config.prefix.get(iri_protocol_and_host) {
                            description_open.push_attribute(
                                (
                                    &format!("{}:{}", &iri_ns_prefix, &iri_qname)[..],
                                    value.as_ref()
                                )
                            );
                            triples_rendered.push(literal_t);
                        }
                    }
                    PLiteral::LanguageTaggedString {value:_, language:_} => {
                        // Don't do anything here, because the
                        // language environment is wrong. Render later.
                    }
                    PLiteral::Typed {value:_, datatype:_} => {
                        // Don't do anything here because we need to
                        // render later.
                    }
                }
            } else {
                debug_assert!(false, "Non literal object returned from literal object method");
            }
        }
        self.write_start(Event::Start(description_open))
            .map_err(map_err)?;

        Ok(triples_rendered)
    }

    fn format_object(&mut self,
                     mut property_open:BytesStart<'_>,
                     object:&PTerm<A>, chunk:&mut PChunk<A>,
                     collection: bool
    )
                     -> Result<(), io::Error> {
        match object {
            PTerm::NamedNode(n) => {
                // Rewrite: 2.4 Empty Property Elements
                if collection {
                    property_open.push_attribute(("rdf:about", n.iri.as_ref()));
                } else {
                    property_open.push_attribute(("rdf:resource", n.iri.as_ref()));
                }

                self.write_start(Event::Start(property_open))
                    .map_err(map_err)?;
            }
            PTerm::BlankNode(n) => {
                if let Some(t) = chunk.find_subject(n) {
                    if let PEpandedTriple::PTripleSeq(ref seq) = t {
                        if !seq.has_literal() {
                            property_open.push_attribute(("rdf:parseType", "Collection"));
                        }
                    }
                    self.write_start(Event::Start(property_open))
                        .map_err(map_err)?;
                    self.format_expanded(&t, chunk)?;
                } else {
                    property_open.push_attribute(("rdf:nodeID", n.id.as_ref()));
                    self.write_start(Event::Start(property_open))
                        .map_err(map_err)?;
                }
            }
            PTerm::Literal(l) => {
                let content =
                    match l {
                        PLiteral::Simple { value } => {
                            property_open.push_attribute(("rdf:datatype",
                                                          "http://www.w3.org/2001/XMLSchema#string"
                            ));
                            value
                        }
                        PLiteral::LanguageTaggedString { value, language } => {
                            property_open.push_attribute(("xml:lang", language.as_ref()));
                            value
                        }
                        PLiteral::Typed { value, datatype } => {
                            property_open.push_attribute(("rdf:datatype", datatype.iri.as_ref()));
                            value
                        }
                    };
                self.write_start(Event::Start(property_open))
                    .map_err(map_err)?;
                self.write_event(Event::Text(BytesText::from_plain_str(&content.as_ref())))
                    .map_err(map_err)?;
            },
        };

        Ok(())
    }

    fn format_property_arc(&mut self, triple: &PTriple<A>,
                           rendered_in_head:&Vec<&PTriple<A>>,
                           chunk:&mut PChunk<A>,
    ) -> Result<(), io::Error> {
        if rendered_in_head.contains(&triple) {
            return Ok(())
        }

        let property_open = self.bytes_start_iri(&triple.predicate);
        self.format_object(property_open, &triple.object, chunk, false)?;

        self.write_close()?;
        Ok(())
    }

    fn format_seq(&mut self, seq: &PTripleSeq<A>, chunk:&mut PChunk<A>)
                  -> Result<(), io::Error> {

        // We can't format seqs with literals in like this -- we need
        // to do long hand
        if seq.has_literal() {
            let subj = seq.subject().clone();
            let v:Vec<PMultiTriple<A>> = seq.clone().into();
            for i in v {
                chunk.insert(i.into())
            }
            if let PNamedOrBlankNode::BlankNode(n) = subj {
                return self.format_expanded(&chunk.find_subject(&n).unwrap(), chunk);
            }
            todo!("We shouldn't get here");
        }


        for tup in seq.list_seq.iter() {
            if let Some(ref triple) = tup.1 {
                match &triple.object {
                    // Just render in place
                    PTerm::BlankNode(bn) => {
                        if let Some(t) = chunk.find_subject(bn) {
                            self.format_expanded(&t, chunk)?;
                        }
                    }
                    // render the object, but not the property which
                    // is the collection joiner
                    PTerm::NamedNode(_) => {
                        let property_open = BytesStart::borrowed_name(b"rdf:Description");
                        self.format_object(property_open, &triple.object, chunk, true)?;
                        self.write_close()?;
                    }
                    _ => todo!()
                }
            }
        }

        Ok(())
    }

    fn format_multi(&mut self, multi_triple: &PMultiTriple<A>,
                    chunk:&mut PChunk<A>,
    ) -> Result<(), io::Error> {
        let rendered_in_head = self.format_head(multi_triple, chunk)?;

        // Rewrite: 2.3 Multiple Property Elements
        for triple in multi_triple.vec.iter() {
            self.format_property_arc(triple, &rendered_in_head, chunk)?;
        }

        self.write_close()?;
        Ok(())
    }

    fn format_expanded(&mut self, expanded:&PEpandedTriple<A>,
                       chunk:&mut PChunk<A>,
    ) -> Result<(), io::Error> {
        chunk.remove_et(expanded);

        match expanded {
            PEpandedTriple::PMultiTriple(ref mt) => {
                self.format_multi(mt, chunk)?;
            }
            PEpandedTriple::PTripleSeq(ref seq) =>{
                self.format_seq(seq, chunk)?;
            }
        }

        Ok(())
    }

    pub fn chunk_seq(&mut self, seq:PTripleSeq<A>) {
        self.chunk.insert(seq.into())
    }

    pub fn chunk_triple(&mut self, triple:PTriple<A>) {
        self.chunk.insert(triple.into());
    }

    pub fn chunk_multi(&mut self, multi: PMultiTriple<A>) {
        self.chunk.insert(multi.into())
    }

    pub fn sort_chunk(&mut self) {
        self.chunk.sort()
    }

    pub fn finish_chunk(&mut self) -> Result<(), io::Error> {
        let mut chk = PChunk::empty();
        std::mem::swap(&mut self.chunk, &mut chk);
        self.format_chunk(chk)
    }

    pub fn format_chunk(&mut self, mut chunk:PChunk<A>) -> Result<(), io::Error> {
        loop {
            let optet = chunk.next();
            if let Some(et) = optet {
                self.format_expanded(&et, &mut chunk)?;
            } else {
                break;
            }
        }
        Ok(())
    }

    /// Finishes writing and returns the underlying `Write`
    pub fn finish(mut self) -> Result<W, io::Error> {
        while !self.open_tag_stack.is_empty() {
            self.write_close()?;
        }

        self.finish_chunk()?;

        self.write_event(Event::End(BytesEnd::borrowed(b"rdf:RDF")))
            .map_err(map_err)?;

        Ok(self.writer.into_inner())
    }
}


pub struct PrettyRdfXmlFormatter<A:AsRef<str>+Debug, W: Write> (
    ChunkedRdfXmlFormatter<A, W>,
    pub Vec<PTriple<A>>
);

impl<A, W> PrettyRdfXmlFormatter<A, W>
where A: AsRef<str> + Clone + Debug + Eq + Hash + PartialEq,
      W: Write,
{
    pub fn new(write: W, config: ChunkedRdfXmlFormatterConfig) -> Result<Self, io::Error> {
        Ok(
            PrettyRdfXmlFormatter(
                ChunkedRdfXmlFormatter::new(write, config)?,
                vec![]
            )
        )
    }

    pub fn format(&mut self, triple:PTriple<A>) -> Result<(), io::Error> {
        &self.1.push(triple);
        Ok(())
    }

    pub fn triples(&self) -> Vec<PTriple<A>> {
        self.1.clone()
    }

    pub fn finish(mut self) -> Result<W, io::Error> {
        let chk = PChunk::normalize(self.1);
        self.0.format_chunk(chk)?;
        self.0.finish()
    }
}





#[cfg(test)]
mod test {
    use indexmap::{IndexMap, indexmap};
    use pretty_assertions::assert_eq;
    use rio_api::parser::TriplesParser;
    use rio_turtle::TurtleError;

    use super::{PChunk, PBlankNode, PNamedNode, PTriple, PTripleSeq,
                ChunkedRdfXmlFormatter, ChunkedRdfXmlFormatterConfig};

    fn tnn () -> PTriple<String> {
        PTriple {
            subject: PNamedNode::new("http://example.com/s".to_string()).into(),
            predicate: PNamedNode::new("http://example.com/p".to_string()).into(),
            object: PNamedNode::new("http://example.com/o".to_string()).into()
        }
    }

    fn tnn1 () -> PTriple<String> {
        PTriple {
            subject: PNamedNode::new("http://example.com/s1".to_string()).into(),
            predicate: PNamedNode::new("http://example.com/p1".to_string()).into(),
            object: PNamedNode::new("http://example.com/o1".to_string()).into()
        }
    }

    fn bnn () -> PTriple<String> {
        PTriple {
            subject: PBlankNode::new("hello_id".to_string()).into(),
            predicate: PNamedNode::new("http://example.com/p".to_string()).into(),
            object: PNamedNode::new("http://example.com/o".to_string()).into()
        }
    }

    #[test]
    pub fn chunk_hello_world() {
        assert!(true)
    }

    #[test]
    pub fn simple_chunk() {
        let chk = PChunk::normalize(
            vec![
                tnn(),
            ]
        );

        assert_eq!(chk.v.len(), 1);
    }

    #[test]
    pub fn multi_chunk() {
        let chk = PChunk::normalize(
            vec![
                tnn(),
                tnn(),
                tnn(),
            ]
        );

        assert_eq!(chk.v.len(), 1);
    }

    #[test]
    pub fn multi_chunk_sort_stable() {
        let mut chk:PChunk<String>=PChunk::empty();
        chk.insert(tnn().into());
        chk.insert(tnn1().into());
        chk.sort();

        assert_eq!(chk.next(), Some(tnn().into()));
        assert_eq!(chk.next(), Some(tnn1().into()));

        let mut chk:PChunk<String>=PChunk::empty();
        chk.insert(tnn1().into());
        chk.insert(tnn().into());
        chk.sort();

        assert_eq!(chk.next(), Some(tnn1().into()));
        assert_eq!(chk.next(), Some(tnn().into()));
    }

    #[test]
    pub fn multi_chunk_sort() {
        let mut chk:PChunk<String>=PChunk::empty();

        chk.insert(PTripleSeq::empty().into());
        chk.insert(bnn().into());
        chk.insert(tnn().into());

        chk.sort();

        assert_eq!(chk.next(), Some(tnn().into()));
        assert_eq!(chk.next(), Some(bnn().into()));
        assert_eq!(chk.next(), Some(PTripleSeq::empty().into()));
    }

    fn spec_prefix() -> IndexMap<&'static str, &'static str> {
        indexmap![
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf",
            "http://purl.org/dc/elements/1.1/" => "dc",
            "http://example.org/stuff/1.0/" => "ex"
        ]
    }

    #[allow(dead_code)]
    fn from_nt(nt: &str) -> String {
        from_nt_prefix(nt, indexmap!("http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf"))
    }

    fn from_nt_prefix(nt: &str, prefix: IndexMap<&str, &str>) -> String {
        let mut source: Vec<PTriple<String>> = vec![];
        let _: Vec<Result<(), TurtleError>>
            = rio_turtle::NTriplesParser::new(nt.as_bytes()).into_iter(
                |rio_triple| {
                    source.push(rio_triple.into());
                    Ok(())
                }
            ).collect();


        let sink = vec![];

        let mut config = ChunkedRdfXmlFormatterConfig::all();
        config.prefix =
            prefix.into_iter().map(
                |(k, v)|
                (k.to_string(), v.to_string())
            ).collect();

        let mut f = ChunkedRdfXmlFormatter::new(sink,config).unwrap();
        let chk = PChunk::normalize(source);
        //dbg!(&chk);
        f.format_chunk(chk).unwrap();

        let w = f.finish().unwrap();
        let s = String::from_utf8(w).unwrap();
        println!("{}", s);
        s
    }

    fn nt_xml_roundtrip_prefix(nt: &str, xml: &str, prefix: IndexMap<&str, &str>){
        assert_eq!(
            from_nt_prefix(nt, prefix), xml
        );
    }

    #[allow(dead_code)]
    fn nt_xml_roundtrip(nt: &str, xml: &str) {
        assert_eq!(
            from_nt(nt), xml
        );
    }

    #[test]
    fn example4_single_triple() {
        nt_xml_roundtrip_prefix(
r###"<http://www.w3.org/TR/rdf-syntax-grammar> <http://purl.org/dc/elements/1.1/title> "RDF1.1 XML Syntax" .
"### ,
r###"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ex="http://example.org/stuff/1.0/">
    <rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar" dc:title="RDF1.1 XML Syntax"/>
</rdf:RDF>"### ,
            spec_prefix()
        )
    }

    #[test]
    fn example4_multiple_property_elements(){
        nt_xml_roundtrip_prefix(
r###"<http://www.w3.org/TR/rdf-syntax-grammar> <http://purl.org/dc/elements/1.1/title> "RDF1.1 XML Syntax" .
<http://www.w3.org/TR/rdf-syntax-grammar> <http://example.org/stuff/1.0/editor> _:genid1 .
_:genid1 <http://example.org/stuff/1.0/fullName> "Dave Beckett" .
_:genid1 <http://example.org/stuff/1.0/homePage> <http://purl.org/net/dajobe/> ."### ,

r###"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ex="http://example.org/stuff/1.0/">
    <rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar" dc:title="RDF1.1 XML Syntax">
        <ex:editor>
            <rdf:Description ex:fullName="Dave Beckett">
                <ex:homePage rdf:resource="http://purl.org/net/dajobe/"/>
            </rdf:Description>
        </ex:editor>
    </rdf:Description>
</rdf:RDF>"### ,
            spec_prefix()
        );
    }

    #[test]
    fn example14_typed_nodes() {
        nt_xml_roundtrip_prefix(
r###"<http://example.org/thing> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/stuff/1.0/Document> .
<http://example.org/thing> <http://purl.org/dc/elements/1.1/title> "A marvelous thing" ."### ,

r###"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ex="http://example.org/stuff/1.0/">
    <ex:Document rdf:about="http://example.org/thing" dc:title="A marvelous thing"/>
</rdf:RDF>"###  ,
            spec_prefix()
        )
    }

    #[test]
    fn example19_collections() {
        nt_xml_roundtrip_prefix(
r###"_:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.org/banana> .
_:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.org/apple> .
_:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid2 .
_:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.org/pear> .
_:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:genid3 .
_:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
<http://example.org/basket> <http://example.org/stuff/1.0/hasFruit> _:genid1 ."### ,

r###"<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ex="http://example.org/stuff/1.0/">
    <rdf:Description rdf:about="http://example.org/basket">
        <ex:hasFruit rdf:parseType="Collection">
            <rdf:Description rdf:about="http://example.org/banana"/>
            <rdf:Description rdf:about="http://example.org/apple"/>
            <rdf:Description rdf:about="http://example.org/pear"/>
        </ex:hasFruit>
    </rdf:Description>
</rdf:RDF>"### ,
            spec_prefix()
        )
    }
}