//! Tools and types used to communicate with Sōzu.

#[macro_use]
extern crate serde;

#[macro_use]
/// custom made logging macros
pub mod logging;
/// Custom buffer used for parsing within the Sōzu codebase.
pub mod buffer;
/// TLS certificates
pub mod certificate;
/// channels used for communication between main process and workers
pub mod channel;
/// parse TOML config and generate requests from it
pub mod config;
/// parse Requests
pub mod parser;
/// Contains Rust types generated by [`prost`](https://docs.rs/prost/latest/prost/)
/// using the protobuf definition in `command.proto`.
///
/// The two main types are `Request` and `Response`.
///
/// Because these types were originally defined in Rust, and later adapted for protobuf,
/// the Rust translation of the protobuf definition looks a bit weird. Instead of having
/// `Request` being a simple enum with Payload like this:
///
/// ```ignore
/// pub enum Request {
///     SaveState(String),
///     LoadState(String),
///     // ...
/// }
/// ```
///
/// It is defined with `oneof` in protobuf. and looks like this in Rust:
///
/// ```ignore
/// pub struct Request {
///     pub request_type: ::core::option::Option<request::RequestType>,
/// }
///
/// pub mod request {
///     pub enum RequestType {
///         #[prost(string, tag = "1")]
///         SaveState(::prost::alloc::string::String),
///         /// load a state file, given its path
///         #[prost(string, tag = "2")]
///         LoadState(::prost::alloc::string::String),
///     }
/// }
/// ```
///
/// but can be instantiated this way:
///
/// ```ignore
/// let load_state_request: Request = RequestType::LoadState(path).into();
/// ```
///
/// A bit cumbersome, but it is the only way to benefit from protobuf in Rust.
pub mod proto;
/// File descriptor readiness
pub mod ready;
/// Helper functions around types received by Sōzu
pub mod request;
/// Helper functions around types sent by Sōzu
pub mod response;
/// sockets used to pass file descriptors
pub mod scm_socket;
/// A representation of Sōzu's state
pub mod state;
/// A writer used for logging
pub mod writer;

/// Used only when returning errors
#[derive(Debug)]
pub enum ObjectKind {
    Backend,
    Certificate,
    Cluster,
    HttpFrontend,
    HttpsFrontend,
    HttpListener,
    HttpsListener,
    Listener,
    TcpCluster,
    TcpListener,
    TcpFrontend,
}

pub trait AsString {
    fn as_string_or(&self, default: &'static str) -> String;
}

impl<T: ToString> AsString for Option<T> {
    fn as_string_or(&self, default: &'static str) -> String {
        match self {
            None => default.to_string(),
            Some(t) => t.to_string(),
        }
    }
}

pub trait AsStr {
    fn as_str_or(&self, default: &'static str) -> &str;
}

impl<T: AsRef<str>> AsStr for Option<T> {
    fn as_str_or(&self, default: &'static str) -> &str {
        match self {
            None => default,
            Some(s) => s.as_ref(),
        }
    }
}
