use crate::xml_escape::{escape_xml_attr, escape_xml_text};
use crate::TwiML;
#[derive(Debug, Clone, PartialEq)]
pub enum ReceiveMediaType {
ApplicationPdf,
ImageTiff,
}
impl ReceiveMediaType {
fn as_str(&self) -> &str {
match self {
ReceiveMediaType::ApplicationPdf => "application/pdf",
ReceiveMediaType::ImageTiff => "image/tiff",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ReceivePageSize {
Letter,
Legal,
A4,
}
impl ReceivePageSize {
fn as_str(&self) -> &str {
match self {
ReceivePageSize::Letter => "letter",
ReceivePageSize::Legal => "legal",
ReceivePageSize::A4 => "a4",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ReceiveAttributes {
pub action: Option<String>,
pub media_type: Option<ReceiveMediaType>,
pub method: Option<String>,
pub page_size: Option<ReceivePageSize>,
pub store_media: Option<bool>,
}
impl ReceiveAttributes {
pub fn new() -> Self {
Self::default()
}
pub fn action(mut self, action: impl Into<String>) -> Self {
self.action = Some(action.into());
self
}
pub fn media_type(mut self, media_type: ReceiveMediaType) -> Self {
self.media_type = Some(media_type);
self
}
pub fn method(mut self, method: impl Into<String>) -> Self {
self.method = Some(method.into());
self
}
pub fn page_size(mut self, page_size: ReceivePageSize) -> Self {
self.page_size = Some(page_size);
self
}
pub fn store_media(mut self, store_media: bool) -> Self {
self.store_media = Some(store_media);
self
}
}
#[derive(Debug, Clone)]
pub struct Receive {
attributes: ReceiveAttributes,
}
impl Receive {
pub(crate) fn new(attributes: Option<ReceiveAttributes>) -> Self {
Self {
attributes: attributes.unwrap_or_default(),
}
}
fn to_xml(&self) -> String {
let mut attrs = Vec::new();
if let Some(ref action) = self.attributes.action {
attrs.push(format!(" action=\"{}\"", escape_xml_attr(action)));
}
if let Some(ref media_type) = self.attributes.media_type {
attrs.push(format!(" mediaType=\"{}\"", media_type.as_str()));
}
if let Some(ref method) = self.attributes.method {
attrs.push(format!(" method=\"{}\"", escape_xml_attr(method)));
}
if let Some(ref page_size) = self.attributes.page_size {
attrs.push(format!(" pageSize=\"{}\"", page_size.as_str()));
}
if let Some(store_media) = self.attributes.store_media {
attrs.push(format!(" storeMedia=\"{}\"", store_media));
}
format!("<Receive{}/>", attrs.join(""))
}
}
#[derive(Debug, Clone, Default)]
pub struct FaxResponse {
receive: Option<Receive>,
comments_before: Vec<String>,
comments: Vec<String>,
comments_after: Vec<String>,
}
impl FaxResponse {
pub fn new() -> Self {
Self::default()
}
pub fn receive(mut self, attributes: Option<ReceiveAttributes>) -> Self {
self.receive = Some(Receive::new(attributes));
self
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comments.push(comment.into());
self
}
pub fn comment_after(mut self, comment: impl Into<String>) -> Self {
self.comments_after.push(comment.into());
self
}
pub fn comment_before(mut self, comment: impl Into<String>) -> Self {
self.comments_before.push(comment.into());
self
}
}
impl TwiML for FaxResponse {
fn to_xml(&self) -> String {
let mut xml = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
for comment in &self.comments_before {
xml.push_str(&format!("<!-- {} -->\n", escape_xml_text(comment)));
}
xml.push_str("<Response>");
for comment in &self.comments {
xml.push_str(&format!("\n <!-- {} -->", escape_xml_text(comment)));
}
if let Some(ref receive) = self.receive {
xml.push_str(&receive.to_xml());
}
xml.push_str("</Response>");
for comment in &self.comments_after {
xml.push_str(&format!("\n<!-- {} -->", escape_xml_text(comment)));
}
xml
}
}