#![forbid(unsafe_code)]
mod abc;
mod config;
mod drums;
mod io;
mod song;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use anyhow::{Result, bail};
use clap::{Parser, Subcommand};
use duet_core::Grid;
use crate::abc::AbcCommand;
use crate::config::{Config, Dirs};
use crate::drums::{DrumsCommand, ReadOptions};
use crate::io::{AnyFormat, Format, read, write};
use crate::song::SongCommand;
#[derive(Parser)]
#[command(name = "duet", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Convert {
input: PathBuf,
output: PathBuf,
#[arg(long, value_enum)]
from: Option<AnyFormat>,
#[arg(long, value_enum)]
to: Option<AnyFormat>,
#[arg(long)]
grid: Option<Grid>,
#[arg(long)]
track: Option<String>,
#[arg(long)]
bars: Option<u32>,
#[arg(long)]
snap_levels: bool,
},
#[command(subcommand)]
Song(SongCommand),
#[command(subcommand)]
Drums(DrumsCommand),
#[command(subcommand)]
Abc(AbcCommand),
#[command(subcommand)]
Config(ConfigCommand),
}
#[derive(Subcommand)]
enum ConfigCommand {
Path,
Show,
}
fn run(cli: Cli, dirs: &Dirs) -> Result<()> {
match cli.command {
Command::Convert {
input,
output,
from,
to,
grid,
track,
bars,
snap_levels,
} => convert(
&input,
&output,
from,
to,
GridFlags {
grid,
track,
bars,
snap_levels,
},
&Config::load(dirs)?,
),
Command::Song(command) => song::run(command, &Config::load(dirs)?),
Command::Drums(command) => drums::run(command, &Config::load(dirs)?),
Command::Abc(command) => abc::run(command, &Config::load(dirs)?),
Command::Config(ConfigCommand::Path) => stdout(&config::describe_paths(dirs)),
Command::Config(ConfigCommand::Show) => stdout(&Config::load(dirs)?.show()),
}
}
struct GridFlags {
grid: Option<Grid>,
track: Option<String>,
bars: Option<u32>,
snap_levels: bool,
}
fn convert(
input: &Path,
output: &Path,
from: Option<AnyFormat>,
to: Option<AnyFormat>,
flags: GridFlags,
config: &Config,
) -> Result<()> {
let text = AnyFormat::Song(Format::Text);
let from = from.unwrap_or_else(|| AnyFormat::of(input, text));
let to = to.unwrap_or_else(|| AnyFormat::of(output, text));
let GridFlags {
grid,
track,
bars,
snap_levels,
} = flags;
if let (AnyFormat::Grid(from), AnyFormat::Grid(to), None) = (from, to, grid) {
let given: Vec<&str> = [(track.is_some(), "--track"), (bars.is_some(), "--bars")]
.into_iter()
.filter_map(|(set, flag)| set.then_some(flag))
.collect();
if !given.is_empty() {
bail!(
"{} {} for reading a song's drum track, and need --grid; without it, a grid \
is copied as it is",
given.join(", "),
if given.len() == 1 { "is" } else { "are" },
);
}
let copy = drums::levels_for(drums::read_grid(input, from, config)?, snap_levels, to)?;
return drums::write_grid(©, output, to);
}
let song = match from {
AnyFormat::Song(format) => read(input, format)?,
AnyFormat::Grid(form) => drums::read_grid(input, form, config)?.to_song()?,
};
match to {
AnyFormat::Song(format) => {
let given: Vec<&str> = [
(grid.is_some(), "--grid"),
(track.is_some(), "--track"),
(bars.is_some(), "--bars"),
(snap_levels, "--snap-levels"),
]
.into_iter()
.filter_map(|(set, flag)| set.then_some(flag))
.collect();
if !given.is_empty() {
bail!(
"{} {} for writing a drum grid, and {} is not one; a grid is .drums, \
.drums.json or .drums.toml, or --to drums, drums-json or drums-toml",
given.join(", "),
if given.len() == 1 { "is" } else { "are" },
output.display()
);
}
write(&song, output, format, config.end)
}
AnyFormat::Grid(form) => {
let Some(grid) = grid else {
bail!(
"writing a drum grid from a song needs --grid, the step, such as \
--grid 1/16; duet never guesses it"
);
};
let options = ReadOptions {
grid,
track,
bars,
snap_levels,
};
drums::write_grid(
&drums::grid_of_song(&song, &options, form, config)?,
output,
form,
)
}
}
}
fn stdout(text: &str) -> Result<()> {
use std::io::Write as _;
std::io::stdout()
.write_all(text.as_bytes())
.map_err(|e| anyhow::anyhow!("writing standard output: {e}"))
}
fn main() -> ExitCode {
match Dirs::find().and_then(|dirs| run(Cli::parse(), &dirs)) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("duet: {e:#}");
ExitCode::FAILURE
}
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use clap::Parser;
use crate::config::Dirs;
use crate::{Cli, run};
pub(crate) fn run_hermetic(args: &[&str]) -> anyhow::Result<()> {
static N: AtomicUsize = AtomicUsize::new(0);
let user = std::env::temp_dir().join(format!(
"duet-no-config-{}-{}",
std::process::id(),
N.fetch_add(1, Ordering::Relaxed)
));
let dirs = Dirs {
user: Some((user, "the test's empty config directory")),
project: None,
};
run(
Cli::parse_from(std::iter::once("duet").chain(args.iter().copied())),
&dirs,
)
}
}