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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#[derive(Default, Debug, Clone)]
/// The configuration that should be used to initialize the
/// system using [`Bastion::init_with`].
///
/// The default behaviors are the following:
/// - All backtraces are shown (see [`Config::show_backtraces`]).
///
/// # Example
///
/// ```rust
/// use bastion::prelude::*;
///
/// fn main() {
///     let config = Config::new().show_backtraces();
///
///     Bastion::init_with(config);
///
///     // You can now use bastion...
///     #
///     # Bastion::start();
///     # Bastion::stop();
///     # Bastion::block_until_stopped();
/// }
/// ```
///
/// [`Bastion::init_with`]: struct.Bastion.html#method.init_with
pub struct Config {
    backtraces: Backtraces,
}

#[derive(PartialEq, Eq, Debug, Clone)]
pub(crate) enum Backtraces {
    /// Shows all backtraces, like an application without
    /// Bastion would.
    Show,
    // TODO: Catch,
    /// Hides all backtraces.
    Hide,
}

impl Config {
    /// Creates a new configuration with the following default
    /// behaviors:
    /// - All backtraces are shown (see [`Config::show_backtraces`]).
    ///
    /// [`Config::show_backtraces`]: #method.show_backtraces
    pub fn new() -> Self {
        Config::default()
    }

    /// Makes Bastion show all backtraces, like an application
    /// without it would. This can be useful when trying to
    /// debug children panicking.
    ///
    /// Note that this is the default behavior.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bastion::prelude::*;
    ///
    /// fn main() {
    ///     let config = Config::new().show_backtraces();
    ///
    ///     Bastion::init_with(config);
    ///
    ///     // You can now use bastion and it will show you the
    ///     // backtraces of panics...
    ///     #
    ///     # Bastion::start();
    ///     # Bastion::stop();
    ///     # Bastion::block_until_stopped();
    /// }
    /// ```
    pub fn show_backtraces(mut self) -> Self {
        self.backtraces = Backtraces::show();
        self
    }

    /// Makes Bastion hide all backtraces.
    ///
    /// Note that the default behavior is to show all backtraces
    /// (see [`Config::show_backtraces`]).
    ///
    /// # Example
    ///
    /// ```rust
    /// use bastion::prelude::*;
    ///
    /// fn main() {
    ///     let config = Config::new().hide_backtraces();
    ///
    ///     Bastion::init_with(config);
    ///
    ///     // You can now use bastion and no panic backtraces
    ///     // will be shown...
    ///     #
    ///     # Bastion::start();
    ///     # Bastion::stop();
    ///     # Bastion::block_until_stopped();
    /// }
    /// ```
    ///
    /// [`Config::show_backtraces`]: #method.show_backtraces
    pub fn hide_backtraces(mut self) -> Self {
        self.backtraces = Backtraces::hide();
        self
    }

    pub(crate) fn backtraces(&self) -> &Backtraces {
        &self.backtraces
    }
}

impl Backtraces {
    fn show() -> Self {
        Backtraces::Show
    }

    fn hide() -> Self {
        Backtraces::Hide
    }

    pub(crate) fn is_hide(&self) -> bool {
        self == &Backtraces::Hide
    }
}

impl Default for Backtraces {
    fn default() -> Self {
        Backtraces::Show
    }
}