use miette::Result;
use super::ops::{build_quote_str, build_str_code, build_unquote_str};
use crate::lang::Arity;
use crate::meta::OuterKind;
use crate::prelude::{Index, *};
use crate::{meta::MetaLanguage, qterm::QTerm, term::CmdOrHole};
#[derive(Default)]
pub struct NixMetaLanguage;
impl MetaLanguage for NixMetaLanguage {
fn expand_tuple(
&self,
_lang1: &str,
_tag: &str,
qterms: &[Arc<QTerm>],
cmds: &[CmdOrHole],
_arity: Arity,
) -> Result<Arc<QTerm>> {
Ok(build_str_code(cmds, qterms))
}
fn expand_quote(
&self,
_lang1: &str,
_tag: &str,
_i: Index,
lang2: &str,
qterm: &Arc<QTerm>,
_cmds: &[CmdOrHole],
) -> Result<Arc<QTerm>> {
Ok(build_quote_str(lang2, qterm))
}
fn expand_unquote(
&self,
_lang1: &str,
_tag: &str,
_i: Index,
lang2: &str,
qterm: &Arc<QTerm>,
_cmds: &[CmdOrHole],
) -> Result<Arc<QTerm>> {
Ok(build_unquote_str(lang2, qterm))
}
fn wrap_child(&self, qterm: Arc<QTerm>, _okind: OuterKind) -> Result<Arc<QTerm>> {
Ok(qterm)
}
fn lift_str(&self, target: &str) -> Result<&'static str> {
match target {
"" | "nix" => Ok("toString"),
_ => miette::bail!("nix can't lift into {target:?}: only homogeneous `toString`"),
}
}
fn emit_str(&self) -> Result<&'static str> {
Ok("(builtins.concatStringsSep \"\\n\")")
}
fn reduce_str(&self, target: &str) -> Result<&'static str> {
miette::bail!(
"nix can't reduce `{target}↓`: the string-based meta has no `QTerm` runtime to \
evaluate a fragment — compute the value in ordinary Nix and splice it with `↙…↘`"
)
}
fn type_str(&self) -> Result<&'static str> {
miette::bail!(
"nix has no type for `⟨T⟩`: Nix is untyped, and a generated fragment is just a \
string — drop the annotation"
)
}
fn name_str(&self) -> Result<&'static str> {
Ok("toString")
}
}