use super::raw::{NixRepresentation, replacement_lists};
use super::{IntoNixExpression, NixExpression, NixList, NixValue};
use crate::Expr;
#[derive(Clone, Debug)]
pub struct NixLibrary {
pub(super) value: NixValue,
}
impl NixLibrary {
pub(crate) fn from_value(value: NixValue) -> Self {
Self { value }
}
#[track_caller]
fn apply(&self, name: &str, arguments: impl IntoIterator<Item = NixValue>) -> NixValue {
self.value.clone().select(name).apply(arguments)
}
#[track_caller]
pub fn throw_if_not<T: IntoNixExpression>(
&self,
condition: impl Into<Expr<bool>>,
message: impl Into<Expr<String>>,
value: T,
) -> T::Expression {
T::Expression::from_expression(self.apply(
"throwIfNot",
[
condition.into().into(),
message.into().into(),
value.into_expression().as_expression(),
],
))
}
#[track_caller]
pub fn optional<T: IntoNixExpression>(
&self,
condition: impl Into<Expr<bool>>,
value: T,
) -> NixList<T::Expression> {
NixList::from_expression(self.apply(
"optional",
[
condition.into().into(),
value.into_expression().as_expression(),
],
))
}
#[track_caller]
pub fn optionals<T: NixExpression>(
&self,
condition: impl Into<Expr<bool>>,
values: NixList<T>,
) -> NixList<T> {
NixList::from_expression(self.apply("optionals", [condition.into().into(), values.into()]))
}
#[track_caller]
pub fn optional_text(
&self,
condition: impl Into<Expr<bool>>,
text: impl Into<Expr<String>>,
) -> Expr<String> {
self.apply(
"optionalString",
[condition.into().into(), text.into().into()],
)
.into_expr()
}
#[track_caller]
pub fn all(&self, conditions: impl IntoIterator<Item = impl Into<Expr<bool>>>) -> Expr<bool> {
self.apply(
"all",
[
NixValue::function(|value| value),
NixValue::list(
conditions
.into_iter()
.map(|condition| condition.into().into()),
),
],
)
.into_expr()
}
#[track_caller]
pub fn concat_lists<T: NixExpression>(
&self,
lists: impl IntoIterator<Item = NixList<T>>,
) -> NixList<T> {
NixList::from_expression(self.apply(
"concatLists",
[NixValue::list(lists.into_iter().map(Into::into))],
))
}
#[track_caller]
pub fn version_at_least(
&self,
version: impl Into<Expr<String>>,
minimum: impl Into<Expr<String>>,
) -> Expr<bool> {
self.apply(
"versionAtLeast",
[version.into().into(), minimum.into().into()],
)
.into_expr()
}
#[track_caller]
pub fn get_dev(&self, package: super::Package) -> super::Package {
super::Package::from_expression(self.apply("getDev", [package.into()]))
}
#[track_caller]
pub fn get_lib(&self, package: super::Package) -> super::Package {
super::Package::from_expression(self.apply("getLib", [package.into()]))
}
#[track_caller]
pub fn version_older(
&self,
version: impl Into<Expr<String>>,
other: impl Into<Expr<String>>,
) -> Expr<bool> {
self.apply("versionOlder", [version.into().into(), other.into().into()])
.into_expr()
}
#[track_caller]
pub fn replace_text(
&self,
text: impl Into<Expr<String>>,
replacements: impl IntoIterator<Item = (impl Into<Expr<String>>, impl Into<Expr<String>>)>,
) -> Expr<String> {
let [from, to] = replacement_lists(
replacements
.into_iter()
.map(|(from, to)| (NixValue::from(from.into()), NixValue::from(to.into()))),
);
self.apply("replaceStrings", [from, to, text.into().into()])
.into_expr()
}
}