1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! 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(),
        }
    }
}