use crate::value::*;
pub(crate) fn coerce_drv_value_to_string(v: &Value) -> Result<String, EvalError> {
let (s, _ctx) = v.coerce_to_string_copy_to_store()?;
Ok(s)
}
pub(crate) fn coerce_drv_value_to_string_with_context(
v: &Value,
) -> Result<(String, StringContext), EvalError> {
v.coerce_to_string_copy_to_store()
}
pub(crate) fn coerce_drv_value_to_string_opt(v: &Value) -> Option<String> {
coerce_drv_value_to_string(v).ok()
}
pub(crate) fn coerce_drv_value_to_string_opt_with_context(
v: &Value,
) -> Result<Option<(String, StringContext)>, EvalError> {
match v.coerce_to_string_copy_to_store() {
Ok(pair) => Ok(Some(pair)),
Err(e @ (EvalError::Throw(_) | EvalError::Abort(_) | EvalError::AssertionFailed(_))) => {
Err(e)
}
Err(_) => Ok(None),
}
}
pub(crate) fn force_attr_string(
attrs: &NixAttrs,
key: &str,
) -> Result<String, EvalError> {
let v = attrs
.get(key)
.ok_or_else(|| EvalError::AttrNotFound(key.into()))?;
let forced = crate::eval::force_value(v)?;
coerce_drv_value_to_string(&forced)
}
pub(crate) fn optional_attr_string(
attrs: &NixAttrs,
key: &str,
) -> Result<Option<String>, EvalError> {
match attrs.get(key) {
None => Ok(None),
Some(v) => {
let forced = crate::eval::force_value(v)?;
Ok(Some(coerce_drv_value_to_string(&forced)?))
}
}
}