use std::fs;
use std::io::{self, Read, Write};
use std::path::Path;
use anyhow::{Context, Result};
use clap::ValueEnum;
use clap::builder::PossibleValue;
use duet_core::{EndRule, Song};
use duet_midi::Midi;
use crate::drums::GridForm;
#[derive(Clone, Copy, PartialEq, Eq, Debug, ValueEnum)]
pub enum Format {
Midi,
Text,
Json,
Toml,
Abc,
}
pub fn format_of(path: &Path, default: Format) -> Format {
if path == Path::new("-") {
return default;
}
match path
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase)
.as_deref()
{
Some("mid" | "midi" | "smf") => Format::Midi,
Some("json") => Format::Json,
Some("toml") => Format::Toml,
Some("abc") => Format::Abc,
Some("notes") => Format::Text,
_ => Format::Text,
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AnyFormat {
Song(Format),
Grid(GridForm),
}
impl AnyFormat {
pub fn of(path: &Path, default: AnyFormat) -> AnyFormat {
if path == Path::new("-") {
return default;
}
let name = path
.file_name()
.and_then(|n| n.to_str())
.map(str::to_ascii_lowercase)
.unwrap_or_default();
if name.ends_with(".drums") {
AnyFormat::Grid(GridForm::Text)
} else if name.ends_with(".drums.json") {
AnyFormat::Grid(GridForm::Json)
} else if name.ends_with(".drums.toml") {
AnyFormat::Grid(GridForm::Toml)
} else {
AnyFormat::Song(format_of(path, Format::Text))
}
}
}
pub fn song_format(
path: &Path,
given: Option<Format>,
default: Format,
what: &str,
) -> Result<Format> {
if let Some(format) = given {
return Ok(format);
}
match AnyFormat::of(path, AnyFormat::Song(default)) {
AnyFormat::Song(format) => Ok(format),
AnyFormat::Grid(_) => anyhow::bail!(
"{} is a drum grid's name (.drums, .drums.json or .drums.toml), and {what} a \
song; duet convert goes between a grid and a song",
path.display()
),
}
}
pub fn grid_form(path: &Path, what: &str) -> Result<GridForm> {
match AnyFormat::of(path, AnyFormat::Grid(GridForm::Text)) {
AnyFormat::Grid(form) => Ok(form),
AnyFormat::Song(_) => anyhow::bail!(
"{} is not a drum grid's name, and {what} a grid; name it .drums, .drums.json or \
.drums.toml, or - for .drums text",
path.display()
),
}
}
impl ValueEnum for AnyFormat {
fn value_variants<'a>() -> &'a [Self] {
static ALL: std::sync::LazyLock<Vec<AnyFormat>> = std::sync::LazyLock::new(|| {
Format::value_variants()
.iter()
.copied()
.map(AnyFormat::Song)
.chain(
[GridForm::Text, GridForm::Json, GridForm::Toml]
.into_iter()
.map(AnyFormat::Grid),
)
.collect()
});
&ALL
}
fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
AnyFormat::Song(format) => format.to_possible_value(),
AnyFormat::Grid(GridForm::Text) => Some(PossibleValue::new("drums")),
AnyFormat::Grid(GridForm::Json) => Some(PossibleValue::new("drums-json")),
AnyFormat::Grid(GridForm::Toml) => Some(PossibleValue::new("drums-toml")),
}
}
}
pub fn read(path: &Path, format: Format) -> Result<Song> {
let bytes = if path == Path::new("-") {
let mut buf = Vec::new();
io::stdin()
.read_to_end(&mut buf)
.context("reading standard input")?;
buf
} else {
fs::read(path).with_context(|| path.display().to_string())?
};
let text = || String::from_utf8(bytes.clone()).context("input is not UTF-8 text");
let song = match format {
Format::Midi => Song::from_midi(&bytes)?,
Format::Text => Song::from_text(&text()?)?,
Format::Json => normalized(serde_json::from_str(&text()?)?)?,
Format::Toml => normalized(toml::from_str(&text()?)?)?,
Format::Abc => {
let book = duet_abc::Book::parse(&text()?)?;
if book.tunes().len() > 1 {
anyhow::bail!(
"{} holds {} tunes; pick one with duet abc render --tune <X>",
path.display(),
book.tunes().len()
);
}
crate::abc::reading(&book.tunes()[0])?
}
};
Ok(song)
}
fn normalized(mut song: Song) -> duet_core::Result<Song> {
song.normalize()?;
Ok(song)
}
pub fn with_end(song: &Song, end: EndRule) -> Result<Song> {
let mut song = song.clone();
if song.end.is_none() {
song.end = Some(song.default_end_by(end)?);
}
Ok(song)
}
pub fn write(song: &Song, path: &Path, format: Format, end: EndRule) -> Result<()> {
let song = with_end(song, end)?;
let song = &song;
let bytes = match format {
Format::Midi => song.to_midi()?,
Format::Text => song.to_text().into_bytes(),
Format::Json => {
let mut s = serde_json::to_string_pretty(song)?;
s.push('\n');
s.into_bytes()
}
Format::Toml => toml::to_string(song)?.into_bytes(),
Format::Abc => duet_abc::write(song, &duet_abc::WriteOptions::default())?.into_bytes(),
};
if path == Path::new("-") {
io::stdout()
.write_all(&bytes)
.context("writing standard output")?;
} else {
fs::write(path, bytes).with_context(|| path.display().to_string())?;
}
Ok(())
}
pub fn same_file(a: &Path, b: &Path) -> bool {
match (fs::canonicalize(a), fs::canonicalize(b)) {
(Ok(a), Ok(b)) => a == b,
_ => false,
}
}
#[cfg(test)]
mod tests {
use duet_core::{Note, Position, Song, Track};
use std::path::Path;
use super::{AnyFormat, Format, read};
use crate::drums::GridForm;
#[test]
fn a_name_ending_drums_is_a_grid_and_a_plain_json_or_toml_is_a_song() {
let text = AnyFormat::Song(Format::Text);
let of = |name: &str| AnyFormat::of(Path::new(name), text);
assert_eq!(of("beat.drums"), AnyFormat::Grid(GridForm::Text));
assert_eq!(of("dir/Beat.DRUMS.JSON"), AnyFormat::Grid(GridForm::Json));
assert_eq!(of("beat.drums.toml"), AnyFormat::Grid(GridForm::Toml));
assert_eq!(of("song.json"), AnyFormat::Song(Format::Json));
assert_eq!(of("song.toml"), AnyFormat::Song(Format::Toml));
assert_eq!(of("drums.json"), AnyFormat::Song(Format::Json));
assert_eq!(of("take.mid"), AnyFormat::Song(Format::Midi));
assert_eq!(of("take.notes"), text);
assert_eq!(of("Take.NOTES"), text);
assert_eq!(of("take.txt"), text, "a notefmt file named before .notes");
assert_eq!(of("-"), text);
}
#[test]
fn read_normalizes_json_and_toml() {
let mut song = Song::default();
let mut track = Track::new("P", 1);
track.notes.push(Note::new(
Position::new(1, 1, 960),
"C4".parse().expect("C4 is a pitch"),
0.3334,
80,
));
song.tracks.push(track);
let dir = std::env::temp_dir().join(format!("duet-read-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("temp dir");
let json = dir.join("song.json");
let toml_path = dir.join("song.toml");
std::fs::write(&json, serde_json::to_string(&song).expect("json")).expect("write");
std::fs::write(&toml_path, toml::to_string(&song).expect("toml")).expect("write");
for (path, format) in [(&json, Format::Json), (&toml_path, Format::Toml)] {
let read = read(path, format).expect("read");
let note = &read.tracks[0].notes[0];
assert_eq!((note.at.to_string(), note.beats), ("1.3.0".into(), 0.333));
}
std::fs::remove_dir_all(&dir).expect("clean up");
}
}