use beamr::atom::Atom;
use beamr::native::ProcessContext;
use beamr::term::Term;
pub(super) enum NifRefusal {
Reported(Term),
Unbuildable(Term),
}
impl NifRefusal {
pub(super) fn reported(term: Result<Term, Term>) -> Self {
match term {
Ok(term) => Self::Reported(term),
Err(reason) => Self::Unbuildable(reason),
}
}
pub(super) fn into_nif_result(self) -> Result<Term, Term> {
match self {
Self::Reported(term) => Ok(term),
Self::Unbuildable(reason) => Err(reason),
}
}
}
pub(super) fn tagged_result_term(
ctx: &mut ProcessContext,
tag: Atom,
bytes: &[u8],
) -> Result<Term, Term> {
let value = ctx.alloc_binary(bytes)?;
ctx.alloc_tuple(&[Term::atom(tag), value])
}
pub(super) fn ok_result_term(ctx: &mut ProcessContext, bytes: &[u8]) -> Result<Term, Term> {
tagged_result_term(ctx, Atom::OK, bytes)
}
pub(super) fn error_result_term(ctx: &mut ProcessContext, message: &str) -> Result<Term, Term> {
tagged_result_term(ctx, Atom::ERROR, message.as_bytes())
}
pub(super) fn raise_reason(ctx: &mut ProcessContext, message: &str) -> Term {
match error_result_term(ctx, message) {
Ok(term) | Err(term) => term,
}
}