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
//! Controls writing of the XML part of SSML. This contains all low level bindings in a sense
//! to the tags. You should probably never use this directly.

use color_eyre::{eyre::eyre, Result};
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;

use std::io::Cursor;

use crate::ssml_constants::*;

/// An XML Writer. Used for manual manipulation of the SSML Output (which uses XML).
///
/// You should probably never use this directly, instead interacting with the parser,
/// however if you'd like to build your own parser, and just reuse the XML Rendering
/// then you'd want to use this.
pub struct XmlWriter {
    /// The XML Writer instance. The thing that actually writes the XML.
    pub writer: Writer<Cursor<Vec<u8>>>,
}

impl XmlWriter {
    /// Creates a new XML Writer. This writerr writes into a std::vec::Vec, and at any
    /// point can be turned into a string. It is your job to close all tags before rendering
    /// this. We don't close everything when you render it. You render what you put in.
    ///
    /// It should also note we automatically write the header:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// ```
    ///
    /// Upon creation of an XML Writer. This is to try, and keep as close to the W3C docs
    /// for SSML v1.1. Which you can read about
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let result = XmlWriter::new();
    /// assert!(result.is_ok());
    /// ```
    pub fn new() -> Result<XmlWriter> {
        let mut writer = Writer::new(Cursor::new(Vec::new()));
        writer.write_event(Event::Decl(BytesDecl::new(b"1.0", None, None)))?;
        Ok(XmlWriter { writer: writer })
    }

    /// Starts an SSML <speak> tag. For AWS Polly this is the root tag, and should only have one
    /// decleration as mentioned in their docs (As of April 20th, 2017):
    ///
    /// ```text
    /// The <speak> tag is the root element of all Amazon Polly SSML text.
    /// All SSML-enhanced text to be spoken must be included within this tag.
    /// ```
    ///
    /// It should be noted although AWS Docs do not mention any attributes you can pass in
    /// to the <speak> tag, we still have an optional `lang`, and `onlangfailure` attributes
    /// to closely mirror the W3C Standard, as seen:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/).
    ///
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_speak_result = new_xml_writer.unwrap().start_ssml_speak(None, None);
    /// assert!(start_speak_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <speak xml:lang="en-US" onlangfailure="processorchoice"
    ///    xmlns="http://www.w3.org/2001/10/synthesis"
    ///    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    /// ```
    pub fn start_ssml_speak(
        &mut self,
        lang: Option<String>,
        onlangfailure: Option<String>,
    ) -> Result<()> {
        let mut elem = BytesStart::owned(b"speak".to_vec(), "speak".len());
        elem.push_attribute(("xml:lang", &*lang.unwrap_or("en-US".to_owned())));
        elem.push_attribute((
            "onlangfailure",
            &*onlangfailure.unwrap_or("processorchoice".to_owned()),
        ));
        elem.push_attribute(("xmlns", "http://www.w3.org/2001/10/synthesis"));
        elem.push_attribute(("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <speak> tag. For AWS Polly this should be the root tag, and you
    /// should only close it when you are done.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_speak_result = new_xml_writer.unwrap().end_ssml_speak();
    /// assert!(end_speak_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </speak>
    /// ```
    pub fn end_ssml_speak(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"speak")))?)
    }

    /// Creates an SSML <break> tag. AWS Polly follows the W3C SSMLv1.1 standard for
    /// this tag.
    ///
    /// You can find the SSML <break> tag documented in the W3C's guide:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/).
    /// Although  you can find the specific implementation details on AWS's site:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#break-tag).
    ///
    /// According to the W3C 1.1 Standard both the strength, and time are optional.
    /// Though both can be used in combination.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let mut xml_writer = new_xml_writer.unwrap();
    /// let result = xml_writer.ssml_break(None, None);
    /// assert!(result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <break />
    /// ```
    ///
    /// ---
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::{BreakStrength, BreakTime};
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let mut xml_writer = new_xml_writer.unwrap();
    /// let result = xml_writer.ssml_break(Some(BreakStrength::XStrong), Some(BreakTime::new(10, true)));
    /// assert!(result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <break strength="x-strong" time="10s" />
    /// ```
    pub fn ssml_break(
        &mut self,
        strength: Option<BreakStrength>,
        time: Option<BreakTime>,
    ) -> Result<()> {
        let mut elem = BytesStart::owned(b"break".to_vec(), "break".len());

        if strength.is_some() {
            elem.push_attribute(("strength", &*format!("{}", strength.unwrap())));
        }
        if time.is_some() {
            elem.push_attribute(("time", &*format!("{}", time.unwrap())));
        }

        Ok(self.writer.write_event(Event::Empty(elem))?)
    }

    /// Starts an SSML Lang tag. The Lang tag is useful for telling say
    /// someone speaking in english that they're about to speak a french word. You can keep
    /// the overall text english, but have a mix of french words in there. Although AWS polly
    /// only documents support the `xml:lang` attribute, we also pass in `onlangfailure`
    /// which is documented inside the W3C SSML 1.1 standard which can be found:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_lang).
    ///
    /// You can find the AWS Documentation that mentions the lang tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#lang-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_lang_result = new_xml_writer.unwrap().start_ssml_lang("fr-FR".to_owned(), None);
    /// assert!(start_lang_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <lang xml:lang="fr-FR" onlangfailure="processorchoice">
    /// ```
    ///
    /// ---
    ///
    /// Rust Code:
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_lang_result = new_xml_writer.unwrap().start_ssml_lang("fr-FR".to_owned(),
    ///   Some("changevoice".to_owned()));
    /// assert!(start_lang_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <lang xml:lang="fr-FR" onlangfailure="changevoice">
    /// ```
    pub fn start_ssml_lang(&mut self, lang: String, onlangfailure: Option<String>) -> Result<()> {
        let mut elem = BytesStart::owned(b"lang".to_vec(), "lang".len());
        elem.push_attribute(("xml:lang", &*lang));
        elem.push_attribute((
            "onlangfailure",
            &*onlangfailure.unwrap_or("processorchoice".to_owned()),
        ));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <lang> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_lang_result = new_xml_writer.unwrap().end_ssml_lang();
    /// assert!(end_lang_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </lang>
    /// ```
    pub fn end_ssml_lang(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"lang")))?)
    }

    /// Starts an SSML Mark tag. Although this will make no difference in the voice
    /// of the text, this will place a marker inside the SSML Metadata returned from Polly.
    /// This can be useful if you want to perform some sort of actions on certain words.
    /// AWS Polly follows the W3C SSML v1.1 Spec here, and documentation can be found:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_mark).
    ///
    /// You can find the AWS Documentation that mentions the mark tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#custom-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_mark_result = new_xml_writer.unwrap().start_ssml_mark("animal".to_owned());
    /// assert!(start_mark_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// Mmark name="animal">
    /// ```
    pub fn start_ssml_mark(&mut self, name: String) -> Result<()> {
        let mut elem = BytesStart::owned(b"mark".to_vec(), "mark".len());
        elem.push_attribute(("name", &*name));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <mark> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_mark_result = new_xml_writer.unwrap().end_ssml_mark();
    /// assert!(end_mark_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </mark>
    /// ```
    pub fn end_ssml_mark(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"mark")))?)
    }

    /// Starts an SSML Paragraph Tag. The Paragraph Tag is useful for breaking
    /// up multiple paragraphs of text. AWS Polly follows the W3C SSML v1.1 Standard Here.
    /// As such the documentation for the paragraph tag can be found:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_paragraph).
    ///
    /// You can find the AWS Documentation that mentions the paragraph tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#p-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_p_result = new_xml_writer.unwrap().start_ssml_paragraph();
    /// assert!(start_p_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <p>
    /// ```
    pub fn start_ssml_paragraph(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::Start(BytesStart::owned(b"p".to_vec(), "p".len())))?)
    }

    /// Ends an SSML <p> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_p_result = new_xml_writer.unwrap().end_ssml_paragraph();
    /// assert!(end_p_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </p>
    /// ```
    pub fn end_ssml_paragraph(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"p")))?)
    }

    /// Starts an SSML Phoneme Tag. The Phoneme Tag is useful for custom pronunciation for words.
    /// The Phoneme Tag should really only be used on a per word/short phrase basis. You don't
    /// want to use a phoneme tag for an entire paragraph of text. The Phoneme Tag in polly
    /// has two required attributes both "ph", and "alphabet". Which deviates from the W3C Standard,
    /// which says only "ph" is required. However since Polly implements close to perfect the W3C
    /// SSML v1.1 Standard here you should still probably read their documentation on the tag:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_phoneme).
    ///
    /// You can find the AWS Documentation that mentions the phoneme tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#phoneme-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::PhonemeAlphabet;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_phoneme_result = new_xml_writer.unwrap().start_ssml_phoneme(PhonemeAlphabet::Ipa,
    ///  "d͡ʒt͡ʃΘɚoʊɛ".to_owned());
    /// assert!(start_phoneme_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <phoneme alphabet="ipa" ph="d͡ʒt͡ʃΘɚoʊɛ">
    /// ```
    pub fn start_ssml_phoneme(&mut self, alphabet: PhonemeAlphabet, ph: String) -> Result<()> {
        let mut elem = BytesStart::owned(b"phoneme".to_vec(), "phoneme".len());
        elem.push_attribute(("alphabet", &*format!("{}", alphabet)));
        elem.push_attribute(("ph", &*ph));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <phoneme> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_phoneme_result = new_xml_writer.unwrap().end_ssml_phoneme();
    /// assert!(end_phoneme_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </phoneme>
    /// ```
    pub fn end_ssml_phoneme(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"phoneme")))?)
    }

    /// Starts an SSML Prosody Tag. The prosody tag seems to be the one that derives the most
    /// from the SSML Specification. Which in some instances is fine because it makes for easier
    /// reading (e.g. +20% pitch), but in other places is kind of sad we can't do that. (e.g.
    /// things like duration). As such I'll only link to the AWS documentation.
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#prosody-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_prosody_result = new_xml_writer.unwrap().start_ssml_prosody(Some("+5db".to_owned()), None, None);
    /// assert!(start_prosody_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <prosody volume="+6db">
    /// ```
    ///
    /// ---
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::ProsodyRate;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_prosody_result = new_xml_writer.unwrap()
    ///   .start_ssml_prosody(Some("+6dB".to_owned()), Some(ProsodyRate::XFast),
    ///    Some("+100%".to_owned()));
    /// assert!(start_prosody_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <prosody volume="+6db" rate="x-fast" pitch="+100%">
    /// ```
    pub fn start_ssml_prosody(
        &mut self,
        volume: Option<String>,
        rate: Option<ProsodyRate>,
        pitch: Option<String>,
    ) -> Result<()> {
        let mut elem = BytesStart::owned(b"prosody".to_vec(), "prosody".len());
        if volume.is_none() && rate.is_none() && pitch.is_none() {
            return Err(eyre!("Prosody Tag was supplied no values."));
        }
        if volume.is_some() {
            elem.push_attribute(("volume", &*volume.unwrap()));
        }
        if rate.is_some() {
            elem.push_attribute(("rate", &*format!("{}", rate.unwrap())));
        }
        if pitch.is_some() {
            elem.push_attribute(("pitch", &*pitch.unwrap()));
        }
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <prosody> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_prosody_result = new_xml_writer.unwrap().end_ssml_prosody();
    /// assert!(end_prosody_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </prosody>
    /// ```
    pub fn end_ssml_prosody(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"prosody")))?)
    }

    /// Starts an SSML Sentence Tag. The Sentence Tag is useful for breaking
    /// up multiple sentences of text. AWS Polly follows the W3C SSML v1.1 Standard Here.
    /// As such the documentation for the sentence tag can be found:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_sentence).
    ///
    /// You can find the AWS Documentation that mentions the sentence tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#s-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_s_result = new_xml_writer.unwrap().start_ssml_sentence();
    /// assert!(start_s_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <s>
    /// ```
    pub fn start_ssml_sentence(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::Start(BytesStart::owned(b"s".to_vec(), "s".len())))?)
    }

    /// Ends an SSML <s> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_s_result = new_xml_writer.unwrap().end_ssml_sentence();
    /// assert!(end_s_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </s>
    /// ```
    pub fn end_ssml_sentence(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"s")))?)
    }

    /// Starts an SSML say-as Tag. The say-as tag is used for determing how a body of text
    /// should be interpreted, for example a phone number, or if you want something spelled
    /// out letter by letter. However AWS polly only supports the `interpret-as` attribute
    /// which is required, and does not support the `format`, and `detail` attributes.
    /// However for posterity you can read the W3C SSML v1.1 Spec:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_say-as).
    /// It should be noted the parameter for interpret-as is kept dynamic, since in the
    /// spec it says this list ***should*** change rapidly.
    ///
    /// You can find the AWS Documentation that mentions the say-as tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#say-as-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_say_as_result = new_xml_writer.unwrap().start_ssml_say_as("character".to_owned());
    /// assert!(start_say_as_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <say-as interpret-as="character">
    /// ```
    pub fn start_ssml_say_as(&mut self, interpret_as: String) -> Result<()> {
        let mut elem = BytesStart::owned(b"say-as".to_vec(), "say-as".len());
        elem.push_attribute(("interpret-as", &*interpret_as));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <say-as> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_say_as_result = new_xml_writer.unwrap().end_ssml_say_as();
    /// assert!(end_say_as_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </say-as>
    /// ```
    pub fn end_ssml_say_as(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"say-as")))?)
    }

    /// Starts an SSML sub Tag. The sub tag is used for a substitution of a word.
    /// For example in elememtal symbols you may want to show the elememtal symbol, but have
    /// the engine say the actual element name. AWS polly follows the W3C SSML v1.1 Spec:
    /// [HERE](https://www.w3.org/TR/2010/REC-speech-synthesis11-20100907/#edef_sub).
    ///
    /// You can find the AWS Documentation that mentions the sub tag:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#sub-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_sub_result = new_xml_writer.unwrap().start_ssml_sub("mercury".to_owned());
    /// assert!(start_sub_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <sub alias="mercury">
    /// ```
    pub fn start_ssml_sub(&mut self, alias: String) -> Result<()> {
        let mut elem = BytesStart::owned(b"sub".to_vec(), "sub".len());
        elem.push_attribute(("alias", &*alias));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <sub> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_sub_result = new_xml_writer.unwrap().end_ssml_sub();
    /// assert!(end_sub_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </sub>
    /// ```
    pub fn end_ssml_sub(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"sub")))?)
    }

    /// Starts an SSML Word/Token tag. The Word/Token tag for AWS Polly also deviates pretty
    /// far from the W3C Spec. So here like a few tags who shall not be named I will also
    /// only ilnk to the AWS Documentation for this tag. Which can be found:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html#w-tag).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::WordRole;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_w_result = new_xml_writer.unwrap().start_ssml_w(WordRole::Verb);
    /// assert!(start_w_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <w role="amazon:VB">
    /// ```
    pub fn start_ssml_w(&mut self, role: WordRole) -> Result<()> {
        let mut elem = BytesStart::owned(b"w".to_vec(), "w".len());
        elem.push_attribute(("role", &*format!("{}", role)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <w> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_w_result = new_xml_writer.unwrap().end_ssml_w();
    /// assert!(end_w_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </w>
    /// ```
    pub fn end_ssml_w(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"w")))?)
    }

    /// Starts an SSML amazon domain tag. These tags are unique to AWS Polly. As such
    /// the only place they are documented is inside the AWS Docs themsleves which are:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::AmazonDomainNames;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_effect_result = new_xml_writer.unwrap()
    ///   .start_ssml_amazon_domain(AmazonDomainNames::News);
    /// assert!(start_amazon_effect_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:domain name="news">
    /// ```
    pub fn start_ssml_amazon_domain(&mut self, name: AmazonDomainNames) -> Result<()> {
        let mut elem = BytesStart::owned(b"amazon:domain".to_vec(), "amazon:domain".len());
        elem.push_attribute(("name", &*format!("{}", name)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <amazon:domain> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_amazon_effect_result = new_xml_writer.unwrap().end_ssml_amazon_domain();
    /// assert!(end_amazon_effect_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </amazon:domain>
    /// ```
    pub fn end_ssml_amazon_domain(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"amazon:domain")))?)
    }

    /// Starts an SSML amazon effect tag. These tags are unique to AWS Polly. As such
    /// the only place they are documented is inside the AWS Docs themsleves which are:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::AmazonEffect;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_effect_result = new_xml_writer.unwrap()
    ///   .start_ssml_amazon_effect(AmazonEffect::Whispered);
    /// assert!(start_amazon_effect_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:effect name="whispered">
    /// ```
    pub fn start_ssml_amazon_effect(&mut self, name: AmazonEffect) -> Result<()> {
        let mut elem = BytesStart::owned(b"amazon:effect".to_vec(), "amazon:effect".len());
        elem.push_attribute(("name", &*format!("{}", name)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <amazon:effect> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_amazon_effect_result = new_xml_writer.unwrap().end_ssml_amazon_effect();
    /// assert!(end_amazon_effect_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </amazon:effect>
    /// ```
    pub fn end_ssml_amazon_effect(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"amazon:effect")))?)
    }

    /// Starts an SSML vocal tract tag. These tags are unique to AWS Polly. As such
    /// the only place they are documented is inside the AWS Docs themsleves which are:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_effect_result = new_xml_writer.unwrap()
    ///   .start_ssml_vocal_tract_length("+10%".to_owned());
    /// assert!(start_amazon_effect_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:effect vocal-tract-length="+10%">
    /// ```
    pub fn start_ssml_vocal_tract_length(&mut self, factor: String) -> Result<()> {
        let mut elem = BytesStart::owned(b"amazon:effect".to_vec(), "amazon:effect".len());
        elem.push_attribute(("vocal-tract-length", &*format!("{}", factor)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Starts an SSML phonation tag. These tags are unique to AWS Polly. As such
    /// the only place they are documented is inside the AWS Docs themsleves which are:
    /// [HERE](http://docs.aws.amazon.com/polly/latest/dg/supported-ssml.html).
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::PhonationVolume;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_phonation_result = new_xml_writer.unwrap()
    ///   .start_ssml_phonation(PhonationVolume::Soft);
    /// assert!(start_amazon_phonation_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:effect phonation="soft">
    /// ```
    pub fn start_ssml_phonation(&mut self, volume: PhonationVolume) -> Result<()> {
        let mut elem = BytesStart::owned(b"amazon:effect".to_vec(), "amazon:effect".len());
        elem.push_attribute(("phonation", &*format!("{}", volume)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Starts an SSML <amazon:auto-breaths> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::*;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_auto_breaths_result = new_xml_writer.unwrap().start_ssml_auto_breaths(
    ///   BreathVolumes::Def,
    ///   AutoBreathFrequency::Def,
    ///   BreathDuration::Def,
    /// );
    /// assert!(start_amazon_auto_breaths_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:auto-breaths volume="default" frequency="default" duration="default">
    /// ```
    pub fn start_ssml_auto_breaths(
        &mut self,
        volume: BreathVolumes,
        frequency: AutoBreathFrequency,
        duration: BreathDuration,
    ) -> Result<()> {
        let mut elem =
            BytesStart::owned(b"amazon:auto-breaths".to_vec(), "amazon:auto-breaths".len());
        elem.push_attribute(("volume", &*format!("{}", volume)));
        elem.push_attribute(("frequency", &*format!("{}", frequency)));
        elem.push_attribute(("duration", &*format!("{}", duration)));
        Ok(self.writer.write_event(Event::Start(elem))?)
    }

    /// Ends an SSML <amazon:auto-breaths> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let end_amazon_auto_breaths_result = new_xml_writer.unwrap().end_ssml_amazon_auto_breaths();
    /// assert!(end_amazon_auto_breaths_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// </amazon:auto-breaths>
    /// ```
    pub fn end_ssml_amazon_auto_breaths(&mut self) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"amazon:auto-breaths")))?)
    }

    /// Starts an SSML <amazon:breath> tag.
    ///
    /// # Examples
    ///
    /// Rust Code:
    ///
    /// ```rust
    /// use text_to_polly_ssml::xml_writer::XmlWriter;
    /// use text_to_polly_ssml::ssml_constants::*;
    /// let mut new_xml_writer = XmlWriter::new();
    /// assert!(new_xml_writer.is_ok());
    /// let start_amazon_breath_result = new_xml_writer.unwrap().write_amazon_breath(
    ///   BreathVolumes::Def,
    ///   BreathDuration::Def,
    /// );
    /// assert!(start_amazon_breath_result.is_ok());
    /// ```
    ///
    /// Generated SSML:
    ///
    /// ```text
    /// <?xml version="1.0"?>
    /// <amazon:breath volume="default" duration="default" />
    /// ```
    pub fn write_amazon_breath(
        &mut self,
        volume: BreathVolumes,
        duration: BreathDuration,
    ) -> Result<()> {
        let mut elem = BytesStart::owned(b"amazon:breath".to_vec(), "amazon:breath".len());
        elem.push_attribute(("volume", &*format!("{}", volume)));
        elem.push_attribute(("duration", &*format!("{}", duration)));

        Ok(self.writer.write_event(Event::Empty(elem))?)
    }

    /// Writes some raw text to the XML Document. Should only be used inbetween <p> tags.
    pub fn write_text(&mut self, text: &str) -> Result<()> {
        Ok(self
            .writer
            .write_event(Event::Text(BytesText::from_plain_str(text)))?)
    }

    /// Renders the XML document in it's current state. This expects the document
    /// to be completely valid UTF-8, and will do no closing of tags for you.
    pub fn render(&mut self) -> String {
        String::from_utf8(self.writer.clone().into_inner().into_inner())
            .expect("SSML is not valid UTF-8!")
    }
}