#![feature(error_generic_member_access, provide_any)]

pub mod error;
pub mod payload;

#[cfg(feature = "serde")]
pub mod serde;

#[cfg(test)]
mod test;

pub use self::{
    error::Error,
    payload::Payload,
};

#[derive(Debug, Clone, PartialEq)]
pub struct SeString(pub Vec<Payload>);

impl SeString {
    pub fn parse<B: AsRef<[u8]>>(bytes: B) -> Result<Self, Error> {
        let payloads = Payload::parse(bytes)?;
        Ok(Self(payloads))
    }

    pub fn encode(&self) -> Vec<u8> {
        self.0
            .iter()
            .flat_map(|p| p.encode())
            .collect()
    }

    pub fn text(&self) -> String {
        self.0.iter()
            .flat_map(|payload| {
                match payload {
                    Payload::Text(t) => Some(&*t.0),
                    _ => None,
                }
            })
            .collect()
    }
}
