pub fn should_include_source_location() -> bool {
std::env::var("HEXSER_INCLUDE_SOURCE_LOCATION")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false)
}
#[cfg(feature = "serde")]
pub fn should_skip_location(
location: &Option<crate::error::source_location::SourceLocation>,
) -> bool {
location.is_none() || !should_include_source_location()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_include_source_location_default() {
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
std::assert_eq!(should_include_source_location(), false);
}
#[test]
fn test_should_include_source_location_enabled_with_1() {
unsafe {
std::env::set_var("HEXSER_INCLUDE_SOURCE_LOCATION", "1");
}
std::assert_eq!(should_include_source_location(), true);
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
}
#[test]
fn test_should_include_source_location_enabled_with_true() {
unsafe {
std::env::set_var("HEXSER_INCLUDE_SOURCE_LOCATION", "true");
}
std::assert_eq!(should_include_source_location(), true);
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
}
#[test]
fn test_should_include_source_location_enabled_with_true_uppercase() {
unsafe {
std::env::set_var("HEXSER_INCLUDE_SOURCE_LOCATION", "TRUE");
}
std::assert_eq!(should_include_source_location(), true);
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
}
#[test]
fn test_should_include_source_location_disabled_with_other_value() {
unsafe {
std::env::set_var("HEXSER_INCLUDE_SOURCE_LOCATION", "false");
}
std::assert_eq!(should_include_source_location(), false);
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
}
#[test]
#[cfg(feature = "serde")]
fn test_should_skip_location_when_none() {
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
let location: Option<crate::error::source_location::SourceLocation> = None;
std::assert_eq!(should_skip_location(&location), true);
}
#[test]
#[cfg(feature = "serde")]
fn test_should_skip_location_when_some_but_disabled() {
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
let location = Some(crate::error::source_location::SourceLocation::new(
"test.rs", 1, 1,
));
std::assert_eq!(should_skip_location(&location), true);
}
#[test]
#[cfg(feature = "serde")]
fn test_should_not_skip_location_when_some_and_enabled() {
unsafe {
std::env::set_var("HEXSER_INCLUDE_SOURCE_LOCATION", "1");
}
let location = Some(crate::error::source_location::SourceLocation::new(
"test.rs", 1, 1,
));
std::assert_eq!(should_skip_location(&location), false);
unsafe {
std::env::remove_var("HEXSER_INCLUDE_SOURCE_LOCATION");
}
}
}