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
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
//! This module provides the encoding buffer.
//!
//! The encoding buffer is the buffer header implementations
//! write there data to. It provides a view special aspects
//! to make it more robust.
//!
//! For example it handles the writing of trailing newlines for headers
//! (if they don't do it) and it fails if you write utf-8 data
//! (in the header) to an buffer which knows that the used mail
//! type doesn't support it.
//!
//! There is also a special `tracing` (cargo) feature which
//! will make it's usage slower, but which will keep track of
//! what data was inserted in which way making debugging and
//! writing tests easier. (Through it should _only_ be enabled
//! for testing and maybe debugging in some cases).
use std::borrow::Cow;
use std::str;

use failure::Fail;
use soft_ascii_string::{SoftAsciiStr, SoftAsciiChar};

use grammar::is_atext;
use ::utils::{
    is_utf8_continuation_byte,
    vec_insert_bytes
};
use ::MailType;
use ::error::{
    EncodingError, EncodingErrorKind,
    UNKNOWN, UTF_8, US_ASCII
};

#[cfg(feature="traceing")]
#[cfg_attr(test, macro_use)]
mod trace;
#[cfg_attr(test, macro_use)]
mod encodable;


#[cfg(feature="traceing")]
pub use self::trace::*;
pub use self::encodable::*;

/// as specified in RFC 5322 not including CRLF
pub const LINE_LEN_SOFT_LIMIT: usize = 78;
/// as specified in RFC 5322 (mail) + RFC 5321 (smtp) not including CRLF
pub const LINE_LEN_HARD_LIMIT: usize = 998;

pub const NEWLINE: &str = "\r\n";
pub const NEWLINE_WITH_SPACE: &str = "\r\n ";


/// EncodingBuffer for a Mail providing a buffer for encodable traits.
pub struct EncodingBuffer {
    mail_type: MailType,
    buffer: Vec<u8>,
    #[cfg(feature="traceing")]
    pub trace: Vec<TraceToken>
}

impl EncodingBuffer {

    /// Create a new buffer only allowing input compatible with a the specified mail type.
    pub fn new(mail_type: MailType) -> Self {
        EncodingBuffer {
            mail_type,
            buffer: Vec::new(),
            #[cfg(feature="traceing")]
            trace: Vec::new()
        }
    }

    /// Returns the mail type for which the buffer was created.
    pub fn mail_type( &self ) -> MailType {
        self.mail_type
    }

    /// returns a new EncodingWriter which contains
    /// a mutable reference to the current string buffer
    ///
    pub fn writer(&mut self) -> EncodingWriter {
        #[cfg(not(feature="traceing"))]
        {
            EncodingWriter::new(self.mail_type, &mut self.buffer)
        }
        #[cfg(feature="traceing")]
        {
            EncodingWriter::new(self.mail_type, &mut self.buffer, &mut self.trace)
        }
    }

    /// calls the provided function with a EncodingWriter cleaning up afterwards
    ///
    /// After calling `func` with the EncodingWriter following cleanup is performed:
    /// - if `func` returned an error `handle.undo_header()` is called, this won't
    ///   undo anything before a `finish_header()` call but will discard partial
    ///   writes
    /// - if `func` succeeded `handle.finish_header()` is called
    pub fn write_header_line<FN>(&mut self, func: FN) -> Result<(), EncodingError>
        where FN: FnOnce(&mut EncodingWriter) -> Result<(), EncodingError>
    {
        let mut handle  = self.writer();
        match func(&mut handle) {
            Ok(()) => {
                handle.finish_header();
                Ok(())
            },
            Err(e) => {
                handle.undo_header();
                Err(e)
            }
        }

    }

    pub fn write_blank_line(&mut self) {
        //TODO/BENCH push_str vs. extends(&[u8])
        self.buffer.extend(NEWLINE.as_bytes());
        #[cfg(feature="traceing")]
        { self.trace.push(TraceToken::BlankLine); }
    }

    /// writes a body to the internal buffer, without verifying it's correctness
    pub fn write_body_unchecked(&mut self, body: &impl AsRef<[u8]>) {
        let slice = body.as_ref();
        self.buffer.extend(slice);
        if !slice.ends_with(NEWLINE.as_bytes()) {
            self.buffer.extend(NEWLINE.as_bytes());
        }
    }

    //TODO impl. a alt. `write_body(body,  boundaries)` which:
    // - checks the body (us-ascii or mime8bit/internationalized)
    // - checks for orphan '\r'/'\n' and 0 bytes
    // - check that no string in boundaries appears in the text
    //   - this probably requires creating a regex for each body
    //     through as boundaries are "fixed" there might be an more
    //     efficient algorithm then a regex (i.e. using tries)

    /// # Error
    ///
    /// This can fail if a body does not contain valid utf8.
    pub fn as_str(&self) -> Result<&str, EncodingError> {
        str::from_utf8(self.buffer.as_slice())
            .map_err(|err| {
                EncodingError::from((
                    err.context(EncodingErrorKind::InvalidTextEncoding {
                        expected_encoding: UTF_8,
                        got_encoding: UNKNOWN
                    }),
                    self.mail_type()
                ))
            })
    }

    /// Converts the internal buffer into an utf-8 string if possible.
    pub fn to_string(&self) -> Result<String, EncodingError> {
        Ok(self.as_str()?.to_owned())
    }

    /// Lossy conversion of the internal buffer to an string.
    pub fn to_string_lossy(&self) -> Cow<str> {
        String::from_utf8_lossy(self.buffer.as_slice())
    }

    /// Return a slice view to the underlying buffer.
    pub fn as_slice(&self) -> &[u8] {
        &self.buffer
    }

}


impl Into<Vec<u8>> for EncodingBuffer {
    fn into(self) -> Vec<u8> {
        self.buffer
    }
}

impl Into<(MailType, Vec<u8>)> for EncodingBuffer {
    fn into(self) -> (MailType, Vec<u8>) {
        (self.mail_type, self.buffer)
    }
}

#[cfg(feature="traceing")]
impl Into<(MailType, Vec<u8>, Vec<TraceToken>)> for EncodingBuffer {
    fn into(self) -> (MailType, Vec<u8>, Vec<TraceToken>) {
        let EncodingBuffer { mail_type, buffer, trace } = self;
        (mail_type, buffer, trace)
    }
}

/// A handle providing method to write to the underlying buffer
/// keeping track of newlines the current line length and places
/// where the line can be broken so that the soft line length
/// limit (78) and the hard length limit (998) can be kept.
///
/// It's basically a string buffer which know how to brake
/// lines at the right place.
///
/// Note any act of writing a header through `EncodingWriter`
/// has to be concluded by either calling `finish_header` or `undo_header`.
/// If not this handle will panic in _test_ builds when being dropped
/// (and the thread is not already panicing) as writes through the handle are directly
/// writes to the underlying buffer which now contains malformed/incomplete
/// data. (Note that this Handle does not own any Drop types so if
/// needed `forget`-ing it won't leak any memory)
///
///
pub struct EncodingWriter<'a> {
    buffer: &'a mut Vec<u8>,
    #[cfg(feature="traceing")]
    trace: &'a mut Vec<TraceToken>,
    mail_type: MailType,
    line_start_idx: usize,
    last_fws_idx: usize,
    skipped_cr: bool,
    /// if there had ben non WS chars since the last FWS
    /// or last line start, if there had been a line
    /// start since the last fws.
    content_since_fws: bool,
    /// represents if there had ben non WS chars before the last FWS
    /// on the current line (false if there was no FWS yet on the current
    /// line).
    content_before_fws: bool,
    /// represents if if a FWS was just marked (opt-FWS) or was written out
    last_fws_has_char: bool,
    header_start_idx: usize,
    #[cfg(feature="traceing")]
    trace_start_idx: usize
}

#[cfg(feature="traceing")]
impl<'a> Drop for EncodingWriter<'a> {

    fn drop(&mut self) {
        use std::thread;
        if !thread::panicking() &&  self.has_unfinished_parts() {
            // we really should panic as the back buffer i.e. the mail will contain
            // some partially written header which definitely is a bug
            panic!("dropped Handle which partially wrote header to back buffer (use `finish_header` or `discard`)")
        }
    }
}

impl<'inner> EncodingWriter<'inner> {

    #[cfg(not(feature="traceing"))]
    fn new(
        mail_type: MailType,
        buffer: &'inner mut Vec<u8>,
    ) -> Self {
        let start_idx = buffer.len();
        EncodingWriter {
            buffer,
            mail_type,
            line_start_idx: start_idx,
            last_fws_idx: start_idx,
            skipped_cr: false,
            content_since_fws: false,
            content_before_fws: false,
            header_start_idx: start_idx,
            last_fws_has_char: false,
        }
    }

    #[cfg(feature="traceing")]
    fn new(
        mail_type: MailType,
        buffer: &'inner mut Vec<u8>,
        trace: &'inner mut Vec<TraceToken>
    ) -> Self {
        let start_idx = buffer.len();
        let trace_start_idx = trace.len();
        EncodingWriter {
            buffer,
            trace,
            mail_type,
            line_start_idx: start_idx,
            last_fws_idx: start_idx,
            skipped_cr: false,
            content_since_fws: false,
            content_before_fws: false,
            header_start_idx: start_idx,
            last_fws_has_char: false,
            trace_start_idx
        }
    }

    fn reinit(&mut self) {
        let start_idx = self.buffer.len();
        self.line_start_idx = start_idx;
        self.last_fws_idx = start_idx;
        self.skipped_cr = false;
        self.content_since_fws = false;
        self.content_before_fws = false;
        self.header_start_idx = start_idx;
        #[cfg(feature="traceing")]
        { self.trace_start_idx = self.trace.len(); }
    }

    /// Returns true if this type thinks we are in the process of writing a header.
    #[inline]
    pub fn has_unfinished_parts(&self) -> bool {
        self.buffer.len() != self.header_start_idx
    }

    /// Returns the associated mail type.
    #[inline]
    pub fn mail_type(&self) -> MailType {
        self.mail_type
    }

    /// Returns true if the current line has content, i.e. any non WS char.
    #[inline]
    pub fn line_has_content(&self) -> bool {
        self.content_before_fws | self.content_since_fws
    }

    /// Returns the length of the current line in bytes.
    #[inline]
    pub fn current_line_byte_length(&self) -> usize {
        self.buffer.len() - self.line_start_idx
    }

    /// marks the current position a a place where a soft
    /// line break (i.e. "\r\n ") can be inserted
    ///
    /// # Trace (test build only)
    /// does push a `MarkFWS` Token
    pub fn mark_fws_pos(&mut self) {
        #[cfg(feature="traceing")]
        { self.trace.push(TraceToken::MarkFWS) }
        self.content_before_fws |= self.content_since_fws;
        self.content_since_fws = false;
        self.last_fws_idx = self.buffer.len();
        self.last_fws_has_char = false;
    }

    /// writes a ascii char to the underlying buffer
    ///
    /// # Error
    /// - fails if the hard line length limit is breached and the
    ///   line can not be broken with soft line breaks
    /// - buffer would contain a orphan '\r' or '\n' after the write
    ///
    /// # Trace (test build only)
    /// does push `NowChar` and then can push `Text`,`CRLF`
    pub fn write_char(&mut self, ch: SoftAsciiChar) -> Result<(), EncodingError>  {
        #[cfg(feature="traceing")]
        { self.trace.push(TraceToken::NowChar) }
        let mut buffer = [0xff_u8; 4];
        let ch: char = ch.into();
        let slice = ch.encode_utf8(&mut buffer);
        self.internal_write_char(slice)
    }

    /// writes a ascii str to the underlying buffer
    ///
    /// # Error
    /// - fails if the hard line length limit is breached and the
    ///   line can not be broken with soft line breaks
    /// - buffer would contain a orphan '\r' or '\n' after the write
    ///
    /// Note that in case of an error part of the content might already
    /// have been written to the buffer, therefore it is recommended
    /// to call `undo_header` after an error (especially if the
    /// handle is doped after this!)
    ///
    /// # Trace (test build only)
    /// does push `NowStr` and then can push `Text`,`CRLF`
    ///
    pub fn write_str(&mut self, s: &SoftAsciiStr)  -> Result<(), EncodingError>  {
        #[cfg(feature="traceing")]
        { self.trace.push(TraceToken::NowStr) }
        self.internal_write_str(s.as_str())
    }


    /// writes a utf8 str into a buffer for an internationalized mail
    ///
    /// # Error (ConditionalWriteResult)
    /// - fails with `ConditionFailure` if the underlying MailType
    ///    is not Internationalized
    /// - fails with `GeneralFailure` if the hard line length limit is reached
    /// - or if the buffer would contain a orphan '\r' or '\n' after the write
    ///
    /// Note that in case of an error part of the content might already
    /// have been written to the buffer, therefore it is recommended
    /// to call `undo_header` after an error (especially if the
    /// handle is droped after this!)
    ///
    /// # Trace (test build only)
    /// does push `NowUtf8` and then can push `Text`,`CRLF`
    pub fn write_if_utf8<'short>(&'short mut self, s: &str)
        -> ConditionalWriteResult<'short, 'inner>
    {
        if self.mail_type().is_internationalized() {
            #[cfg(feature="traceing")]
            { self.trace.push(TraceToken::NowUtf8) }
            self.internal_write_str(s).into()
        } else {
            ConditionalWriteResult::ConditionFailure(self)
        }
    }

    pub fn write_utf8(&mut self, s: &str) -> Result<(), EncodingError> {
        if self.mail_type().is_internationalized() {
            #[cfg(feature="traceing")]
            { self.trace.push(TraceToken::NowUtf8) }
            self.internal_write_str(s)
        } else {
            let mut err = EncodingError::from((
                EncodingErrorKind::InvalidTextEncoding {
                    expected_encoding: US_ASCII,
                    got_encoding: UTF_8
                },
                self.mail_type()
            ));
            let raw_line = &self.buffer[self.line_start_idx..];
            let mut line = String::from_utf8_lossy(raw_line).into_owned();
            line.push_str(s);
            err.set_str_context(line);
            Err(err)
        }
    }

    /// Writes a str assumed to be atext if it is atext given the mail type
    ///
    /// This method is mainly an optimization as the "is atext" and is
    /// "is ascii if MailType is Ascii" aspects are checked at the same
    /// time resulting in a str which you know is ascii _if_ the mail
    /// type is Ascii and which might be non-us-ascii if the mail type
    /// is Internationalized.
    ///
    /// # Error (ConditionalWriteResult)
    /// - fails with `ConditionFailure` if the text is not valid atext,
    ///   this indirectly also includes the utf8/Internationalization check
    ///   as the `atext` grammar differs between normal and internationalized
    ///   mail.
    /// - fails with `GeneralFailure` if the hard line length limit is reached and
    ///   the line can't be broken with soft line breaks
    /// - or if buffer would contain a orphan '\r' or '\n' after the write
    ///   (excluding a tailing `'\r'` as it is still valid if followed by an
    ///    `'\n'`)
    ///
    /// Note that in case of an error part of the content might already
    /// have been written to the buffer, therefore it is recommended
    /// to call `undo_header` after an error (especially if the
    /// handle is doped after this!)
    ///
    /// # Trace (test build only)
    /// does push `NowAText` and then can push `Text`
    ///
    pub fn write_if_atext<'short>(&'short mut self, s: &str)
        -> ConditionalWriteResult<'short, 'inner>
    {
        if s.chars().all( |ch| is_atext( ch, self.mail_type() ) ) {
            #[cfg(feature="traceing")]
            { self.trace.push(TraceToken::NowAText) }
            // the ascii or not aspect is already converted by `is_atext`
            self.internal_write_str(s).into()
        } else {
            ConditionalWriteResult::ConditionFailure(self)
        }
    }

    /// passes the input `s` to the condition evaluation function `cond` and
    /// then writes it _without additional checks_ to the buffer if `cond` returned
    /// true
    ///
    pub fn write_if<'short, FN>(&'short mut self, s: &str, cond: FN)
        -> ConditionalWriteResult<'short, 'inner>
        where FN: FnOnce(&str) -> bool
    {
        if cond(s) {
            #[cfg(feature="traceing")]
            { self.trace.push(TraceToken::NowCondText) }
            // the ascii or not aspect is already converted by `is_atext`
            self.internal_write_str(s).into()
        } else {
            ConditionalWriteResult::ConditionFailure(self)
        }
    }

    /// writes a string to the encoder without checking if it is compatible
    /// with the mail type, if not used correctly this can write Utf8 to
    /// an Ascii Mail, which is incorrect but has to be safe wrt. rust's safety.
    ///
    /// Use it as a replacement for cases similar to following:
    ///
    /// ```ignore
    /// check_if_text_if_valid(text)?;
    /// if mail_type.is_internationalized() {
    ///     handle.write_utf8(text)?;
    /// } else {
    ///     handle.write_str(SoftAsciiStr::from_unchecked(text))?;
    /// }
    /// ```
    ///
    /// ==> instead ==>
    ///
    /// ```ignore
    /// check_if_text_if_valid(text)?;
    /// handle.write_str_unchecked(text)?;
    /// ```
    ///
    /// through is gives a different tracing its roughly equivalent.
    ///
    pub fn write_str_unchecked( &mut self, s: &str) -> Result<(), EncodingError> {
        #[cfg(feature="traceing")]
        { self.trace.push(TraceToken::NowUnchecked) }
        self.internal_write_str(s)
    }

    /// like finish_header, but won't start a new line
    ///
    /// This is meant to be used when _miss-using_ the
    /// writer to write a "think", which is not a full
    /// header. E.g. for testing if a header component
    /// is written correctly. So you _normally_ should
    /// not use it.
    pub fn commit_partial_header(&mut self) {
        #[cfg(feature="traceing")]
        { if let Some(&TraceToken::End) = self.trace.last() {}
            else { self.trace.push(TraceToken::End) } }
        self.reinit();
    }

    /// finishes the writing of a header
    ///
    /// It makes sure the header ends in "\r\n".
    /// If the header ends in a orphan '\r' this
    /// method will just "use" it for the "\r\n".
    ///
    /// If the header ends in a CRLF/start of buffer
    /// followed by only WS (' ' or '\t' ) the valid
    /// header ending is reached by truncating away
    /// the WS padding. This is needed as "blank" lines
    /// are not allowed.
    ///
    /// # Trace (test build only)
    /// - can push 0-1 of `[CRLF, TruncateToCRLF]`
    /// - then does push `End`
    /// - calling `finish_current()` multiple times in a row
    ///   will not generate multiple `End` tokens, just one
    pub fn finish_header(&mut self) {
        self.start_new_line();
        #[cfg(feature="traceing")]
        { if let Some(&TraceToken::End) = self.trace.last() {}
            else { self.trace.push(TraceToken::End) } }
        self.reinit();
    }

    /// undoes all writes to the internal buffer
    /// since the last `finish_header` or `undo_header` or
    /// creation of this handle
    ///
    /// # Trace (test build only)
    /// also removes tokens pushed since the last
    /// `finish_header` or `undo_header` or creation of
    /// this handle
    ///
    pub fn undo_header(&mut self) {
        self.buffer.truncate(self.header_start_idx);
        #[cfg(feature="traceing")]
        { self.trace.truncate(self.trace_start_idx); }
        self.reinit();
    }



    //---------------------------------------------------------------------------------------------/
    //-/////////////////////////// methods only using the public iface   /////////////////////////-/

    /// calls mark_fws_pos and then writes a space
    ///
    /// This method exists for convenience.
    ///
    /// Note that it can not fail a you just pushed
    /// a place to brake the line before writing a space.
    ///
    /// Note that currently soft line breaks will not
    /// collapse whitespace. As such if you use `write_fws`
    /// and then the line is broken at that position it will
    /// start with two spaces (one from `\r\n ` and one which
    /// had been there before).
    pub fn write_fws(&mut self) {
        self.mark_fws_pos();
        self.last_fws_has_char = true;
        // OK: Can not error as we just marked a fws pos.
        let _ = self.write_char(SoftAsciiChar::from_unchecked(' '));
    }



    //---------------------------------------------------------------------------------------------/
    //-///////////////////////////          private methods               ////////////////////////-/

    /// this might partial write some data and then fail.
    /// while we could implement a undo option it makes
    /// little sense for the use case the generally available
    /// `undo_header` is enough.
    fn internal_write_str(&mut self, s: &str)  -> Result<(), EncodingError>  {
        if s.is_empty() {
            return Ok(());
        }
        //TODO I think I wrote a iterator for this somewhere
        let mut start = 0;
        // the first byte is never a continuation byte so we start
        // scanning at the second byte
        for (idx_m1, bch) in s.as_bytes()[1..].iter().enumerate() {
            if !is_utf8_continuation_byte(*bch) {
                // the idx is 1 smaller then it should so add 1
                let end = idx_m1 + 1;
                self.internal_write_char(&s[start..end])?;
                start = end;
            }
        }

        //write last letter
        self.internal_write_char(&s[start..])?;
        Ok(())
    }

    /// if the line has at last one non-WS char a new line
    /// will be started by adding `\r\n` if the current line
    /// only consists of WS then a new line will be started by
    /// removing the blank line (not that WS are only ' ' and '\r')
    fn start_new_line(&mut self) {
        if self.line_has_content() {
            #[cfg(feature="traceing")]
            { self.trace.push(TraceToken::CRLF) }

            self.buffer.push(b'\r');
            self.buffer.push(b'\n');
        } else {
            #[cfg(feature="traceing")]
            {
                if self.buffer.len() > self.line_start_idx {
                    self.trace.push(TraceToken::TruncateToCRLF);
                }
            }
            // e.g. if we "broke" the line on a tailing space => "\r\n  "
            // this would not be valid so we cut awy the trailing white space
            // be if we have "ab  " we do not want to cut away the trailing
            // whitespace but just add "\r\n"
            self.buffer.truncate(self.line_start_idx);
        }
        self.line_start_idx = self.buffer.len();
        self.content_since_fws = false;
        self.content_before_fws = false;
        self.last_fws_idx = self.line_start_idx;

    }

    fn break_line_on_fws(&mut self) -> bool {
        if self.content_before_fws && self.last_fws_idx > self.line_start_idx {
            let newline =
                if self.last_fws_has_char {
                    debug_assert!([b' ', b'\t'].contains(&self.buffer[self.last_fws_idx]));
                    NEWLINE
                } else {
                    NEWLINE_WITH_SPACE
                };

            vec_insert_bytes(&mut self.buffer, self.last_fws_idx, newline.as_bytes());
            self.line_start_idx = self.last_fws_idx + 2;
            self.content_before_fws = false;
            true
        } else {
            false
        }
    }

    /// # Constraints
    ///
    /// `unchecked_utf8_char` is expected to be exactly
    /// one char, which means it's 1-4 bytes in length.
    ///
    /// The reason why a slice is expected instead of a
    /// char is, that this function will at some point push
    /// to a byte buffer requiring a `&[u8]` and many function
    /// calling this function can directly produce a &[u8]/&str.
    ///
    /// # Panic
    ///
    /// Panics if `unchecked_utf8_char` is empty.
    /// If debug assertions are enabled it also panics, if
    /// unchecked_utf8_char is more than just one char.
    fn internal_write_char(&mut self, unchecked_utf8_char: &str) -> Result<(), EncodingError> {
        debug_assert_eq!(unchecked_utf8_char.chars().count(), 1);

        let bch = unchecked_utf8_char.as_bytes()[0];
        if bch == b'\n' {
            if self.skipped_cr {
                self.start_new_line()
            } else {
                ec_bail!(
                    mail_type: self.mail_type(),
                    kind: Malformed
                );
            }
            self.skipped_cr = false;
            return Ok(());
        } else {
            if self.skipped_cr {
                ec_bail!(
                    mail_type: self.mail_type(),
                    kind: Malformed
                );
            }
            if bch == b'\r' {
                self.skipped_cr = true;
                return Ok(());
            } else {
                self.skipped_cr = false;
            }
        }

        if self.current_line_byte_length() >= LINE_LEN_SOFT_LIMIT {
            self.break_line_on_fws();

            if self.current_line_byte_length() >= LINE_LEN_HARD_LIMIT {
                ec_bail!(
                    mail_type: self.mail_type(),
                    kind: HardLineLengthLimitBreached
                );
            }
        }

        self.buffer.extend(unchecked_utf8_char.as_bytes());
        #[cfg(feature="traceing")]
        {
            //FIXME[rust/nll]: just use a `if let`-`else` with NLL's
            let need_new =
                if let Some(&mut TraceToken::Text(ref mut string)) = self.trace.last_mut() {
                    string.push_str(unchecked_utf8_char);
                    false
                } else {
                    true
                };
            if need_new {
                let mut string = String::new();
                string.push_str(unchecked_utf8_char);
                self.trace.push(TraceToken::Text(string))
            }

        }

        // we can't allow "blank" lines
        if bch != b' ' && bch != b'\t' {
            // if there is no fws this is equiv to line_has_content
            // else line_has_content = self.content_before_fws|self.content_since_fws
            self.content_since_fws = true;
        }
        Ok(())
    }
}

pub enum ConditionalWriteResult<'a, 'b: 'a> {
    Ok,
    ConditionFailure(&'a mut EncodingWriter<'b>),
    GeneralFailure(EncodingError)
}

impl<'a, 'b: 'a> From<Result<(), EncodingError>> for ConditionalWriteResult<'a, 'b> {
    fn from(v: Result<(), EncodingError>) -> Self {
        match v {
            Ok(()) => ConditionalWriteResult::Ok,
            Err(e) => ConditionalWriteResult::GeneralFailure(e)
        }
    }
}

impl<'a, 'b: 'a> ConditionalWriteResult<'a, 'b> {

    #[inline]
    pub fn handle_condition_failure<FN>(self, func: FN) -> Result<(), EncodingError>
        where FN: FnOnce(&mut EncodingWriter) -> Result<(), EncodingError>
    {
        use self::ConditionalWriteResult as CWR;

        match self {
            CWR::Ok => Ok(()),
            CWR::ConditionFailure(handle) => {
                func(handle)
            },
            CWR::GeneralFailure(err) => Err(err)
        }
    }
}





#[cfg(test)]
mod test {

    use soft_ascii_string::{ SoftAsciiChar, SoftAsciiStr};
    use ::MailType;
    use ::error::EncodingErrorKind;

    use super::TraceToken::*;
    use super::{EncodingBuffer as _Encoder};

    mod test_test_utilities {
        use encoder::TraceToken::*;
        use super::super::simplify_trace_tokens;

        #[test]
        fn does_simplify_tokens_strip_nows() {
            let inp = vec![
                NowChar,
                Text("h".into()),
                CRLF,
                NowStr,
                Text("y yo".into()),
                CRLF,
                NowUtf8,
                Text(", what's".into()),
                CRLF,
                NowUnchecked,
                Text("up!".into()),
                CRLF,
                NowAText,
                Text("abc".into())
            ];
            let out = simplify_trace_tokens(inp);
            assert_eq!(out, vec![
                Text("h".into()),
                CRLF,
                Text("y yo".into()),
                CRLF,
                Text(", what's".into()),
                CRLF,
                Text("up!".into()),
                CRLF,
                Text("abc".into())
            ])

        }

        #[test]
        fn simplify_does_collapse_text() {
            let inp = vec![
                NowChar,
                Text("h".into()),
                NowStr,
                Text("y yo".into()),
                NowUtf8,
                Text(", what's".into()),
                NowUnchecked,
                Text(" up! ".into()),
                NowAText,
                Text("abc".into())
            ];
            let out = simplify_trace_tokens(inp);
            assert_eq!(out, vec![
                Text("hy yo, what's up! abc".into())
            ]);
        }

        #[test]
        fn simplify_works_with_empty_text() {
            let inp = vec![
                NowStr,
                Text("".into()),
                CRLF,
            ];
            assert_eq!(simplify_trace_tokens(inp), vec![
                Text("".into()),
                CRLF
            ])
        }

        #[test]
        fn simplify_works_with_trailing_empty_text() {
            let inp = vec![
                Text("a".into()),
                CRLF,
                Text("".into()),
            ];
            assert_eq!(simplify_trace_tokens(inp), vec![
                Text("a".into()),
                CRLF,
                Text("".into())
            ])
        }

    }

    mod EncodableInHeader {
        #![allow(non_snake_case)]
        use super::super::*;
        use self::TraceToken::*;

        #[test]
        fn is_implemented_for_closures() {
            let closure = enc_func!(|handle: &mut EncodingWriter| {
                handle.write_utf8("hy ho")
            });

            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(closure.encode(&mut handle));
                handle.finish_header();
            }
            assert_eq!(encoder.trace.as_slice(), &[
                NowUtf8,
                Text("hy ho".into()),
                CRLF,
                End
            ])
        }
    }


    mod EncodingBuffer {
        #![allow(non_snake_case)]
        use super::*;
        use super::{ _Encoder as EncodingBuffer };

        #[test]
        fn new_encoder() {
            let encoder = EncodingBuffer::new(MailType::Internationalized);
            assert_eq!(encoder.mail_type(), MailType::Internationalized);
        }

        #[test]
        fn write_body_unchecked() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            let body1 = "una body\r\n";
            let body2 = "another body";

            encoder.write_body_unchecked(&body1);
            encoder.write_blank_line();
            encoder.write_body_unchecked(&body2);

            assert_eq!(
                encoder.as_slice(),
                concat!(
                    "una body\r\n",
                    "\r\n",
                    "another body\r\n"
                ).as_bytes()
            )
        }
    }


    mod EncodingWriter {
        #![allow(non_snake_case)]
        use std::mem;
        use std::str;

        use super::*;
        use super::{ _Encoder as EncodingBuffer };

        #[test]
        fn commit_partial_and_drop_does_not_panic() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_unchecked("12")));
                handle.commit_partial_header();
            }
            assert_eq!(encoder.as_slice(), b"12");
        }

        #[test]
        fn undo_does_undo() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(
                    handle.write_str(SoftAsciiStr::from_unchecked("Header-One: 12")));
                handle.undo_header();
            }
            assert_eq!(encoder.as_slice(), b"");
        }

        #[test]
        fn undo_does_not_undo_to_much() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12").unwrap()));
                handle.finish_header();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("ups: sa").unwrap()));
                handle.undo_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12\r\n");
        }

        #[test]
        fn finish_adds_crlf_if_needed() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12").unwrap()));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12\r\n");
        }

        #[test]
        fn finish_does_not_add_crlf_if_not_needed() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12\r\n").unwrap()));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12\r\n");
        }

        #[test]
        fn finish_does_truncat_if_needed() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12\r\n   ").unwrap()));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12\r\n");
        }


        #[test]
        fn finish_can_handle_fws() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12 +\r\n 4").unwrap()));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12 +\r\n 4\r\n");
        }

        #[test]
        fn finish_only_truncats_if_needed() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(
                    SoftAsciiStr::from_str("Header-One: 12 +\r\n 4  ").unwrap()));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"Header-One: 12 +\r\n 4  \r\n");
        }


        #[test]
        fn orphan_lf_error() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_err!(handle.write_str(SoftAsciiStr::from_str("H: \na").unwrap()));
                handle.undo_header()
            }
        }
        #[test]
        fn orphan_cr_error() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_err!(handle.write_str(SoftAsciiStr::from_str("H: \ra").unwrap()));
                handle.undo_header()
            }
        }

        #[test]
        fn orphan_trailing_lf() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_err!(handle.write_str(SoftAsciiStr::from_str("H: a\n").unwrap()));
                handle.undo_header();
            }
        }

        #[test]
        fn orphan_trailing_cr() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("H: a\r").unwrap()));
                //it's fine not to error in the trailing \r case as we want to write
                //a \r\n anyway
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"H: a\r\n");
        }

         #[test]
        fn soft_line_limit_can_be_breached() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                for _ in 0u32..500 {
                    assert_ok!(handle.internal_write_char("a"));
                }
                handle.finish_header();
            }
        }

        #[test]
        fn hard_line_limit_can_not_be_breached() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                for _ in 0u32..998 {
                    assert_ok!(handle.internal_write_char("a"));
                }

                assert_err!(handle.internal_write_char("b"));
                handle.finish_header();
            }
        }

        #[test]
        fn break_line_on_fws() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("A23456789:").unwrap()));
                handle.mark_fws_pos();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str(concat!(
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "12345678XX"
                )).unwrap()));
                handle.finish_header();
            }
            assert_eq!(
                encoder.as_str().unwrap(),
                concat!(
                    "A23456789:\r\n ",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "12345678XX\r\n"
                )
            );
        }

        #[test]
        fn break_line_on_fws_does_not_insert_unessesary_space() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("A23456789:").unwrap()));
                handle.write_fws();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str(concat!(
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "12345678XX"
                )).unwrap()));
                handle.finish_header();
            }

            assert_eq!(
                encoder.as_str().unwrap(),
                concat!(
                    "A23456789:\r\n ",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "12345678XX\r\n"
                )
            );
        }


        #[test]
        fn to_long_unbreakable_line() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("A23456789:").unwrap()));
                handle.mark_fws_pos();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str(concat!(
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "80_3456789",
                    "90_3456789",
                    "00_3456789",
                )).unwrap()));
                handle.finish_header();
            }
            assert_eq!(
                encoder.as_str().unwrap(),
                concat!(
                    "A23456789:\r\n ",
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                    "80_3456789",
                    "90_3456789",
                    "00_3456789\r\n",
                )
            );
        }

        #[test]
        fn multiple_lines_breaks() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("A23456789:").unwrap()));
                handle.mark_fws_pos();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str(concat!(
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789",
                )).unwrap()));
                handle.mark_fws_pos();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str(concat!(
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                )).unwrap()));
                handle.finish_header();
            }
            assert_eq!(
                encoder.as_str().unwrap(),
                concat!(
                    "A23456789:\r\n ",
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789",
                    "50_3456789",
                    "60_3456789",
                    "70_3456789\r\n ",
                    "10_3456789",
                    "20_3456789",
                    "30_3456789",
                    "40_3456789\r\n",
                )
            );
        }

        #[test]
        fn hard_line_limit() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                for x in 0..998 {
                    if let Err(_) = handle.write_char(SoftAsciiChar::from_unchecked('X')) {
                        panic!("error when writing char nr.: {:?}", x+1)
                    }
                }
                let res = &[
                    handle.write_char(SoftAsciiChar::from_unchecked('X')).is_err(),
                    handle.write_char(SoftAsciiChar::from_unchecked('X')).is_err(),
                    handle.write_char(SoftAsciiChar::from_unchecked('X')).is_err(),
                    handle.write_char(SoftAsciiChar::from_unchecked('X')).is_err(),
                ];
                assert_eq!(
                    res, &[true, true, true, true]
                );
                handle.undo_header();
            }
        }

        #[test]
        fn write_utf8_fail_on_ascii_mail() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_err!(handle.write_utf8("↓"));
                handle.undo_header();
            }
        }

        #[test]
        fn write_utf8_ascii_string_fail_on_ascii_mail() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_err!(handle.write_utf8("just_ascii"));
                handle.undo_header();
            }
        }

        #[test]
        fn write_utf8_ok_on_internationalized_mail() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("❤"));
                handle.finish_header();
            }
            assert_eq!(encoder.as_str().unwrap(), "❤\r\n");
        }

        #[test]
        fn try_write_atext_ascii() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_if_atext("hoho")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                let mut had_cond_failure = false;
                assert_ok!(handle.write_if_atext("a(b")
                    .handle_condition_failure(|_| {had_cond_failure=true; Ok(())}));
                assert!(had_cond_failure);
                assert_ok!(handle.write_if_atext("")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                handle.finish_header();
            }
            assert_eq!(encoder.as_slice(), b"hoho\r\n");
        }

        #[test]
        fn try_write_atext_internationalized() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_if_atext("hoho")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                let mut had_cond_failure = false;
                assert_ok!(handle.write_if_atext("a(b")
                    .handle_condition_failure(|_| {had_cond_failure=true; Ok(())}));
                assert!(had_cond_failure);
                assert_ok!(handle.write_if_atext("❤")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                handle.finish_header();
            }
            assert_eq!(encoder.as_str().unwrap(), "hoho❤\r\n");
        }

        #[test]
        fn multiple_finish_calls_are_ok() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_if_atext("hoho")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                let mut had_cond_failure = false;
                assert_ok!(handle.write_if_atext("a(b")
                    .handle_condition_failure(|_| {had_cond_failure=true; Ok(())}));
                assert!(had_cond_failure);
                assert_ok!(handle.write_if_atext("❤")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                handle.finish_header();
                handle.finish_header();
                handle.finish_header();
                handle.finish_header();
            }
            assert_eq!(encoder.as_str().unwrap(), "hoho❤\r\n");
        }

        #[test]
        fn multiple_finish_and_undo_calls() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_if_atext("hoho")
                    .handle_condition_failure(|_|panic!("no condition failur expected")));
                handle.undo_header();
                handle.finish_header();
                handle.undo_header();
                handle.undo_header();
            }
            assert_eq!(encoder.as_slice(), b"");
        }

        #[test]
        fn header_body_header() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("H: yay"));
                handle.finish_header();
            }
            encoder.write_body_unchecked(&"da body");
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("❤"));
                handle.finish_header();
            }
            assert_eq!(
                encoder.as_slice(),
                concat!(
                    "H: yay\r\n",
                    "da body\r\n",
                    "❤\r\n"
                ).as_bytes()
            );
        }

        #[test]
        fn has_unfinished_parts() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("Abc:"));
                assert!(handle.has_unfinished_parts());
                handle.undo_header();
                assert_not!(handle.has_unfinished_parts());
                assert_ok!(handle.write_utf8("Abc: c"));
                assert!(handle.has_unfinished_parts());
                handle.finish_header();
                assert_not!(handle.has_unfinished_parts());
            }
        }

        #[test]
        fn drop_without_write_is_ok() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            let handle = encoder.writer();
            mem::drop(handle)
        }

        #[test]
        fn drop_after_undo_is_ok() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            let mut handle = encoder.writer();
            assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One").unwrap()));
            handle.undo_header();
            mem::drop(handle);
        }

        #[test]
        fn drop_after_finish_is_ok() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            let mut handle = encoder.writer();
            assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One: 12").unwrap()));
            handle.finish_header();
            mem::drop(handle);
        }

        #[should_panic]
        #[test]
        fn drop_unfinished_panics() {
            let mut encoder = EncodingBuffer::new(MailType::Ascii);
            let mut handle = encoder.writer();
            assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header-One:").unwrap()));
            mem::drop(handle);
        }

        #[test]
        fn trace_and_undo() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("something"));
                handle.mark_fws_pos();
                assert_ok!(handle.write_utf8("<else>"));
                handle.undo_header();
            }
            assert_eq!(encoder.trace.len(), 0);
        }

        #[test]
        fn trace_and_undo_does_do_to_much() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_utf8("H: a"));
                handle.finish_header();
                assert_ok!(handle.write_utf8("something"));
                handle.mark_fws_pos();
                assert_ok!(handle.write_utf8("<else>"));
                handle.undo_header();
            }
            assert_eq!(encoder.trace, vec![
                NowUtf8,
                Text("H: a".into()),
                CRLF,
                End
            ]);
        }

        #[test]
        fn trace_traces() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            {
                let mut handle = encoder.writer();
                assert_ok!(handle.write_str(SoftAsciiStr::from_str("Header").unwrap()));
                assert_ok!(handle.write_char(SoftAsciiChar::from_unchecked(':')));
                let mut had_cond_failure = false;
                assert_ok!(handle.write_if_atext("a(b)c")
                    .handle_condition_failure(|_|{had_cond_failure=true; Ok(())}));
                assert_ok!(handle.write_if_atext("abc")
                    .handle_condition_failure(|_|panic!("unexpected cond failure")));
                assert_ok!(handle.write_utf8("❤"));
                assert_ok!(handle.write_str_unchecked("remove me\r\n"));
                assert_ok!(handle.write_utf8("   "));
                handle.finish_header()
            }
            assert_eq!(encoder.trace, vec![
                NowStr,
                Text("Header".into()),
                NowChar,
                Text(":".into()),
                NowAText,
                Text("abc".into()),
                NowUtf8,
                Text("❤".into()),
                NowUnchecked,
                Text("remove me".into()),
                CRLF,
                NowUtf8,
                Text("   ".into()),
                TruncateToCRLF,
                End
            ]);
        }

        #[test]
        fn with_handle_on_error() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            let res = encoder.write_header_line(|hdl| {
                hdl.write_utf8("some partial writes")?;
                Err(EncodingErrorKind::Other { kind: "error ;=)" }.into())
            });
            assert_err!(res);
            assert_eq!(encoder.trace, vec![]);
            assert_eq!(encoder.as_slice(), b"");
        }

        #[test]
        fn with_handle_partial_writes() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            let res = encoder.write_header_line(|hdl| {
                hdl.write_utf8("X-A: 12")
            });
            assert_ok!(res);
            assert_eq!(encoder.trace, vec![
                NowUtf8,
                Text("X-A: 12".into()),
                CRLF,
                End
            ]);
            assert_eq!(encoder.as_slice(), b"X-A: 12\r\n");
        }

        #[test]
        fn with_handle_ok() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            let res = encoder.write_header_line(|hdl| {
                hdl.write_utf8("X-A: 12")?;
                hdl.finish_header();
                Ok(())
            });
            assert_ok!(res);
            assert_eq!(encoder.trace, vec![
                NowUtf8,
                Text("X-A: 12".into()),
                CRLF,
                End,
            ]);
            assert_eq!(encoder.as_slice(), b"X-A: 12\r\n")
        }

        #[test]
        fn douple_write_fws() {
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            let res = encoder.write_header_line(|hdl| {
                hdl.write_fws();
                hdl.write_fws();
                Ok(())
            });
            assert_ok!(res);
            assert_eq!(encoder.trace, vec![
                MarkFWS, NowChar, Text(" ".to_owned()),
                MarkFWS, NowChar, Text(" ".to_owned()),
                TruncateToCRLF,
                End
            ]);
            assert_eq!(encoder.as_slice(), b"")
        }

        #[test]
        fn double_write_fws_then_long_line() {
            let long_line = concat!(
                "10_3456789",
                "20_3456789",
                "30_3456789",
                "40_3456789",
                "50_3456789",
                "60_3456789",
                "70_3456789",
                "80_3456789",
            );
            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            let res = encoder.write_header_line(|hdl| {
                hdl.write_fws();
                hdl.write_fws();
                hdl.write_utf8(long_line)?;
                Ok(())
            });
            assert_ok!(res);
            assert_eq!(encoder.trace, vec![
                MarkFWS, NowChar, Text(" ".to_owned()),
                MarkFWS, NowChar, Text(" ".to_owned()),
                NowUtf8, Text(long_line.to_owned()),
                CRLF,
                End
            ]);
            assert_eq!(encoder.as_slice(), format!("  {}\r\n", long_line).as_bytes())
        }

        #[test]
        fn semantic_ws_are_not_eaten_with_line_breaking() {
            let long_line_1 = concat!(
                "Header:789",
                "20_3456789",
                "30_3456789",
                "40_3456789",
                "50_3456789",
                "60_3456789",
            );
            let long_line_2 = concat!(
                " xxxxxxxxx",
                "80_3456789"
            );

            let expected_res = concat!(
                "Header:789",
                "20_3456789",
                "30_3456789",
                "40_3456789",
                "50_3456789",
                "60_3456789",
                "\r\n ",
                " xxxxxxxxx",
                "80_3456789",
                "\r\n"
            );

            let mut encoder = EncodingBuffer::new(MailType::Internationalized);
            encoder.write_header_line(|hdl| {
                hdl.write_utf8(long_line_1).unwrap();
                hdl.mark_fws_pos();
                hdl.write_utf8(long_line_2).unwrap();
                hdl.finish_header();
                Ok(())
            }).unwrap();

            let got = str::from_utf8(encoder.as_slice()).unwrap();
            assert_eq!(expected_res, got);
        }
    }

    ec_test! {
        does_ec_test_work,
        {
            use super::EncodingWriter;
            enc_func!(|x: &mut EncodingWriter| {
                x.write_utf8("hy")
            })
        } => Utf8 => [
            Text "hy"
        ]
    }

    ec_test! {
        does_ec_test_work_with_encode_closure,
        {
            use super::EncodingWriter;
            let think = "hy";
            enc_closure!(move |x: &mut EncodingWriter| {
                x.write_utf8(think)
            })
        } => Utf8 => [
            Text "hy"
        ]
    }

    ec_test! {
        does_ec_test_allow_early_return,
        {
            use super::EncodingWriter;
            // this is just a type system test, if it compiles it can bail
            if false { ec_bail!(kind: Other { kind: "if false ..." }) }
            enc_func!(|x: &mut EncodingWriter| {
                x.write_utf8("hy")
            })
        } => Utf8 => [
            Text "hy"
        ]
    }

    mod trait_object {
        use super::super::*;

        #[derive(Default, Clone, PartialEq, Debug)]
        struct TestType(&'static str);

        impl EncodableInHeader for TestType {
            fn encode(&self, encoder:  &mut EncodingWriter) -> Result<(), EncodingError> {
                encoder.write_utf8(self.0)
            }

            fn boxed_clone(&self) -> Box<EncodableInHeader> {
                Box::new(self.clone())
            }
        }

        #[derive(Default, Clone, PartialEq, Debug)]
        struct AnotherType(&'static str);

        impl EncodableInHeader for AnotherType {
            fn encode(&self, encoder:  &mut EncodingWriter) -> Result<(), EncodingError> {
                encoder.write_utf8(self.0)
            }

            fn boxed_clone(&self) -> Box<EncodableInHeader> {
                Box::new(self.clone())
            }
        }

        #[test]
        fn is() {
            let tt = TestType::default();
            let erased: &EncodableInHeader = &tt;
            assert_eq!( true, erased.is::<TestType>() );
            assert_eq!( false, erased.is::<AnotherType>());
        }

        #[test]
        fn downcast_ref() {
            let tt = TestType::default();
            let erased: &EncodableInHeader = &tt;
            let res: Option<&TestType> = erased.downcast_ref::<TestType>();
            assert_eq!( Some(&tt), res );
            assert_eq!( None, erased.downcast_ref::<AnotherType>() );
        }

        #[test]
        fn downcast_mut() {
            let mut tt_nr2 = TestType::default();
            let mut tt = TestType::default();
            let erased: &mut EncodableInHeader = &mut tt;
            {
                let res: Option<&mut TestType> = erased.downcast_mut::<TestType>();
                assert_eq!( Some(&mut tt_nr2), res );
            }
            assert_eq!( None, erased.downcast_mut::<AnotherType>() );
        }

        #[test]
        fn downcast() {
            let tt = Box::new( TestType::default() );
            let erased: Box<EncodableInHeader> = tt;
            let erased = assert_err!(erased.downcast::<AnotherType>());
            let _: Box<TestType> = assert_ok!(erased.downcast::<TestType>());
        }
    }
}