use crate::interop::raw::{NixFunctionExt, NixpkgsExt};
use crate::{
IntoRusixValue, RusixValue, ValidationError,
interop::{Nixpkgs, raw::NixValue},
};
#[derive(Clone, Debug)]
pub struct OptionType(
NixValue,
);
impl OptionType {
#[track_caller]
pub fn named(name: &str) -> Self {
Self(Nixpkgs::new().lib_value(&format!("types.{name}")))
}
pub fn opaque(value: NixValue) -> Self {
Self(value)
}
pub fn as_value(&self) -> NixValue {
self.0.clone()
}
#[track_caller]
pub fn null_or(self) -> Self {
Self(Nixpkgs::new().function("types.nullOr").call(self.0))
}
#[track_caller]
pub fn list_of(self) -> Self {
Self(Nixpkgs::new().function("types.listOf").call(self.0))
}
#[track_caller]
pub fn attrs_of(self) -> Self {
Self(Nixpkgs::new().function("types.attrsOf").call(self.0))
}
#[track_caller]
pub fn function_to(self) -> Self {
Self(Nixpkgs::new().function("types.functionTo").call(self.0))
}
#[track_caller]
pub fn one_of(types: impl IntoIterator<Item = Self>) -> Self {
Self(
Nixpkgs::new()
.function("types.oneOf")
.call(NixValue::list(types.into_iter().map(|t| t.0))),
)
}
#[track_caller]
pub fn coerced_from(self, source: Self, coercion: NixValue) -> Self {
Self(
Nixpkgs::new()
.function("types.coercedTo")
.apply([source.0, coercion, self.0]),
)
}
#[track_caller]
pub fn submodule(
options: impl IntoRusixValue,
freeform: Option<Self>,
) -> Result<Self, ValidationError> {
let mut fields = vec![("options", options.try_into_nix_value()?)];
if let Some(ty) = freeform {
fields.push(("freeformType", ty.0));
}
Ok(Self(
Nixpkgs::new()
.function("types.submodule")
.call(NixValue::record(fields)),
))
}
}
#[derive(Clone, Debug)]
pub struct OptionDecl {
value: NixValue,
}
impl OptionDecl {
#[track_caller]
pub fn new(ty: OptionType) -> Self {
Self::from_value(
Nixpkgs::new()
.function("mkOption")
.call(NixValue::record([("type", ty.0)])),
)
}
pub fn from_value(value: NixValue) -> Self {
Self { value }
}
#[track_caller]
pub fn enable(description: &str) -> Self {
Self::from_value(Nixpkgs::new().function("mkEnableOption").call(description))
}
#[track_caller]
fn metadata(mut self, name: &str, value: NixValue) -> Self {
self.value = Nixpkgs::new()
.function("mergeAttrs")
.apply([self.value, NixValue::record([(name, value)])]);
self
}
#[track_caller]
pub fn default(self, value: impl Into<NixValue>) -> Self {
self.metadata("default", value.into())
}
#[track_caller]
pub fn default_text(self, value: impl Into<NixValue>) -> Self {
self.metadata("defaultText", value.into())
}
#[track_caller]
pub fn example(self, value: impl Into<NixValue>) -> Self {
self.metadata("example", value.into())
}
#[track_caller]
pub fn description(self, value: impl Into<NixValue>) -> Self {
self.metadata("description", value.into())
}
#[track_caller]
pub fn internal(self) -> Self {
self.metadata("internal", true.into())
}
#[track_caller]
pub fn read_only(self) -> Self {
self.metadata("readOnly", true.into())
}
}
impl IntoRusixValue for OptionDecl {
#[track_caller]
fn into_value(self) -> RusixValue {
self.value.into_value()
}
}