lexical_bool
LexicalBool provides a bool-like type that can be parsed from a string
let tests = &[
("true", true),
("t", true),
("1", true),
("yes", true),
("false", false),
("f", false),
("0", false),
("no", false),
];
for &(input, ok) in tests {
let lb : LexicalBool = input.parse().expect("valid input");
assert_eq!(lb, ok);
}
Using your own values
note This uses TLS, so the changes are only valid for the current thread
assert!(lexical_bool::initialize_true_values(
&[ "foo", "bar" ]
));
assert!(lexical_bool::initialize_false_values(
&[ "baz", "qux" ]
));
assert_eq!(lexical_bool::initialize_true_values(
&[ "true", "1" ]
), false);
let tests = &[
("foo", true),
("bar", true),
("baz", false),
("qux", false),
];
for &(input, ok) in tests {
let lb : LexicalBool = input.parse().expect("valid input");
assert_eq!(lb, ok);
}
use std::str::FromStr as _;
use lexical_bool::Error;
let input = "true";
assert_eq!(
LexicalBool::from_str(input),
Err(Error::InvalidInput(input.to_string()))
);