use miette::Result;
use std::marker::PhantomData;
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};
pub trait ShellDialect: Default + Send + Sync + 'static {
const NAME: &'static str;
}
#[derive(Default)]
pub struct BashDialect;
impl ShellDialect for BashDialect {
const NAME: &'static str = "bash";
}
#[derive(Default)]
pub struct ZshDialect;
impl ShellDialect for ZshDialect {
const NAME: &'static str = "zsh";
}
#[derive(Default)]
pub struct ShellMetaLanguage<D: ShellDialect>(PhantomData<D>);
pub type BashMetaLanguage = ShellMetaLanguage<BashDialect>;
pub type ZshMetaLanguage = ShellMetaLanguage<ZshDialect>;
impl<D: ShellDialect> MetaLanguage for ShellMetaLanguage<D> {
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> {
let host = D::NAME;
miette::bail!(
"{host} can't lift `{target}↑`: a shell value is already text, so there is no \
conversion to spell — splice it with `↙…↘`, which interpolates it as written"
)
}
fn emit_str(&self) -> Result<&'static str> {
let host = D::NAME;
miette::bail!(
"{host} can't emit `←`: the string-based meta has no `b_` accumulator to emit into, \
and a shell join takes its operand inside a substitution rather than after a prefix \
— collect the fragments in an array and splice the join itself, \
`↙$(printf '%s\\n' \"${{frags[@]}}\")↘`"
)
}
fn reduce_str(&self, target: &str) -> Result<&'static str> {
let host = D::NAME;
miette::bail!(
"{host} can't reduce `{target}↓`: the string-based meta has no `QTerm` runtime to \
evaluate a fragment — compute the value in ordinary shell and splice it with `↙…↘`"
)
}
fn type_str(&self) -> Result<&'static str> {
let host = D::NAME;
miette::bail!(
"{host} has no type for `⟨T⟩`: the shell is untyped and a generated fragment is just \
a word — drop the annotation"
)
}
fn name_str(&self) -> Result<&'static str> {
let host = D::NAME;
miette::bail!(
"{host} has no spelling for `⟨N⟩`: a name is already its own text here — splice it \
with `↙…↘`"
)
}
}