bastion/config.rs
1#[derive(Default, Debug, Clone)]
2/// The configuration that should be used to initialize the
3/// system using [`Bastion::init_with`].
4///
5/// The default behaviors are the following:
6/// - All backtraces are shown (see [`Config::show_backtraces`]).
7///
8/// # Example
9///
10/// ```rust
11/// use bastion::prelude::*;
12///
13/// # #[cfg(feature = "tokio-runtime")]
14/// # #[tokio::main]
15/// # async fn main() {
16/// # run();
17/// # }
18/// #
19/// # #[cfg(not(feature = "tokio-runtime"))]
20/// # fn main() {
21/// # run();
22/// # }
23/// #
24/// # fn run() {
25/// let config = Config::new().show_backtraces();
26///
27/// Bastion::init_with(config);
28///
29/// // You can now use bastion...
30/// #
31/// # Bastion::start();
32/// # Bastion::stop();
33/// # Bastion::block_until_stopped();
34/// # }
35/// ```
36///
37/// [`Bastion::init_with`]: crate::Bastion::init_with
38pub struct Config {
39 backtraces: Backtraces,
40}
41
42#[derive(PartialEq, Eq, Debug, Clone)]
43pub(crate) enum Backtraces {
44 /// Shows all backtraces, like an application without
45 /// Bastion would.
46 Show,
47 // TODO: Catch,
48 /// Hides all backtraces.
49 Hide,
50}
51
52impl Config {
53 /// Creates a new configuration with the following default
54 /// behaviors:
55 /// - All backtraces are shown (see [`Config::show_backtraces`]).
56 pub fn new() -> Self {
57 Config::default()
58 }
59
60 /// Makes Bastion show all backtraces, like an application
61 /// without it would. This can be useful when trying to
62 /// debug children panicking.
63 ///
64 /// Note that this is the default behavior.
65 ///
66 /// # Example
67 ///
68 /// ```rust
69 /// use bastion::prelude::*;
70 ///
71 /// # #[cfg(feature = "tokio-runtime")]
72 /// # #[tokio::main]
73 /// # async fn main() {
74 /// # run();
75 /// # }
76 /// #
77 /// # #[cfg(not(feature = "tokio-runtime"))]
78 /// # fn main() {
79 /// # run();
80 /// # }
81 /// #
82 /// # fn run() {
83 /// let config = Config::new().show_backtraces();
84 ///
85 /// Bastion::init_with(config);
86 ///
87 /// // You can now use bastion and it will show you the
88 /// // backtraces of panics...
89 /// #
90 /// # Bastion::start();
91 /// # Bastion::stop();
92 /// # Bastion::block_until_stopped();
93 /// # }
94 /// ```
95 pub fn show_backtraces(mut self) -> Self {
96 self.backtraces = Backtraces::show();
97 self
98 }
99
100 /// Makes Bastion hide all backtraces.
101 ///
102 /// Note that the default behavior is to show all backtraces
103 /// (see [`Config::show_backtraces`]).
104 ///
105 /// # Example
106 ///
107 /// ```rust
108 /// use bastion::prelude::*;
109 ///
110 /// # #[cfg(feature = "tokio-runtime")]
111 /// # #[tokio::main]
112 /// # async fn main() {
113 /// # run();
114 /// # }
115 /// #
116 /// # #[cfg(not(feature = "tokio-runtime"))]
117 /// # fn main() {
118 /// # run();
119 /// # }
120 /// #
121 /// # fn run() {
122 /// let config = Config::new().hide_backtraces();
123 ///
124 /// Bastion::init_with(config);
125 ///
126 /// // You can now use bastion and no panic backtraces
127 /// // will be shown...
128 /// #
129 /// # Bastion::start();
130 /// # Bastion::stop();
131 /// # Bastion::block_until_stopped();
132 /// # }
133 /// ```
134 pub fn hide_backtraces(mut self) -> Self {
135 self.backtraces = Backtraces::hide();
136 self
137 }
138
139 pub(crate) fn backtraces(&self) -> &Backtraces {
140 &self.backtraces
141 }
142}
143
144impl Backtraces {
145 fn show() -> Self {
146 Backtraces::Show
147 }
148
149 fn hide() -> Self {
150 Backtraces::Hide
151 }
152
153 pub(crate) fn is_hide(&self) -> bool {
154 self == &Backtraces::Hide
155 }
156}
157
158impl Default for Backtraces {
159 fn default() -> Self {
160 Backtraces::Show
161 }
162}