twiml_rust/
lib.rs

1//! # TwiML Rust
2//!
3//! A Rust library for generating TwiML (Twilio Markup Language) XML responses.
4//!
5//! TwiML is an XML-based language for controlling phone calls, SMS messages, and faxes.
6//! This library provides a type-safe, idiomatic Rust API for generating TwiML responses.
7//!
8//! ## Features
9//!
10//! - **Voice Responses**: Generate TwiML for voice calls with verbs like Say, Play, Dial, Gather, etc.
11//! - **Messaging Responses**: Generate TwiML for SMS/MMS messages
12//! - **Fax Responses**: Generate TwiML for fax operations
13//! - **Validation**: Built-in validation to ensure TwiML conforms to Twilio's requirements
14//! - **Type Safety**: Strongly typed API with builder patterns
15//!
16//! ## Example
17//!
18//! ```rust
19//! use twiml_rust::{VoiceResponse, TwiML};
20//!
21//! let response = VoiceResponse::new()
22//!     .say("Hello, World!")
23//!     .play("https://example.com/audio.mp3");
24//!
25//! let xml = response.to_xml();
26//! println!("{}", xml);
27//! ```
28
29pub mod error;
30pub mod fax;
31pub mod messaging;
32pub mod validation;
33pub mod validation_warnings;
34pub mod voice;
35pub mod xml_escape;
36
37pub use error::{Error, Result};
38pub use fax::{FaxResponse, Receive, ReceiveAttributes, ReceiveMediaType, ReceivePageSize};
39pub use messaging::{
40    Body, Media, Message, MessageAttributes, MessagingResponse, Redirect, RedirectAttributes,
41};
42pub use validation::{
43    validate_twiml, validate_twiml_strict, TwiMLValidator, ValidationError, ValidationErrorType,
44};
45pub use voice::VoiceResponse;
46
47/// Common trait for TwiML responses
48pub trait TwiML {
49    /// Convert the TwiML to an XML string
50    fn to_xml(&self) -> String;
51
52    /// Validate the TwiML
53    fn validate(&self) -> Result<Vec<ValidationError>> {
54        validate_twiml(&self.to_xml())
55    }
56
57    /// Validate the TwiML with strict validation
58    fn validate_strict(&self) -> Result<Vec<ValidationError>> {
59        validate_twiml_strict(&self.to_xml())
60    }
61}