Skip to main content

to_be/
lib.rs

1// lib.rs - to-be API
2
3
4#[rustfmt::skip]
5mod constants {
6    #![allow(clippy::redundant_static_lifetimes)]
7
8    // NOTE: `FALSEY_PRECISE_STRINGS` and `TRUEY_PRECISE_STRINGS` must be in sorted order as
9    // they are consumed in binary search; the others in most-likely order.
10
11    pub(super) const FALSEY_PRECISE_STRINGS : &'static [&'static str; 10] = &[
12        "0",
13        "FALSE",
14        "False",
15        "NO",
16        "No",
17        "OFF",
18        "Off",
19        "false",
20        "no",
21        "off",
22    ];
23
24    pub(super) const TRUEY_PRECISE_STRINGS : &'static [&'static str; 10] = &[
25        "1",
26        "ON",
27        "On",
28        "TRUE",
29        "True",
30        "YES",
31        "Yes",
32        "on",
33        "true",
34        "yes",
35    ];
36
37    pub(super) const FALSEY_LOWERCASE_STRINGS : &'static [&'static str; 4] = &[
38        "false",
39        "no",
40        "off",
41        "0",
42    ];
43
44    pub(super) const TRUEY_LOWERCASE_STRINGS : &'static [&'static str; 4] = &[
45        "true",
46        "yes",
47        "on",
48        "1",
49    ];
50}
51
52
53/// Directs custom truthyness behaviour.
54#[derive(Clone)]
55#[derive(Debug)]
56pub enum Terms<'a> {
57    /// Use the built-in comparison strings.
58    Default,
59    /// Use the given `*precise_strings` and, optionally, the given
60    /// `*lower_strings` to evaluate the truthyness of a given string.
61    Strings {
62        falsey_precise_strings :   &'a [&'a str],
63        falsey_lowercase_strings : &'a [&'a str],
64        truey_precise_strings :    &'a [&'a str],
65        truey_lowercase_strings :  &'a [&'a str],
66    },
67}
68
69fn string_is_truthy_against_(
70    s : &str,
71    sorted_precise_strings : &[&str],
72    lowercase_strings : &[&str],
73) -> bool {
74    let s = s.trim();
75
76    if sorted_precise_strings.binary_search(&s).is_ok() {
77        true
78    } else {
79        let l = s.to_ascii_lowercase();
80
81        lowercase_strings.iter().any(|&f| f == l)
82    }
83}
84
85fn string_is_truthy_with_(
86    s : &str,
87    terms : Terms,
88    stock_falsey_sorted_precise_strings : &[&str],
89    stock_falsey_lowercase_strings : &[&str],
90    stock_truey_sorted_precise_strings : &[&str],
91    stock_truey_lowercase_strings : &[&str],
92) -> Option<bool> {
93    let s = s.trim();
94
95    match terms {
96        Terms::Default => {
97            if stock_falsey_sorted_precise_strings.binary_search(&s).is_ok() {
98                return Some(false);
99            }
100            if stock_truey_sorted_precise_strings.binary_search(&s).is_ok() {
101                return Some(true);
102            }
103        },
104        Terms::Strings {
105            falsey_precise_strings,
106            truey_precise_strings,
107            ..
108        } => {
109            if falsey_precise_strings.contains(&s) {
110                return Some(false);
111            }
112            if truey_precise_strings.contains(&s) {
113                return Some(true);
114            }
115        },
116    };
117
118    let l = s.to_ascii_lowercase();
119    let (falsey_lowercase_strings, truey_lowercase_strings) = match terms {
120        Terms::Default => (stock_falsey_lowercase_strings, stock_truey_lowercase_strings),
121        Terms::Strings {
122            falsey_lowercase_strings,
123            truey_lowercase_strings,
124            ..
125        } => (falsey_lowercase_strings, truey_lowercase_strings),
126    };
127
128    if falsey_lowercase_strings.iter().any(|&f| f == l) {
129        return Some(false);
130    }
131    if truey_lowercase_strings.iter().any(|&f| f == l) {
132        return Some(true);
133    }
134
135    None
136}
137
138/// Obtain the stock term strings of the library.
139///
140/// This may be handy when you want to, say, provide your own "truey" term
141/// strings but rely on the stock "falsey" term strings.
142pub fn stock_term_strings() -> Terms<'static> {
143    Terms::Strings {
144        falsey_precise_strings :   constants::FALSEY_PRECISE_STRINGS,
145        falsey_lowercase_strings : constants::FALSEY_LOWERCASE_STRINGS,
146        truey_precise_strings :    constants::TRUEY_PRECISE_STRINGS,
147        truey_lowercase_strings :  constants::TRUEY_LOWERCASE_STRINGS,
148    }
149}
150
151/// Indicates that the given string, when trimmed, is deemed as "falsey".
152///
153/// # Note:
154/// It is NOT guaranteed that `string_is_falsey(x) == !string_is_truey(x)`.
155pub fn string_is_falsey(s : &str) -> bool {
156    string_is_truthy_against_(
157        s,
158        constants::FALSEY_PRECISE_STRINGS,
159        constants::FALSEY_LOWERCASE_STRINGS,
160    )
161}
162
163/// Indicates that the given string, when trimmed, is deemed as "truey".
164///
165/// # Note:
166/// It is NOT guaranteed that `string_is_falsey(x) == !string_is_truey(x)`.
167///
168/// # Returns:
169/// - `None` - string is not classified as "truthy";
170/// - `Some(false)` - string (is classified as "truthy" and) is deemed
171///   "falsey";
172/// - `Some(true)` - string (is classified as "truthy" and) is deemed
173///   "truey";
174pub fn string_is_truey(s : &str) -> bool {
175    string_is_truthy_against_(
176        s,
177        constants::TRUEY_PRECISE_STRINGS,
178        constants::TRUEY_LOWERCASE_STRINGS,
179    )
180}
181
182/// Indicates whether the given string is "truthy" and, if so, whether it is
183/// "truey" or "falsey".
184///
185/// # Returns:
186/// - `None` - string is not classified as "truthy";
187/// - `Some(false)` - string (is classified as "truthy" and) is deemed
188///   "falsey";
189/// - `Some(true)` - string (is classified as "truthy" and) is deemed
190///   "truey";
191pub fn string_is_truthy(s : &str) -> Option<bool> {
192    string_is_truthy_with_(
193        s,
194        Terms::Default,
195        constants::FALSEY_PRECISE_STRINGS,
196        constants::FALSEY_LOWERCASE_STRINGS,
197        constants::TRUEY_PRECISE_STRINGS,
198        constants::TRUEY_LOWERCASE_STRINGS,
199    )
200}
201
202/// Indicates whether the instance can be classed as "truthy" when evaluated
203/// against the given terms strings.
204pub fn string_is_truthy_with(
205    s : &str,
206    terms : Terms,
207) -> Option<bool> {
208    string_is_truthy_with_(
209        s,
210        terms,
211        constants::FALSEY_PRECISE_STRINGS,
212        constants::FALSEY_LOWERCASE_STRINGS,
213        constants::TRUEY_PRECISE_STRINGS,
214        constants::TRUEY_LOWERCASE_STRINGS,
215    )
216}
217
218/// Trait that provides truthy attributes for an implementing type.
219pub trait Truthy {
220    /// Indicates whether the instance can be classed as "falsey".
221    fn is_falsey(&self) -> bool {
222        Some(false) == self.is_truthy()
223    }
224    /// Indicates whether the instance can be classed as "truey".
225    fn is_truey(&self) -> bool {
226        Some(true) == self.is_truthy()
227    }
228    /// Indicates whether the instance can be classed as "truthy", and, if
229    /// so, whether it is "truey" or "falsey".
230    fn is_truthy(&self) -> Option<bool>;
231}
232
233/// Specialisation of [Truthy] for type `T` for any type that implements
234/// [AsStr].
235#[cfg(feature = "implement-Truthy-for-AsStr")]
236#[allow(non_snake_case)]
237mod implement_Truthy_for_AsStr {
238    use super::Truthy;
239    use base_traits::AsStr;
240
241    impl<T> Truthy for T
242    where
243        T : AsStr,
244    {
245        fn is_truthy(&self) -> Option<bool> {
246            super::string_is_truthy(self.as_str())
247        }
248    }
249}
250
251#[cfg(feature = "implement-Truthy-for-bool")]
252#[allow(non_snake_case)]
253mod implement_Truthy_for_bool {
254    use super::Truthy;
255
256    impl Truthy for bool {
257        fn is_truthy(&self) -> Option<bool> {
258            Some(*self)
259        }
260    }
261
262    impl Truthy for &bool {
263        fn is_truthy(&self) -> Option<bool> {
264            Some(**self)
265        }
266    }
267
268    impl Truthy for Option<bool> {
269        fn is_truthy(&self) -> Option<bool> {
270            *self
271        }
272    }
273
274    impl Truthy for &Option<bool> {
275        fn is_truthy(&self) -> Option<bool> {
276            **self
277        }
278    }
279
280    impl Truthy for Option<&bool> {
281        fn is_truthy(&self) -> Option<bool> {
282            self.map(|&b| b)
283        }
284    }
285
286    impl Truthy for &Option<&bool> {
287        fn is_truthy(&self) -> Option<bool> {
288            self.map(|&b| b)
289        }
290    }
291}
292
293#[cfg(feature = "implement-Truthy-for-str")]
294#[allow(non_snake_case)]
295mod implement_Truthy_for_str {
296    use super::Truthy;
297
298    impl Truthy for &str {
299        fn is_truthy(&self) -> Option<bool> {
300            super::string_is_truthy(self)
301        }
302    }
303
304    impl Truthy for &&str {
305        fn is_truthy(&self) -> Option<bool> {
306            super::string_is_truthy(*self)
307        }
308    }
309}
310
311#[cfg(feature = "implement-Truthy-for-CStr")]
312#[allow(non_snake_case)]
313mod implement_Truthy_for_CStr {
314    use super::Truthy;
315    use std::ffi::CStr;
316
317    impl Truthy for CStr {
318        fn is_truthy(&self) -> Option<bool> {
319            match self.to_str() {
320                Err(_) => None,
321                Ok(c) => {
322                    super::string_is_truthy(c)
323                }
324            }
325        }
326    }
327
328    impl Truthy for &CStr {
329        fn is_truthy(&self) -> Option<bool> {
330            match self.to_str() {
331                Err(_) => None,
332                Ok(c) => {
333                    super::string_is_truthy(c)
334                }
335            }
336        }
337    }
338}
339
340#[cfg(feature = "implement-Truthy-for-CString")]
341#[allow(non_snake_case)]
342mod implement_Truthy_for_CString {
343    use super::Truthy;
344    use std::ffi::CString;
345
346    impl Truthy for CString {
347        fn is_truthy(&self) -> Option<bool> {
348            match self.to_str() {
349                Err(_) => None,
350                Ok(c) => {
351                    super::string_is_truthy(c)
352                }
353            }
354        }
355    }
356
357    impl Truthy for &CString {
358        fn is_truthy(&self) -> Option<bool> {
359            match self.to_str() {
360                Err(_) => None,
361                Ok(c) => {
362                    super::string_is_truthy(c)
363                }
364            }
365        }
366    }
367}
368
369#[cfg(feature = "implement-Truthy-for-OsStr")]
370#[allow(non_snake_case)]
371mod implement_Truthy_for_OsStr {
372    use super::Truthy;
373    use std::ffi::OsStr;
374
375    impl Truthy for OsStr {
376        fn is_truthy(&self) -> Option<bool> {
377            super::os_string_is_truthy(self)
378        }
379    }
380
381    impl Truthy for &OsStr {
382        fn is_truthy(&self) -> Option<bool> {
383            super::os_string_is_truthy(*self)
384        }
385    }
386}
387
388#[cfg(feature = "implement-Truthy-for-OsString")]
389#[allow(non_snake_case)]
390mod implement_Truthy_for_OsString {
391    use super::Truthy;
392    use std::ffi::OsString;
393
394    impl Truthy for OsString {
395        fn is_truthy(&self) -> Option<bool> {
396            match self.to_str() {
397                None => None,
398                Some(c) => {
399                    super::string_is_truthy(c)
400                }
401            }
402        }
403    }
404
405    impl Truthy for &OsString {
406        fn is_truthy(&self) -> Option<bool> {
407            match self.to_str() {
408                None => None,
409                Some(c) => {
410                    super::string_is_truthy(c)
411                }
412            }
413        }
414    }
415}
416
417#[cfg(feature = "implement-Truthy-for-String")]
418#[allow(non_snake_case)]
419mod implement_Truthy_for_String {
420    use super::Truthy;
421
422    impl Truthy for String {
423        fn is_truthy(&self) -> Option<bool> {
424            super::string_is_truthy(self.as_str())
425        }
426    }
427
428    impl Truthy for &String {
429        fn is_truthy(&self) -> Option<bool> {
430            super::string_is_truthy(self.as_str())
431        }
432    }
433}
434
435
436#[cfg(test)]
437mod tests {
438    #![allow(non_snake_case)]
439
440
441    mod test_API {
442        #![allow(non_snake_case)]
443
444        use super::super::{
445            string_is_falsey,
446            string_is_truey,
447            string_is_truthy,
448            string_is_truthy_with,
449            Terms,
450        };
451
452
453        #[test]
454        fn TEST_string_is_falsey_1() {
455            assert_eq!(false, string_is_falsey(""));
456
457            assert_eq!(true, string_is_falsey("0"));
458            assert_eq!(true, string_is_falsey("false"));
459            assert_eq!(true, string_is_falsey(" FALSE"));
460            assert_eq!(true, string_is_falsey("False"));
461            assert_eq!(true, string_is_falsey("FaLSe"));
462            assert_eq!(true, string_is_falsey("no"));
463            assert_eq!(true, string_is_falsey("No "));
464            assert_eq!(true, string_is_falsey("NO"));
465            assert_eq!(true, string_is_falsey(" Off "));
466            assert_eq!(true, string_is_falsey("off"));
467            assert_eq!(true, string_is_falsey("OFF"));
468
469            assert_eq!(false, string_is_falsey("1"));
470            assert_eq!(false, string_is_falsey("true"));
471            assert_eq!(false, string_is_falsey("TRUE"));
472            assert_eq!(false, string_is_falsey("True"));
473            assert_eq!(false, string_is_falsey("tRuE"));
474            assert_eq!(false, string_is_falsey("yes"));
475            assert_eq!(false, string_is_falsey(" YES"));
476            assert_eq!(false, string_is_falsey("Yes   "));
477            assert_eq!(false, string_is_falsey("yEs"));
478        }
479
480        #[test]
481        fn TEST_string_is_truey_1() {
482            assert_eq!(false, string_is_truey(""));
483
484            assert_eq!(false, string_is_truey("0"));
485            assert_eq!(false, string_is_truey("false"));
486            assert_eq!(false, string_is_truey(" FALSE"));
487            assert_eq!(false, string_is_truey("False"));
488            assert_eq!(false, string_is_truey("FaLSe"));
489            assert_eq!(false, string_is_truey("no"));
490            assert_eq!(false, string_is_truey("No "));
491            assert_eq!(false, string_is_truey("NO"));
492            assert_eq!(false, string_is_truey(" Off "));
493            assert_eq!(false, string_is_truey("off"));
494            assert_eq!(false, string_is_truey("OFF"));
495
496            assert_eq!(true, string_is_truey("1"));
497            assert_eq!(true, string_is_truey("true"));
498            assert_eq!(true, string_is_truey("TRUE"));
499            assert_eq!(true, string_is_truey("True"));
500            assert_eq!(true, string_is_truey("tRuE"));
501            assert_eq!(true, string_is_truey("yes"));
502            assert_eq!(true, string_is_truey(" YES"));
503            assert_eq!(true, string_is_truey("Yes   "));
504            assert_eq!(true, string_is_truey("yEs"));
505        }
506
507        #[test]
508        fn TEST_string_is_truthy_1() {
509            assert_eq!(None, string_is_truthy(""));
510
511            assert_eq!(None, string_is_truthy("Nyet"));
512            assert_eq!(None, string_is_truthy("NYET"));
513            assert_eq!(None, string_is_truthy("nyET"));
514            assert_eq!(None, string_is_truthy("nope"));
515            assert_eq!(None, string_is_truthy("Nope"));
516            assert_eq!(None, string_is_truthy("NOPE"));
517
518            assert_eq!(None, string_is_truthy("Da"));
519            assert_eq!(None, string_is_truthy("DA"));
520            assert_eq!(None, string_is_truthy("dA"));
521            assert_eq!(None, string_is_truthy("yup"));
522            assert_eq!(None, string_is_truthy("Yup"));
523            assert_eq!(None, string_is_truthy("yUp"));
524
525            assert_eq!(Some(false), string_is_truthy("0"));
526            assert_eq!(Some(false), string_is_truthy("false"));
527            assert_eq!(Some(false), string_is_truthy(" FALSE"));
528            assert_eq!(Some(false), string_is_truthy("False"));
529            assert_eq!(Some(false), string_is_truthy("FaLSe"));
530            assert_eq!(Some(false), string_is_truthy("no"));
531            assert_eq!(Some(false), string_is_truthy("No "));
532            assert_eq!(Some(false), string_is_truthy("NO"));
533            assert_eq!(Some(false), string_is_truthy(" Off "));
534            assert_eq!(Some(false), string_is_truthy("off"));
535            assert_eq!(Some(false), string_is_truthy("OFF"));
536
537            assert_eq!(Some(true), string_is_truthy("1"));
538            assert_eq!(Some(true), string_is_truthy("true"));
539            assert_eq!(Some(true), string_is_truthy("TRUE"));
540            assert_eq!(Some(true), string_is_truthy("True"));
541            assert_eq!(Some(true), string_is_truthy("tRuE"));
542            assert_eq!(Some(true), string_is_truthy("yes"));
543            assert_eq!(Some(true), string_is_truthy(" YES"));
544            assert_eq!(Some(true), string_is_truthy("Yes   "));
545            assert_eq!(Some(true), string_is_truthy("yEs"));
546        }
547
548        #[test]
549        fn TEST_string_is_truthy_with_1() {
550            #[rustfmt::skip]
551            const TRUEY_PRECISE_STRINGS : &[&str] = &[
552                "Da",
553                "YUP",
554                "Yup",
555            ];
556            #[rustfmt::skip]
557            const TRUEY_LOWERCASE_STRINGS : &[&str] = &[
558                "da",
559                "yup",
560            ];
561            #[rustfmt::skip]
562            const FALSEY_PRECISE_STRINGS : &[&str] = &[
563                "Nyet",
564                "Nope",
565            ];
566            #[rustfmt::skip]
567            const FALSEY_LOWERCASE_STRINGS : &[&str] = &[
568                "nyet",
569                "nope",
570            ];
571
572            let terms = Terms::Strings {
573                falsey_precise_strings :   FALSEY_PRECISE_STRINGS,
574                falsey_lowercase_strings : FALSEY_LOWERCASE_STRINGS,
575                truey_precise_strings :    TRUEY_PRECISE_STRINGS,
576                truey_lowercase_strings :  TRUEY_LOWERCASE_STRINGS,
577            };
578
579            assert_eq!(Some(false), string_is_truthy_with("Nyet", terms.clone()));
580            assert_eq!(Some(false), string_is_truthy_with("NYET", terms.clone()));
581            assert_eq!(Some(false), string_is_truthy_with("nyET", terms.clone()));
582            assert_eq!(Some(false), string_is_truthy_with("nope", terms.clone()));
583            assert_eq!(Some(false), string_is_truthy_with("Nope", terms.clone()));
584            assert_eq!(Some(false), string_is_truthy_with("NOPE", terms.clone()));
585
586            assert_eq!(Some(true), string_is_truthy_with("Da", terms.clone()));
587            assert_eq!(Some(true), string_is_truthy_with("DA", terms.clone()));
588            assert_eq!(Some(true), string_is_truthy_with("dA", terms.clone()));
589            assert_eq!(Some(true), string_is_truthy_with("yup", terms.clone()));
590            assert_eq!(Some(true), string_is_truthy_with("Yup", terms.clone()));
591            assert_eq!(Some(true), string_is_truthy_with("yUp", terms.clone()));
592
593            assert_eq!(None, string_is_truthy_with("", terms.clone()));
594
595            assert_eq!(None, string_is_truthy_with("0", terms.clone()));
596            assert_eq!(None, string_is_truthy_with("false", terms.clone()));
597            assert_eq!(None, string_is_truthy_with(" FALSE", terms.clone()));
598            assert_eq!(None, string_is_truthy_with("False", terms.clone()));
599            assert_eq!(None, string_is_truthy_with("FaLSe", terms.clone()));
600            assert_eq!(None, string_is_truthy_with("no", terms.clone()));
601            assert_eq!(None, string_is_truthy_with("No ", terms.clone()));
602            assert_eq!(None, string_is_truthy_with("NO", terms.clone()));
603            assert_eq!(None, string_is_truthy_with(" Off ", terms.clone()));
604            assert_eq!(None, string_is_truthy_with("off", terms.clone()));
605            assert_eq!(None, string_is_truthy_with("OFF", terms.clone()));
606
607            assert_eq!(None, string_is_truthy_with("1", terms.clone()));
608            assert_eq!(None, string_is_truthy_with("true", terms.clone()));
609            assert_eq!(None, string_is_truthy_with("TRUE", terms.clone()));
610            assert_eq!(None, string_is_truthy_with("True", terms.clone()));
611            assert_eq!(None, string_is_truthy_with("tRuE", terms.clone()));
612            assert_eq!(None, string_is_truthy_with("yes", terms.clone()));
613            assert_eq!(None, string_is_truthy_with(" YES", terms.clone()));
614            assert_eq!(None, string_is_truthy_with("Yes   ", terms.clone()));
615            assert_eq!(None, string_is_truthy_with("yEs", terms.clone()));
616        }
617    }
618
619    mod test_Truthy {
620        #![allow(non_snake_case)]
621
622        #[cfg(any(
623            feature = "implement-Truthy-for-AsStr",
624            feature = "implement-Truthy-for-String",
625            feature = "implement-Truthy-for-bool",
626            feature = "implement-Truthy-for-str",
627        ))]
628        use super::super::Truthy as _;
629        #[cfg(feature = "implement-Truthy-for-CString")]
630        use std::ffi::CString;
631        #[cfg(feature = "implement-Truthy-for-OsString")]
632        use std::ffi::OsString;
633
634
635        #[cfg(feature = "implement-Truthy-for-CString")]
636        #[test]
637        fn TEST_CString_Truthy() {
638            // is_falsey
639            {
640                assert_eq!(false, CString::new("").unwrap().is_falsey());
641                assert_eq!(false, CString::new("Copyright ©").unwrap().is_falsey());
642
643                assert_eq!(true, CString::new("0").unwrap().is_falsey());
644                assert_eq!(true, CString::new("false").unwrap().is_falsey());
645                assert_eq!(true, CString::new(" FALSE").unwrap().is_falsey());
646                assert_eq!(true, CString::new("False").unwrap().is_falsey());
647                assert_eq!(true, CString::new("FaLSe").unwrap().is_falsey());
648                assert_eq!(true, CString::new("no").unwrap().is_falsey());
649                assert_eq!(true, CString::new("No ").unwrap().is_falsey());
650                assert_eq!(true, CString::new("NO").unwrap().is_falsey());
651                assert_eq!(true, CString::new(" Off ").unwrap().is_falsey());
652                assert_eq!(true, CString::new("off").unwrap().is_falsey());
653                assert_eq!(true, CString::new("OFF").unwrap().is_falsey());
654
655                assert_eq!(false, CString::new("1").unwrap().is_falsey());
656                assert_eq!(false, CString::new("true").unwrap().is_falsey());
657                assert_eq!(false, CString::new("TRUE").unwrap().is_falsey());
658                assert_eq!(false, CString::new("True").unwrap().is_falsey());
659                assert_eq!(false, CString::new("tRuE").unwrap().is_falsey());
660                assert_eq!(false, CString::new("yes").unwrap().is_falsey());
661                assert_eq!(false, CString::new(" YES").unwrap().is_falsey());
662                assert_eq!(false, CString::new("Yes   ").unwrap().is_falsey());
663                assert_eq!(false, CString::new("yEs").unwrap().is_falsey());
664            }
665
666            // is_truey
667            {
668                assert_eq!(false, CString::new("").unwrap().is_truey());
669                assert_eq!(false, CString::new("Copyright ©").unwrap().is_truey());
670
671                assert_eq!(false, CString::new("0").unwrap().is_truey());
672                assert_eq!(false, CString::new("false").unwrap().is_truey());
673                assert_eq!(false, CString::new(" FALSE").unwrap().is_truey());
674                assert_eq!(false, CString::new("False").unwrap().is_truey());
675                assert_eq!(false, CString::new("FaLSe").unwrap().is_truey());
676                assert_eq!(false, CString::new("no").unwrap().is_truey());
677                assert_eq!(false, CString::new("No ").unwrap().is_truey());
678                assert_eq!(false, CString::new("NO").unwrap().is_truey());
679                assert_eq!(false, CString::new(" Off ").unwrap().is_truey());
680                assert_eq!(false, CString::new("off").unwrap().is_truey());
681                assert_eq!(false, CString::new("OFF").unwrap().is_truey());
682
683                assert_eq!(true, CString::new("1").unwrap().is_truey());
684                assert_eq!(true, CString::new("true").unwrap().is_truey());
685                assert_eq!(true, CString::new("TRUE").unwrap().is_truey());
686                assert_eq!(true, CString::new("True").unwrap().is_truey());
687                assert_eq!(true, CString::new("tRuE").unwrap().is_truey());
688                assert_eq!(true, CString::new("yes").unwrap().is_truey());
689                assert_eq!(true, CString::new(" YES").unwrap().is_truey());
690                assert_eq!(true, CString::new("Yes   ").unwrap().is_truey());
691                assert_eq!(true, CString::new("yEs").unwrap().is_truey());
692            }
693
694            // is_truthy
695            {
696                assert_eq!(None, CString::new("").unwrap().is_truthy());
697                assert_eq!(None, CString::new("Copyright ©").unwrap().is_truthy());
698
699                assert_eq!(Some(false), CString::new("0").unwrap().is_truthy());
700                assert_eq!(Some(false), CString::new("false").unwrap().is_truthy());
701                assert_eq!(Some(false), CString::new(" FALSE").unwrap().is_truthy());
702                assert_eq!(Some(false), CString::new("False").unwrap().is_truthy());
703                assert_eq!(Some(false), CString::new("FaLSe").unwrap().is_truthy());
704                assert_eq!(Some(false), CString::new("no").unwrap().is_truthy());
705                assert_eq!(Some(false), CString::new("No ").unwrap().is_truthy());
706                assert_eq!(Some(false), CString::new("NO").unwrap().is_truthy());
707                assert_eq!(Some(false), CString::new(" Off ").unwrap().is_truthy());
708                assert_eq!(Some(false), CString::new("off").unwrap().is_truthy());
709                assert_eq!(Some(false), CString::new("OFF").unwrap().is_truthy());
710
711                assert_eq!(Some(true), CString::new("1").unwrap().is_truthy());
712                assert_eq!(Some(true), CString::new("true").unwrap().is_truthy());
713                assert_eq!(Some(true), CString::new("TRUE").unwrap().is_truthy());
714                assert_eq!(Some(true), CString::new("True").unwrap().is_truthy());
715                assert_eq!(Some(true), CString::new("tRuE").unwrap().is_truthy());
716                assert_eq!(Some(true), CString::new("yes").unwrap().is_truthy());
717                assert_eq!(Some(true), CString::new(" YES").unwrap().is_truthy());
718                assert_eq!(Some(true), CString::new("Yes   ").unwrap().is_truthy());
719                assert_eq!(Some(true), CString::new("yEs").unwrap().is_truthy());
720            }
721        }
722
723        #[cfg(feature = "implement-Truthy-for-OsString")]
724        #[test]
725        fn TEST_OsString_Truthy() {
726            // is_falsey
727            {
728                assert_eq!(false, OsString::from("").is_falsey());
729
730                assert_eq!(true, OsString::from("0").is_falsey());
731                assert_eq!(true, OsString::from("false").is_falsey());
732                assert_eq!(true, OsString::from(" FALSE").is_falsey());
733                assert_eq!(true, OsString::from("False").is_falsey());
734                assert_eq!(true, OsString::from("FaLSe").is_falsey());
735                assert_eq!(true, OsString::from("no").is_falsey());
736                assert_eq!(true, OsString::from("No ").is_falsey());
737                assert_eq!(true, OsString::from("NO").is_falsey());
738                assert_eq!(true, OsString::from(" Off ").is_falsey());
739                assert_eq!(true, OsString::from("off").is_falsey());
740                assert_eq!(true, OsString::from("OFF").is_falsey());
741
742                assert_eq!(false, OsString::from("1").is_falsey());
743                assert_eq!(false, OsString::from("true").is_falsey());
744                assert_eq!(false, OsString::from("TRUE").is_falsey());
745                assert_eq!(false, OsString::from("True").is_falsey());
746                assert_eq!(false, OsString::from("tRuE").is_falsey());
747                assert_eq!(false, OsString::from("yes").is_falsey());
748                assert_eq!(false, OsString::from(" YES").is_falsey());
749                assert_eq!(false, OsString::from("Yes   ").is_falsey());
750                assert_eq!(false, OsString::from("yEs").is_falsey());
751            }
752
753            // is_truey
754            {
755                assert_eq!(false, OsString::from("").is_truey());
756
757                assert_eq!(false, OsString::from("0").is_truey());
758                assert_eq!(false, OsString::from("false").is_truey());
759                assert_eq!(false, OsString::from(" FALSE").is_truey());
760                assert_eq!(false, OsString::from("False").is_truey());
761                assert_eq!(false, OsString::from("FaLSe").is_truey());
762                assert_eq!(false, OsString::from("no").is_truey());
763                assert_eq!(false, OsString::from("No ").is_truey());
764                assert_eq!(false, OsString::from("NO").is_truey());
765                assert_eq!(false, OsString::from(" Off ").is_truey());
766                assert_eq!(false, OsString::from("off").is_truey());
767                assert_eq!(false, OsString::from("OFF").is_truey());
768
769                assert_eq!(true, OsString::from("1").is_truey());
770                assert_eq!(true, OsString::from("true").is_truey());
771                assert_eq!(true, OsString::from("TRUE").is_truey());
772                assert_eq!(true, OsString::from("True").is_truey());
773                assert_eq!(true, OsString::from("tRuE").is_truey());
774                assert_eq!(true, OsString::from("yes").is_truey());
775                assert_eq!(true, OsString::from(" YES").is_truey());
776                assert_eq!(true, OsString::from("Yes   ").is_truey());
777                assert_eq!(true, OsString::from("yEs").is_truey());
778            }
779
780            // is_truthy
781            {
782                assert_eq!(None, OsString::from("").is_truthy());
783
784                assert_eq!(Some(false), OsString::from("0").is_truthy());
785                assert_eq!(Some(false), OsString::from("false").is_truthy());
786                assert_eq!(Some(false), OsString::from(" FALSE").is_truthy());
787                assert_eq!(Some(false), OsString::from("False").is_truthy());
788                assert_eq!(Some(false), OsString::from("FaLSe").is_truthy());
789                assert_eq!(Some(false), OsString::from("no").is_truthy());
790                assert_eq!(Some(false), OsString::from("No ").is_truthy());
791                assert_eq!(Some(false), OsString::from("NO").is_truthy());
792                assert_eq!(Some(false), OsString::from(" Off ").is_truthy());
793                assert_eq!(Some(false), OsString::from("off").is_truthy());
794                assert_eq!(Some(false), OsString::from("OFF").is_truthy());
795
796                assert_eq!(Some(true), OsString::from("1").is_truthy());
797                assert_eq!(Some(true), OsString::from("true").is_truthy());
798                assert_eq!(Some(true), OsString::from("TRUE").is_truthy());
799                assert_eq!(Some(true), OsString::from("True").is_truthy());
800                assert_eq!(Some(true), OsString::from("tRuE").is_truthy());
801                assert_eq!(Some(true), OsString::from("yes").is_truthy());
802                assert_eq!(Some(true), OsString::from(" YES").is_truthy());
803                assert_eq!(Some(true), OsString::from("Yes   ").is_truthy());
804                assert_eq!(Some(true), OsString::from("yEs").is_truthy());
805            }
806        }
807
808        #[cfg(feature = "implement-Truthy-for-String")]
809        #[test]
810        fn TEST_String_Truthy() {
811            // is_falsey
812            {
813                assert_eq!(false, String::from("").is_falsey());
814
815                assert_eq!(true, String::from("0").is_falsey());
816                assert_eq!(true, String::from("false").is_falsey());
817                assert_eq!(true, String::from(" FALSE").is_falsey());
818                assert_eq!(true, String::from("False").is_falsey());
819                assert_eq!(true, String::from("FaLSe").is_falsey());
820                assert_eq!(true, String::from("no").is_falsey());
821                assert_eq!(true, String::from("No ").is_falsey());
822                assert_eq!(true, String::from("NO").is_falsey());
823                assert_eq!(true, String::from(" Off ").is_falsey());
824                assert_eq!(true, String::from("off").is_falsey());
825                assert_eq!(true, String::from("OFF").is_falsey());
826
827                assert_eq!(false, String::from("1").is_falsey());
828                assert_eq!(false, String::from("true").is_falsey());
829                assert_eq!(false, String::from("TRUE").is_falsey());
830                assert_eq!(false, String::from("True").is_falsey());
831                assert_eq!(false, String::from("tRuE").is_falsey());
832                assert_eq!(false, String::from("yes").is_falsey());
833                assert_eq!(false, String::from(" YES").is_falsey());
834                assert_eq!(false, String::from("Yes   ").is_falsey());
835                assert_eq!(false, String::from("yEs").is_falsey());
836            }
837
838            // is_truey
839            {
840                assert_eq!(false, String::from("").is_truey());
841
842                assert_eq!(false, String::from("0").is_truey());
843                assert_eq!(false, String::from("false").is_truey());
844                assert_eq!(false, String::from(" FALSE").is_truey());
845                assert_eq!(false, String::from("False").is_truey());
846                assert_eq!(false, String::from("FaLSe").is_truey());
847                assert_eq!(false, String::from("no").is_truey());
848                assert_eq!(false, String::from("No ").is_truey());
849                assert_eq!(false, String::from("NO").is_truey());
850                assert_eq!(false, String::from(" Off ").is_truey());
851                assert_eq!(false, String::from("off").is_truey());
852                assert_eq!(false, String::from("OFF").is_truey());
853
854                assert_eq!(true, String::from("1").is_truey());
855                assert_eq!(true, String::from("true").is_truey());
856                assert_eq!(true, String::from("TRUE").is_truey());
857                assert_eq!(true, String::from("True").is_truey());
858                assert_eq!(true, String::from("tRuE").is_truey());
859                assert_eq!(true, String::from("yes").is_truey());
860                assert_eq!(true, String::from(" YES").is_truey());
861                assert_eq!(true, String::from("Yes   ").is_truey());
862                assert_eq!(true, String::from("yEs").is_truey());
863            }
864
865            // is_truthy
866            {
867                assert_eq!(None, String::from("").is_truthy());
868
869                assert_eq!(Some(false), String::from("0").is_truthy());
870                assert_eq!(Some(false), String::from("false").is_truthy());
871                assert_eq!(Some(false), String::from(" FALSE").is_truthy());
872                assert_eq!(Some(false), String::from("False").is_truthy());
873                assert_eq!(Some(false), String::from("FaLSe").is_truthy());
874                assert_eq!(Some(false), String::from("no").is_truthy());
875                assert_eq!(Some(false), String::from("No ").is_truthy());
876                assert_eq!(Some(false), String::from("NO").is_truthy());
877                assert_eq!(Some(false), String::from(" Off ").is_truthy());
878                assert_eq!(Some(false), String::from("off").is_truthy());
879                assert_eq!(Some(false), String::from("OFF").is_truthy());
880
881                assert_eq!(Some(true), String::from("1").is_truthy());
882                assert_eq!(Some(true), String::from("true").is_truthy());
883                assert_eq!(Some(true), String::from("TRUE").is_truthy());
884                assert_eq!(Some(true), String::from("True").is_truthy());
885                assert_eq!(Some(true), String::from("tRuE").is_truthy());
886                assert_eq!(Some(true), String::from("yes").is_truthy());
887                assert_eq!(Some(true), String::from(" YES").is_truthy());
888                assert_eq!(Some(true), String::from("Yes   ").is_truthy());
889                assert_eq!(Some(true), String::from("yEs").is_truthy());
890            }
891        }
892
893        #[cfg(feature = "implement-Truthy-for-bool")]
894        #[test]
895        fn TEST_bool_Truthy() {
896            // bool
897            {
898                assert_eq!(true, false.is_falsey());
899                assert_eq!(false, false.is_truey());
900                assert_eq!(Some(false), false.is_truthy());
901
902                assert_eq!(false, true.is_falsey());
903                assert_eq!(true, true.is_truey());
904                assert_eq!(Some(true), true.is_truthy());
905            }
906
907            // &bool
908            {
909                assert_eq!(true, (&false).is_falsey());
910                assert_eq!(false, (&false).is_truey());
911                assert_eq!(Some(false), (&false).is_truthy());
912
913                assert_eq!(false, (&true).is_falsey());
914                assert_eq!(true, (&true).is_truey());
915                assert_eq!(Some(true), (&true).is_truthy());
916            }
917
918            // Option<bool>
919            {
920                assert_eq!(true, Some(false).is_falsey());
921                assert_eq!(false, Some(false).is_truey());
922                assert_eq!(Some(false), Some(false).is_truthy());
923
924                assert_eq!(false, Some(true).is_falsey());
925                assert_eq!(true, Some(true).is_truey());
926                assert_eq!(Some(true), Some(true).is_truthy());
927            }
928
929            // Option<&bool>
930            {
931                assert_eq!(true, Some(&false).is_falsey());
932                assert_eq!(false, Some(&false).is_truey());
933                assert_eq!(Some(false), Some(&false).is_truthy());
934
935                assert_eq!(false, Some(&true).is_falsey());
936                assert_eq!(true, Some(&true).is_truey());
937                assert_eq!(Some(true), Some(&true).is_truthy());
938            }
939
940            // &Option<bool>
941            {
942                assert_eq!(true, (&Some(false)).is_falsey());
943                assert_eq!(false, (&Some(false)).is_truey());
944                assert_eq!(Some(false), (&Some(false)).is_truthy());
945
946                assert_eq!(false, (&Some(true)).is_falsey());
947                assert_eq!(true, (&Some(true)).is_truey());
948                assert_eq!(Some(true), (&Some(true)).is_truthy());
949            }
950
951            // &Option<&bool>
952            {
953                assert_eq!(true, (&Some(&false)).is_falsey());
954                assert_eq!(false, (&Some(&false)).is_truey());
955                assert_eq!(Some(false), (&Some(&false)).is_truthy());
956
957                assert_eq!(false, (&Some(&true)).is_falsey());
958                assert_eq!(true, (&Some(&true)).is_truey());
959                assert_eq!(Some(true), (&Some(&true)).is_truthy());
960            }
961        }
962
963        #[cfg(any(
964            feature = "implement-Truthy-for-AsStr",
965            feature = "implement-Truthy-for-str",
966        ))]
967        #[test]
968        fn TEST_str_Truthy() {
969            // is_falsey
970            {
971                assert_eq!(false, "".is_falsey());
972
973                assert_eq!(true, "0".is_falsey());
974                assert_eq!(true, "false".is_falsey());
975                assert_eq!(true, " FALSE".is_falsey());
976                assert_eq!(true, "False".is_falsey());
977                assert_eq!(true, "FaLSe".is_falsey());
978                assert_eq!(true, "no".is_falsey());
979                assert_eq!(true, "No ".is_falsey());
980                assert_eq!(true, "NO".is_falsey());
981                assert_eq!(true, " Off ".is_falsey());
982                assert_eq!(true, "off".is_falsey());
983                assert_eq!(true, "OFF".is_falsey());
984
985                assert_eq!(false, "1".is_falsey());
986                assert_eq!(false, "true".is_falsey());
987                assert_eq!(false, "TRUE".is_falsey());
988                assert_eq!(false, "True".is_falsey());
989                assert_eq!(false, "tRuE".is_falsey());
990                assert_eq!(false, "yes".is_falsey());
991                assert_eq!(false, " YES".is_falsey());
992                assert_eq!(false, "Yes   ".is_falsey());
993                assert_eq!(false, "yEs".is_falsey());
994            }
995
996            {
997                assert_eq!(false, "".is_truey());
998
999                assert_eq!(false, "0".is_truey());
1000                assert_eq!(false, "false".is_truey());
1001                assert_eq!(false, " FALSE".is_truey());
1002                assert_eq!(false, "False".is_truey());
1003                assert_eq!(false, "FaLSe".is_truey());
1004                assert_eq!(false, "no".is_truey());
1005                assert_eq!(false, "No ".is_truey());
1006                assert_eq!(false, "NO".is_truey());
1007                assert_eq!(false, " Off ".is_truey());
1008                assert_eq!(false, "off".is_truey());
1009                assert_eq!(false, "OFF".is_truey());
1010
1011                assert_eq!(true, "1".is_truey());
1012                assert_eq!(true, "true".is_truey());
1013                assert_eq!(true, "TRUE".is_truey());
1014                assert_eq!(true, "True".is_truey());
1015                assert_eq!(true, "tRuE".is_truey());
1016                assert_eq!(true, "yes".is_truey());
1017                assert_eq!(true, " YES".is_truey());
1018                assert_eq!(true, "Yes   ".is_truey());
1019                assert_eq!(true, "yEs".is_truey());
1020            }
1021
1022            // is_truthy
1023            {
1024                assert_eq!(None, "".is_truthy());
1025
1026                assert_eq!(Some(false), "0".is_truthy());
1027                assert_eq!(Some(false), "false".is_truthy());
1028                assert_eq!(Some(false), " FALSE".is_truthy());
1029                assert_eq!(Some(false), "False".is_truthy());
1030                assert_eq!(Some(false), "FaLSe".is_truthy());
1031                assert_eq!(Some(false), "no".is_truthy());
1032                assert_eq!(Some(false), "No ".is_truthy());
1033                assert_eq!(Some(false), "NO".is_truthy());
1034                assert_eq!(Some(false), " Off ".is_truthy());
1035                assert_eq!(Some(false), "off".is_truthy());
1036                assert_eq!(Some(false), "OFF".is_truthy());
1037
1038                assert_eq!(Some(true), "1".is_truthy());
1039                assert_eq!(Some(true), "true".is_truthy());
1040                assert_eq!(Some(true), "TRUE".is_truthy());
1041                assert_eq!(Some(true), "True".is_truthy());
1042                assert_eq!(Some(true), "tRuE".is_truthy());
1043                assert_eq!(Some(true), "yes".is_truthy());
1044                assert_eq!(Some(true), " YES".is_truthy());
1045                assert_eq!(Some(true), "Yes   ".is_truthy());
1046                assert_eq!(Some(true), "yEs".is_truthy());
1047            }
1048        }
1049    }
1050}