use crate::{
CliCommandsAlg, CliContextAlg, CliNamedSources, CliNestAlg, CliOperationAlg, CliProgramAlg, CliSources,
Conventions, Fallible, NamedSource, PositionalSource, ProductSource, SumSource, Total,
};
use alux_ext::OperationAlg;
pub trait CompileCliProgram<Compiler> {
type Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands;
}
#[derive(Debug, Default)]
pub struct Empty;
#[derive(Debug)]
pub struct Merge<Left, Right> {
left: Left,
right: Right,
}
#[derive(Debug)]
pub struct Named<Program>(Program);
#[derive(Debug)]
pub struct Nest<Program> {
name: &'static str,
program: Program,
}
#[derive(Debug)]
pub struct Within<Handler, Inputs, Mode, Program> {
handler: Handler,
inputs: Inputs,
mode: Mode,
program: Program,
}
#[derive(Debug)]
pub struct Command<Handler, Inputs, Mode = Total> {
name: &'static str,
handler: Handler,
inputs: Inputs,
mode: Mode,
conventions: Conventions,
}
#[derive(Debug)]
pub struct Operation<Handler, Inputs = (), Mode = Total> {
handler: Handler,
inputs: Inputs,
mode: Mode,
conventions: Conventions,
}
#[derive(Debug)]
pub struct CliProgram<Program>(Program);
#[derive(Debug, Default)]
pub struct CliProgramBuilder;
impl CliProgramBuilder {
pub fn commands(&self) -> CliProgram<Empty> {
CliProgram(Empty)
}
pub fn op<Handler>(&self, handler: Handler) -> Operation<Handler> {
Operation { handler, inputs: (), mode: Total, conventions: Conventions::default() }
}
pub fn program<Program>(&self, program: Program) -> CliProgram<Named<Program>> {
CliProgram(Named(program))
}
pub fn positional(&self, doc: &'static str) -> PositionalSource {
PositionalSource::new(doc)
}
pub fn product(&self) -> ProductSource<()> {
ProductSource::new()
}
pub fn sum(&self) -> SumSource<()> {
SumSource::new()
}
pub fn named(&self, doc: &'static str) -> NamedSource {
NamedSource::new(doc)
}
}
impl<Handler, Inputs, Mode> Operation<Handler, Inputs, Mode> {
pub fn arg_for<Source>(self, name: &'static str, source: Source) -> Operation<Handler, (Inputs, Source), Mode> {
let _: &'static str = name;
self.arg(source)
}
pub fn arg<Source>(self, source: Source) -> Operation<Handler, (Inputs, Source), Mode> {
Operation {
handler: self.handler,
inputs: (self.inputs, source),
mode: self.mode,
conventions: self.conventions,
}
}
pub fn declare(self, name: &'static str) -> Command<Handler, Inputs, Mode> {
Command { name, handler: self.handler, inputs: self.inputs, mode: self.mode, conventions: self.conventions }
}
}
impl<Handler, Inputs, Mode> Operation<Handler, Inputs, Mode> {
#[must_use]
pub fn options_first(mut self) -> Self {
self.conventions.options_first = true;
self
}
#[must_use]
pub fn unknown_options_as_operands(mut self) -> Self {
self.conventions.unknown_options_are_operands = true;
self
}
#[must_use]
pub fn no_end_of_options(mut self) -> Self {
self.conventions.double_dash_ends_options = false;
self
}
#[must_use]
pub fn numeric_option(mut self, long: &'static str) -> Self {
self.conventions.numeric_option = Some(long);
self
}
#[must_use]
pub fn verbatim(mut self) -> Self {
self.conventions.verbatim = true;
self
}
pub fn within<Program>(self, program: CliProgram<Program>) -> Within<Handler, Inputs, Mode, Program> {
Within { handler: self.handler, inputs: self.inputs, mode: self.mode, program: program.0 }
}
}
impl<Handler, Inputs> Operation<Handler, Inputs, Total> {
pub fn fallible(self) -> Operation<Handler, Inputs, Fallible> {
Operation { handler: self.handler, inputs: self.inputs, mode: Fallible, conventions: self.conventions }
}
}
impl<Program> CliProgram<Program> {
pub fn merge<Other>(self, other: CliProgram<Other>) -> CliProgram<Merge<Program, Other>> {
CliProgram(Merge { left: self.0, right: other.0 })
}
pub fn command<Handler, Inputs, Mode>(
self,
name: &'static str,
operation: Operation<Handler, Inputs, Mode>,
) -> CliProgram<Merge<Program, Command<Handler, Inputs, Mode>>> {
self.merge(CliProgram(operation.declare(name)))
}
pub fn cmd<Handler, Inputs, Mode>(
self,
operation: Operation<Handler, Inputs, Mode>,
) -> CliProgram<Merge<Program, Command<Handler, Inputs, Mode>>> {
self.merge(CliProgram(operation.declare("")))
}
pub fn nest<Other>(self, name: &'static str, other: CliProgram<Other>) -> CliProgram<Merge<Program, Nest<Other>>> {
self.merge(CliProgram(other.under(name)))
}
pub fn within<Handler, Inputs, Mode, Other>(
self,
operation: Operation<Handler, Inputs, Mode>,
other: CliProgram<Other>,
) -> CliProgram<Merge<Program, Within<Handler, Inputs, Mode, Other>>> {
self.merge(CliProgram(operation.within(other)))
}
pub fn under(self, name: &'static str) -> Nest<Program> {
Nest { name, program: self.0 }
}
pub fn into_program(self) -> Program {
self.0
}
}
impl<Compiler> CompileCliProgram<Compiler> for Empty
where
Compiler: CliCommandsAlg,
{
type Commands = Compiler::Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
compiler.cli_empty()
}
}
impl<Compiler, Left, Right, Commands> CompileCliProgram<Compiler> for Merge<Left, Right>
where
Compiler: CliCommandsAlg<Commands = Commands>,
Left: CompileCliProgram<Compiler, Commands = Commands>,
Right: CompileCliProgram<Compiler, Commands = Commands>,
{
type Commands = Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
compiler.cli_merge(self.left.compile_cli_program(compiler), self.right.compile_cli_program(compiler))
}
}
impl<Compiler, Program> CompileCliProgram<Compiler> for Named<Program>
where
Program: CliProgramAlg<Compiler>,
{
type Commands = Program::Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
self.0.compile_cli(compiler)
}
}
impl<Compiler, Program, Commands> CompileCliProgram<Compiler> for Nest<Program>
where
Compiler: CliNestAlg<Commands = Commands>,
Program: CompileCliProgram<Compiler, Commands = Commands>,
{
type Commands = Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
let program = self.program;
compiler.cli_nest_with(self.name, |compiler| program.compile_cli_program(compiler))
}
}
impl<Compiler, Handler, Inputs, Mode> CompileCliProgram<Compiler> for Command<Handler, Inputs, Mode>
where
Compiler: CliOperationAlg<Handler, Inputs, Mode>,
Handler: OperationAlg,
Inputs: CliSources<Handler::Args>,
{
type Commands = Compiler::Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
compiler.cli_operation(self.name, self.handler, self.inputs, self.mode, self.conventions)
}
}
impl<Compiler, Handler, Inputs, Mode, Program, Inner, InnerCommands> CompileCliProgram<Compiler>
for Within<Handler, Inputs, Mode, Program>
where
Compiler: CliContextAlg<Handler, Inputs, Mode, Inner = Inner, InnerCommands = InnerCommands>,
Handler: OperationAlg,
Inputs: CliSources<Handler::Args> + CliNamedSources,
Program: CompileCliProgram<Inner, Commands = InnerCommands>,
{
type Commands = Compiler::Commands;
fn compile_cli_program(self, compiler: &Compiler) -> Self::Commands {
let inner = self.program.compile_cli_program(&compiler.cli_inner());
compiler.cli_within(self.handler, self.inputs, self.mode, inner)
}
}