use crate::environment::Environment;
use crate::error::BindError;
pub(super) const DEFAULT_MAX_RAW_BYTES: usize = 1024 * 1024;
pub(super) fn resolve_raw<E: Environment>(
environment: &E,
name: &str,
has_default: bool,
allow_empty: bool,
) -> Result<Option<String>, BindError> {
resolve_raw_with_max_bytes(
environment,
name,
has_default,
allow_empty,
DEFAULT_MAX_RAW_BYTES,
)
}
pub(super) fn resolve_raw_with_max_bytes<E: Environment>(
environment: &E,
name: &str,
has_default: bool,
allow_empty: bool,
max_bytes: usize,
) -> Result<Option<String>, BindError> {
match environment
.get(name)
.map_err(|source| BindError::environment(name.to_owned(), source))?
{
Some(value) if value.is_empty() && !allow_empty && has_default => Ok(None),
Some(value) if value.is_empty() && !allow_empty => Err(BindError::empty(name.to_owned())),
Some(value) => ensure_within_max_bytes(name, value, max_bytes).map(Some),
None => Ok(None),
}
}
pub(super) fn ensure_within_max_bytes(
name: &str,
value: String,
max_bytes: usize,
) -> Result<String, BindError> {
if value.len() > max_bytes {
return Err(BindError::value_too_large(name.to_owned(), max_bytes));
}
Ok(value)
}