Skip to main content

parse_bool_lenient

Function parse_bool_lenient 

Source
pub fn parse_bool_lenient(raw: Option<&str>) -> Option<bool>
Expand description

Read a value as a boolean, leniently: anything not false-ish is true.

FALSE_SPELLINGS are false; any other non-blank value is true. Deliberately lopsided, and not what parse_bool does.

This exists because deployments exist. A variable that has been set to disabled or enabled or y for years is a value some running system depends on, and a stricter reader would turn a working deployment into a failing one at the worst possible moment. Reach for this when adopting an existing estate, not when designing a new variable.

ยงExamples

use env_reader::parse_bool_lenient;

assert_eq!(parse_bool_lenient(Some("false")), Some(false));
assert_eq!(parse_bool_lenient(Some("0")), Some(false));
assert_eq!(parse_bool_lenient(Some("true")), Some(true));
// The lopsided part.
assert_eq!(parse_bool_lenient(Some("disabled")), Some(true));
assert_eq!(parse_bool_lenient(Some("  ")), None);