pipa-lang 1.0.0-alpha.1

A tiny template language
Documentation
// SPDX-FileCopyrightText: Copyright 2026 olav@occy.org
// SPDX-License-Identifier: MPL-2.0

use crate::lang::adapter::Adapter;
use crate::lang::args::eval_unary_arg;
use crate::lang::arity::Arity;
use crate::lang::arity::assert_func_arity;
use crate::lang::error::Error;
use crate::lang::machine::Machine;
use crate::lang::node::Node;
use crate::value::Value;
use crate::value::list::List;

pub(crate) fn keys(
    adapter: &impl Adapter,
    machine: &mut Machine<'_>,
    stdin: Option<&Value>,
    node: Node,
) -> Result<Value, Error> {
    assert_func_arity(adapter, machine, stdin, &node, &Arity::new(1))?;

    let value = eval_unary_arg(adapter, machine, stdin, node)?;
    let value = value.unwrap_or_default();

    Ok(match value {
        Value::Bytes(_) => List::default().into(),
        Value::List(v) => v
            .into_iter()
            .enumerate()
            .map(|(i, _)| Value::from(i))
            .collect::<List>()
            .into(),
        Value::Map(v) => v
            .into_iter()
            .map(|(k, _)| Value::from(k))
            .collect::<List>()
            .into(),
    })
}