#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![allow(clippy::tabs_in_doc_comments)]
extern crate alloc;
extern crate core;
use alloc::{
borrow::Cow,
string::{String, ToString}
};
use core::fmt::{Debug, Write};
mod audio;
mod r#break;
mod element;
mod emphasis;
mod error;
mod group;
mod lang;
mod mark;
pub mod mstts;
mod prosody;
mod say_as;
mod speak;
mod text;
mod unit;
pub mod util;
pub mod visit;
pub mod visit_mut;
mod voice;
mod xml;
pub use self::{
audio::{Audio, AudioRepeat, audio},
r#break::{Break, BreakStrength, breaks},
element::{CustomElement, Element, IntoElement},
emphasis::{Emphasis, EmphasisLevel, emphasis},
error::{Error, Result},
group::{Group, group},
lang::{Lang, lang},
mark::{Mark, mark},
prosody::{Prosody, ProsodyContour, ProsodyControl, ProsodyPitch, ProsodyRate, ProsodyVolume, prosody},
say_as::{DateFormat, SayAs, SpeechFormat, say_as},
speak::{Speak, speak},
text::{Text, text},
unit::{Decibels, DecibelsError, TimeDesignation, TimeDesignationError},
voice::{Voice, VoiceConfig, VoiceGender, voice},
xml::{EscapedDisplay, XmlWriter}
};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Flavor {
#[default]
Generic,
MicrosoftAzureCognitiveSpeechServices,
GoogleCloudTextToSpeech,
AmazonPolly,
PykeSongbird
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct SerializeOptions {
pub flavor: Flavor,
pub pretty: bool
}
impl Default for SerializeOptions {
fn default() -> Self {
SerializeOptions {
flavor: Flavor::Generic,
pretty: false
}
}
}
impl SerializeOptions {
pub fn min(mut self) -> Self {
self.pretty = false;
self
}
pub fn pretty(mut self) -> Self {
self.pretty = true;
self
}
pub fn flavor(mut self, flavor: Flavor) -> Self {
self.flavor = flavor;
self
}
}
pub trait Serialize {
fn serialize<W: Write>(&self, writer: &mut W, options: &SerializeOptions) -> crate::Result<()> {
let mut writer = XmlWriter::new(writer, options.pretty);
self.serialize_xml(&mut writer, options)?;
Ok(())
}
fn serialize_xml<W: Write>(&self, writer: &mut XmlWriter<W>, options: &SerializeOptions) -> crate::Result<()>;
fn serialize_to_string(&self, options: &SerializeOptions) -> crate::Result<String> {
let mut out = String::new();
self.serialize(&mut out, options)?;
Ok(out)
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Meta<'s> {
raw: Cow<'s, str>,
name: Option<Cow<'s, str>>
}
impl<'s> Meta<'s> {
pub fn new(xml: impl Into<Cow<'s, str>>) -> Self {
Meta { raw: xml.into(), name: None }
}
pub fn with_name(mut self, name: impl Into<Cow<'s, str>>) -> Self {
self.name = Some(name.into());
self
}
pub fn to_owned(&self) -> Meta<'static> {
self.clone().into_owned()
}
pub fn into_owned(self) -> Meta<'static> {
Meta {
raw: match self.raw {
Cow::Borrowed(b) => Cow::Owned(b.to_string()),
Cow::Owned(b) => Cow::Owned(b)
},
name: match self.name {
Some(Cow::Borrowed(b)) => Some(Cow::Owned(b.to_string())),
Some(Cow::Owned(b)) => Some(Cow::Owned(b)),
None => None
}
}
}
}
impl<'s> Serialize for Meta<'s> {
fn serialize_xml<W: Write>(&self, writer: &mut XmlWriter<W>, _: &SerializeOptions) -> crate::Result<()> {
writer.raw(&self.raw)
}
}