use crate::common::output;
use crate::common::report::{merge_reports, report_error, report_failure};
use crate::typeset::Command;
use crate::typeset::PrintContext;
use crate::typeset::Scope::Global;
use crate::typeset::VariableAttr::Export;
use crate::typeset::syntax::OptionSpec;
use crate::typeset::syntax::PRINT_OPTION;
use crate::typeset::syntax::interpret;
use crate::typeset::syntax::parse;
use yash_env::Env;
use yash_env::option::State::On;
use yash_env::semantics::Field;
pub const PORTABLE_OPTIONS: &[OptionSpec<'static>] = &[PRINT_OPTION];
pub const PRINT_CONTEXT: PrintContext<'static> = PrintContext {
builtin_name: "export",
builtin_is_significant: true,
options_allowed: &[],
};
pub async fn main(env: &mut Env, args: Vec<Field>) -> yash_env::builtin::Result {
match parse(PORTABLE_OPTIONS, args) {
Ok((options, operands)) => match interpret(options, operands) {
Ok(mut command) => {
match &mut command {
Command::SetVariables(sv) => {
sv.attrs.push((Export, On));
sv.scope = Global;
}
Command::PrintVariables(pv) => {
pv.attrs.push((Export, On));
pv.scope = Global;
}
Command::SetFunctions(sf) => unreachable!("{sf:?}"),
Command::PrintFunctions(pf) => unreachable!("{pf:?}"),
}
match command.execute(env, &PRINT_CONTEXT) {
Ok(result) => output(env, &result).await,
Err(errors) => report_failure(env, merge_reports(&errors).unwrap()).await,
}
}
Err(error) => report_error(env, &error).await,
},
Err(error) => report_error(env, &error).await,
}
}