use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use clap::{Subcommand, ValueEnum};
use duet_abc::{Book, Dynamics, ReadOptions, Tune, WriteOptions};
use duet_core::Song;
use duet_schemas::{From, Notice};
use crate::config::Config;
use crate::io::{Format, FormatParser, read, same_file, song_format, with_end, write_from};
#[derive(Subcommand)]
pub enum AbcCommand {
Check {
input: PathBuf,
},
Render {
input: PathBuf,
output: PathBuf,
#[arg(long)]
tune: Option<u32>,
#[arg(long, value_parser = FormatParser)]
to: Option<Format>,
},
Write {
input: PathBuf,
output: PathBuf,
#[arg(long)]
unit: Option<String>,
#[arg(long, value_enum, default_value = "refuse")]
dynamics: DynamicsArg,
#[arg(long, value_parser = FormatParser)]
from: Option<Format>,
},
}
#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum DynamicsArg {
Refuse,
Nearest,
}
fn text_of(path: &Path) -> Result<String> {
if path == Path::new("-") {
let mut buf = String::new();
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
.context("reading standard input")?;
Ok(buf)
} else {
fs::read_to_string(path).with_context(|| path.display().to_string())
}
}
pub fn reading(tune: &Tune) -> Result<(Song, From)> {
let reading = tune.to_song(&ReadOptions::default())?;
let n = reading.notices.len();
if n > 0 {
eprintln!(
"duet: X:{} held {n} notation-only construct{}, not carried into the song; \
duet abc check lists them",
tune.x(),
if n == 1 { "" } else { "s" }
);
}
let mut from = From::new("abc");
from.tune = Some(tune.x().to_string());
from.notices = reading.notices.iter().map(notice).collect();
Ok((reading.song, from))
}
fn notice(n: &duet_abc::Notice) -> Notice {
let at = |v: usize| u32::try_from(v).unwrap_or(u32::MAX);
Notice::new(at(n.line), at(n.col), n.construct, n.found.clone())
}
fn unit_of(text: &str) -> Result<(u32, u32)> {
let bad = || anyhow::anyhow!("'{text}' is not a unit length like 1/8 or 1/4");
let (n, d) = text.split_once('/').unwrap_or((text, "1"));
let n: u32 = n.trim().parse().map_err(|_| bad())?;
let d: u32 = d.trim().parse().map_err(|_| bad())?;
if n == 0 || d == 0 {
return Err(bad());
}
Ok((n, d))
}
pub fn run(command: AbcCommand, config: &Config) -> Result<()> {
match command {
AbcCommand::Check { input } => {
let book = Book::parse(&text_of(&input)?)?;
let mut out = String::new();
let mut refused = 0;
let readings = book.read_all(&ReadOptions::default());
for (tune, reading) in book.tunes().iter().zip(readings) {
let name = match tune.title() {
Some(t) => format!("X:{} {t}", tune.x()),
None => format!("X:{}", tune.x()),
};
match reading {
Ok(reading) => {
out.push_str(&format!(
"{name}: read, with {} notation-only construct{}\n",
reading.notices.len(),
if reading.notices.len() == 1 { "" } else { "s" }
));
for notice in &reading.notices {
out.push_str(&format!(" {notice}\n"));
}
}
Err(e) => {
refused += 1;
out.push_str(&format!("{name}: not read: {e}\n"));
}
}
}
crate::stdout(&out)?;
if refused > 0 {
bail!(
"{refused} of {} tune{} not read",
book.tunes().len(),
if book.tunes().len() == 1 { "" } else { "s" }
);
}
Ok(())
}
AbcCommand::Render {
input,
output,
tune,
to,
} => {
if input != Path::new("-") && same_file(&input, &output) {
bail!(
"{} is the input; render writes a new file",
output.display()
);
}
let book = Book::parse(&text_of(&input)?)?;
let tune = match tune {
Some(x) => book
.tune(x)
.with_context(|| format!("{} holds no tune X:{x}", input.display()))?,
None if book.tunes().len() == 1 => &book.tunes()[0],
None => bail!(
"{} holds {} tunes; pick one with --tune <X>",
input.display(),
book.tunes().len()
),
};
let (song, from) = reading(tune)?;
let to = to.or((output == Path::new("-")).then_some(Format::Midi));
let to = song_format(&output, to, "duet abc render writes")?;
write_from(&song, &output, to, config.end, Some(&from))
}
AbcCommand::Write {
input,
output,
unit,
dynamics,
from,
} => {
if input != Path::new("-") && same_file(&input, &output) {
bail!("{} is the input; write makes a new file", output.display());
}
let from = song_format(&input, from, "duet abc write reads")?;
let song = with_end(&read(&input, from, "duet abc write reads")?, config.end)?;
let mut options = WriteOptions::default().with_dynamics(match dynamics {
DynamicsArg::Refuse => Dynamics::Refuse,
DynamicsArg::Nearest => Dynamics::Nearest,
});
if let Some(unit) = unit {
let (n, d) = unit_of(&unit)?;
options = options.with_unit(n, d);
}
let abc = duet_abc::write(&song, &options)?;
if output == Path::new("-") {
crate::stdout(&abc)
} else {
fs::write(&output, abc).with_context(|| output.display().to_string())
}
}
}
}