lexical_bool

LexicalBool provides a bool-like type that can be parsed from a string

``rust let tests = &[ // defaulttrue` values ("true", true), ("t", true), ("1", true), ("yes", true),

// default `false` values
("false", false),
("f", false),
("0", false),
("no", false),

];

for &(input, ok) in tests { let lb : LexicalBool = input.parse().expect("valid input"); // LexicalBool can be "deref" into a bool, or compared directly with one (partialeq) assert_eq!(lb, ok); } ```

Using your own values

note This uses TLS, so the changes are only valid for the current thread ```rust // set the true values assert!(lexicalbool::initializetruevalues( &[ "foo", "bar" ] )); // set the false values assert!(lexicalbool::initializefalsevalues( &[ "baz", "qux" ] ));

// once set, you cannot change them (in this thread) asserteq!(lexicalbool::initializetruevalues( &[ "true", "1" ] ), false);

let tests = &[ // your true values ("foo", true), ("bar", true),

// your `false` values
("baz", false),
("qux", false),

];

for &(input, ok) in tests { // if parse (or fromstr) is called before {initializetruevalues, initializefalsevalues} // then it'll default to {lexicalbool::TRUTHYVALUES, lexicalbool::FALSEY_VALUES}

let lb : LexicalBool = input.parse().expect("valid input");
// LexicalBool can be "deref" into a bool, or compared directly with one (partialeq)
assert_eq!(lb, ok);

}

// ..and invalid bools use std::str::FromStr as ; use lexicalbool::Error;

let input = "true"; asserteq!( LexicalBool::fromstr(input), Err(Error::InvalidInput(input.to_string())) ); ```