Skip to main content

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/// systemd `sd_notify` integration (READY=1 / STOPPING=1 / etc.)
71pub mod sd_notify;
72/// A representation of Sōzu's state
73pub mod state;
74/// A writer used for logging
75pub mod writer;
76
77/// Used only when returning errors
78#[derive(Debug)]
79pub enum ObjectKind {
80    Backend,
81    Certificate,
82    Cluster,
83    HttpFrontend,
84    HttpsFrontend,
85    HttpListener,
86    HttpsListener,
87    Listener,
88    TcpCluster,
89    TcpListener,
90    TcpFrontend,
91}
92
93pub trait AsString {
94    fn as_string_or(&self, default: &'static str) -> String;
95}
96
97impl<T: ToString> AsString for Option<T> {
98    fn as_string_or(&self, default: &'static str) -> String {
99        match self {
100            None => default.to_string(),
101            Some(t) => t.to_string(),
102        }
103    }
104}
105
106pub trait AsStr {
107    fn as_str_or(&self, default: &'static str) -> &str;
108}
109
110impl<T: AsRef<str>> AsStr for Option<T> {
111    fn as_str_or(&self, default: &'static str) -> &str {
112        match self {
113            None => default,
114            Some(s) => s.as_ref(),
115        }
116    }
117}