sozu_command_lib/
lib.rs

1//! Tools and types used to communicate with Sōzu.
2
3#[macro_use]
4extern crate serde;
5
6#[macro_use]
7/// custom made logging macros
8pub mod logging;
9/// Custom buffer used for parsing within the Sōzu codebase.
10pub mod buffer;
11/// TLS certificates
12pub mod certificate;
13/// channels used for communication between main process and workers
14pub mod channel;
15/// parse TOML config and generate requests from it
16pub mod config;
17/// parse Requests
18pub mod parser;
19/// Contains Rust types generated by [`prost`](https://docs.rs/prost/latest/prost/)
20/// using the protobuf definition in `command.proto`.
21///
22/// The two main types are `Request` and `Response`.
23///
24/// Because these types were originally defined in Rust, and later adapted for protobuf,
25/// the Rust translation of the protobuf definition looks a bit weird. Instead of having
26/// `Request` being a simple enum with Payload like this:
27///
28/// ```ignore
29/// pub enum Request {
30///     SaveState(String),
31///     LoadState(String),
32///     // ...
33/// }
34/// ```
35///
36/// It is defined with `oneof` in protobuf. and looks like this in Rust:
37///
38/// ```ignore
39/// pub struct Request {
40///     pub request_type: ::core::option::Option<request::RequestType>,
41/// }
42///
43/// pub mod request {
44///     pub enum RequestType {
45///         #[prost(string, tag = "1")]
46///         SaveState(::prost::alloc::string::String),
47///         /// load a state file, given its path
48///         #[prost(string, tag = "2")]
49///         LoadState(::prost::alloc::string::String),
50///     }
51/// }
52/// ```
53///
54/// but can be instantiated this way:
55///
56/// ```ignore
57/// let load_state_request: Request = RequestType::LoadState(path).into();
58/// ```
59///
60/// A bit cumbersome, but it is the only way to benefit from protobuf in Rust.
61pub mod proto;
62/// File descriptor readiness
63pub mod ready;
64/// Helper functions around types received by Sōzu
65pub mod request;
66/// Helper functions around types sent by Sōzu
67pub mod response;
68/// sockets used to pass file descriptors
69pub mod scm_socket;
70/// A representation of Sōzu's state
71pub mod state;
72/// A writer used for logging
73pub mod writer;
74
75/// Used only when returning errors
76#[derive(Debug)]
77pub enum ObjectKind {
78    Backend,
79    Certificate,
80    Cluster,
81    HttpFrontend,
82    HttpsFrontend,
83    HttpListener,
84    HttpsListener,
85    Listener,
86    TcpCluster,
87    TcpListener,
88    TcpFrontend,
89}
90
91pub trait AsString {
92    fn as_string_or(&self, default: &'static str) -> String;
93}
94
95impl<T: ToString> AsString for Option<T> {
96    fn as_string_or(&self, default: &'static str) -> String {
97        match self {
98            None => default.to_string(),
99            Some(t) => t.to_string(),
100        }
101    }
102}
103
104pub trait AsStr {
105    fn as_str_or(&self, default: &'static str) -> &str;
106}
107
108impl<T: AsRef<str>> AsStr for Option<T> {
109    fn as_str_or(&self, default: &'static str) -> &str {
110        match self {
111            None => default,
112            Some(s) => s.as_ref(),
113        }
114    }
115}