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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! # Behold
//! `behold` is a simple library that allows contextual debugging.
#[macro_use]
extern crate lazy_static;

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};

/// The core data structure - stores shared global context and instance specific configuration
#[derive(Clone)]
pub struct Behold {
    /// Context to determine when to speak up
    context: Arc<Mutex<BTreeMap<String, bool>>>,
    /// Determine if this behold instance should produce output
    speak_up: bool,
    /// Suffix used when displaying output
    tag: Option<String>,
}

impl Behold {
    /// Create a new Behold instance
    pub fn new() -> Self {
        BEHOLD.clone()
    }

    /// Set the value of the global Behold context
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().when_context("do-it".to_string()).show("Hello world!".to_string())
    /// ```
    /// Will output nothing.
    /// ```
    /// use behold::behold;
    /// behold().set_context("do-it".to_string(), true);
    /// behold().when_context("do-it".to_string()).show("Hello world!".to_string())
    /// ```
    /// Will produce the output:
    /// ```ignore
    /// "Hello world!"
    /// ```
    pub fn set_context(&self, key: String, value: bool) {
        let context = (*self.context).lock();

        if let Ok(mut context) = context {
            (*context).insert(key, value);
        } else if let Err(err) = context {
            panic!(
                "when_context called on an instance of Behold - mutex already acquired - {:?}!",
                err
            );
        }
    }

    /// Return a Behold instance that appends output with the specified tag
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().tag("apples".to_string()).show("Hello world!".to_string());
    /// ```
    /// Will produce the output:
    /// ```ignore
    /// "Hello world!, apples"
    /// ```
    pub fn tag(&self, tag: String) -> Self {
        Behold {
            context: self.context.clone(),
            speak_up: self.speak_up,
            tag: Some(tag),
        }
    }

    /// Behave just like ```tag``` but accepts a ```&str```
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().tag_str(&"apples").show("Hello world!".to_string());
    /// ```
    /// Will produce the output:
    /// ```ignore
    /// "Hello world!, apples"
    /// ```
    pub fn tag_str(&self, tag: &str) -> Self {
        self.tag(tag.to_string())
    }

    /// Produce a behold instance which can speak up or not, depending on the parameter
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().when(0 % 2 == 1).show("Hello world!".to_string())
    /// ```
    /// Will output nothing.
    /// ```
    /// use behold::behold;
    /// behold().when(0 % 2 == 0).show("Hello world!".to_string())
    /// ```
    /// Will output
    /// ```ignore
    /// "Hello world!, apples"
    /// ```
    pub fn when(&self, speak_up: bool) -> Self {
        Behold {
            context: self.context.clone(),
            speak_up: speak_up,
            tag: self.tag.clone(),
        }
    }

    /// Produce a behold instance which can speak up or not, depending on the specified context
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().when_context("do-it".to_string()).show("Hello world!".to_string())
    /// ```
    /// Will output nothing.
    /// ```
    /// use behold::behold;
    /// behold().set_context("do-it".to_string(), true);
    /// behold().when_context("do-it".to_string()).show("Hello world!".to_string())
    /// ```
    /// Will output
    /// ```ignore
    /// "Hello world!"
    /// ```
    pub fn when_context(&self, key: String) -> Self {
        let speak_up = match (*self.context).lock() {
            Ok(context) => (*context).get(&key).cloned().unwrap_or_default(),
            Err(err) => {
                panic!(
                    "when_context called on an instance of Behold - mutex already acquired - {:?}!",
                    err
                );
            }
        };
        Behold {
            context: self.context.clone(),
            speak_up: speak_up,
            tag: self.tag.clone(),
        }
    }

    /// Print the provided string if this behold instance is configured to speak up
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().show("Hello world!".to_string());
    /// ```
    /// Will produce the output:
    /// ```ignore
    /// "Hello world!"
    /// ```
    pub fn show(&self, msg: String) {
        if self.speak_up {
            if let Some(ref tag) = self.tag {
                println!("{}, {}", msg, tag);
            } else {
                println!("{}", msg);
            }
        }
    }

    /// Behave just like ```show``` but accepts a ```&str```
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().show_str(&"Hello world!");
    /// ```
    /// Will produce the output:
    /// ```ignore
    /// "Hello world!"
    /// ```
    pub fn show_str(&self, msg: &str) {
        self.show(msg.to_string())
    }

    /// Call the provided function if this behold instance is configured to speak up
    /// # Examples
    /// ```
    /// use behold::behold;
    /// behold().call(&|| { println!("Hello world!"); } );
    /// ```
    /// Will output:
    /// ```ignore
    /// "Hello world!"
    /// ```
    /// Whereas the following:
    ///```
    /// use behold::behold;
    /// behold().when(false).call(&|| { println!("Hello world!"); } );
    /// ```
    /// Will output nothing.
    pub fn call(&self, f: &Fn()) {
        if self.speak_up {
            f()
        }
    }
}

/// Convenience function for quickly constructing a behold instance.
///
/// # Examples
///
/// ```rust
/// use behold::behold;
/// behold().show_str("Hello world!");
/// ```
/// Will produce the output:
/// ```ignore
/// "Hello world!"
/// ```
pub fn behold() -> Behold {
    Behold::new()
}

lazy_static! {
    static ref BEHOLD: Behold = {
    	Behold {
	    	context: Arc::new(Mutex::new(BTreeMap::new())),
	    	speak_up: true,
	    	tag: None,
	    }
	};
}


#[cfg(test)]
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));