use clap::{Parser, ValueEnum};
use std::{fmt::Display, path::PathBuf};
use tectonic::{config::PersistentConfig, Result};
use tectonic_status_base::StatusBackend;
use tracing::error;
use crate::v2cli::{CommandCustomizations, TectonicCommand};
#[derive(Debug, Copy, Clone, ValueEnum)]
pub enum BundleJob {
#[value(name = "all")]
All,
#[value(name = "select")]
Select,
#[value(name = "pack")]
Pack,
}
impl Display for BundleJob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::All => write!(f, "all"),
Self::Select => write!(f, "select"),
Self::Pack => write!(f, "pack"),
}
}
}
impl BundleJob {
pub fn do_select(&self) -> bool {
matches!(self, Self::All | Self::Select)
}
pub fn do_pack(&self) -> bool {
matches!(self, Self::All | Self::Pack)
}
}
#[derive(Parser, Debug)]
pub struct BundleCreateCommand {
#[arg(long, default_value_t = BundleJob::All)]
pub job: BundleJob,
pub bundle_spec: PathBuf,
#[arg(long)]
pub build_dir: PathBuf,
#[arg(default_value_t = BundleFormat::BundleV1)]
pub format: BundleFormat,
#[arg(long, default_value_t = false)]
pub allow_hash_mismatch: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum BundleFormat {
#[value(name = "v1")]
BundleV1,
}
impl Display for BundleFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BundleV1 => write!(f, "v1")?,
}
Ok(())
}
}
impl TectonicCommand for BundleCreateCommand {
fn customize(&self, cc: &mut CommandCustomizations) {
cc.always_stderr = true;
}
fn execute(self, _config: PersistentConfig, _status: &mut dyn StatusBackend) -> Result<i32> {
if self.job.do_select() {
match super::actions::select(&self) {
Ok(_) => {}
Err(e) => {
error!("select job failed with error: {e}");
return Err(e.into());
}
};
}
if self.job.do_pack() {
match super::actions::pack(&self) {
Ok(_) => {}
Err(e) => {
error!("bundle packer failed with error: {e}");
return Err(e.into());
}
};
}
Ok(0)
}
}