use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow, bail};
use duet_core::EndRule;
use duet_drums::{LaneMap, LanePatch};
use etcetera::{AppStrategy, AppStrategyArgs, choose_app_strategy};
use serde::Deserialize;
pub const CONFIG_DIR_VAR: &str = "DUET_CONFIG_DIR";
const BUILT_IN: &str = "built-in";
pub struct Dirs {
pub user: Option<(PathBuf, &'static str)>,
pub project: Option<PathBuf>,
}
impl Dirs {
pub fn find() -> Result<Dirs> {
let user = match std::env::var_os(CONFIG_DIR_VAR).filter(|v| !v.is_empty()) {
Some(dir) => Some((PathBuf::from(dir), CONFIG_DIR_VAR)),
None => choose_app_strategy(AppStrategyArgs {
top_level_domain: String::new(),
author: String::new(),
app_name: "duet".to_owned(),
})
.ok()
.map(|s| (s.config_dir(), "the platform's config directory")),
};
let cwd = std::env::current_dir().context("finding the working directory")?;
let project = cwd
.ancestors()
.map(|dir| dir.join(".duet"))
.find(|dir| dir.is_dir());
Ok(Dirs { user, project })
}
pub fn files(&self) -> Vec<PathBuf> {
let mut out = Vec::new();
for dir in self.user.iter().map(|(d, _)| d).chain(&self.project) {
out.push(dir.join("duet.toml"));
out.push(dir.join("duet.local.toml"));
}
out
}
}
pub struct Config {
pub end: EndRule,
end_from: String,
pub lanes: LaneMap,
pub text_lanes: LaneMap,
pub text_notices: Vec<String>,
patches: Vec<(String, LanePatch)>,
lane_from: Vec<(String, &'static str, String)>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawFile {
song: Option<RawSong>,
lanes: Option<toml::Table>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawSong {
end: Option<toml::Value>,
}
fn end_rule(value: &toml::Value) -> Result<EndRule> {
let bad = || anyhow!("[song] end is \"last-bar\" or {{ round-up = <bars> }}");
match value {
toml::Value::String(s) if s == "last-bar" => Ok(EndRule::LastBar),
toml::Value::Table(t) if t.len() == 1 => {
let n = t
.get("round-up")
.and_then(toml::Value::as_integer)
.ok_or_else(bad)?;
match u32::try_from(n) {
Ok(n) if n > 0 => Ok(EndRule::RoundUp(n)),
_ => bail!("[song] end: round-up is a whole number of bars, 1 or more, not {n}"),
}
}
_ => Err(bad()),
}
}
impl Config {
pub fn built_in() -> Config {
Config {
end: EndRule::LastBar,
end_from: BUILT_IN.to_owned(),
lanes: LaneMap::gm(),
text_lanes: LaneMap::gm(),
text_notices: Vec::new(),
patches: Vec::new(),
lane_from: Vec::new(),
}
}
pub fn load(dirs: &Dirs) -> Result<Config> {
Config::load_from(&dirs.files())
}
pub fn load_from(files: &[PathBuf]) -> Result<Config> {
let mut config = Config::built_in();
for path in files {
if path.is_file() {
config
.apply(path)
.with_context(|| path.display().to_string())?;
}
}
(config.text_lanes, config.text_notices) = LaneMap::gm_for_text(&config.patches);
Ok(config)
}
fn apply(&mut self, path: &Path) -> Result<()> {
let text = fs::read_to_string(path)?;
let raw: RawFile = toml::from_str(&text)?;
let from = path.display().to_string();
if let Some(end) = raw.song.and_then(|s| s.end) {
self.end = end_rule(&end)?;
self.end_from.clone_from(&from);
}
let mut patches = Vec::new();
for (name, value) in raw.lanes.unwrap_or_default() {
let patch: LanePatch = value
.try_into()
.map_err(|e: toml::de::Error| anyhow!("[lanes.{name}]: {}", e.message()))?;
for (field, set) in [
("key", patch.key.is_some()),
("group", patch.group.is_some()),
("aliases", patch.aliases.is_some()),
] {
if set {
self.lane_from
.retain(|(l, f, _)| !(l == &name && *f == field));
self.lane_from.push((name.clone(), field, from.clone()));
}
}
patches.push((name, patch));
}
self.lanes = self.lanes.patched(&patches)?;
self.patches.extend(patches);
Ok(())
}
fn lane_source(&self, lane: &str, field: &str) -> &str {
self.lane_from
.iter()
.find(|(l, f, _)| l == lane && *f == field)
.map_or(BUILT_IN, |(_, _, s)| s)
}
pub fn show(&self) -> String {
let mut out = String::from("# duet config: every value, and where it came from\n\n");
let end = match self.end {
EndRule::RoundUp(n) => format!("{{ round-up = {n} }}"),
_ => "\"last-bar\"".to_owned(),
};
let _ = writeln!(out, "[song]\nend = {end} # {}", self.end_from);
for lane in self.lanes.lanes() {
let src = |field| self.lane_source(&lane.name, field);
let aliases: Vec<String> = lane.aliases.iter().map(|a| format!("\"{a}\"")).collect();
let _ = write!(
out,
"\n[lanes.{}]\nkey = {} # {}\ngroup = \"{}\" # {}\naliases = [{}] # {}\n",
lane.name,
lane.key,
src("key"),
lane.group,
src("group"),
aliases.join(", "),
src("aliases"),
);
}
if !self.text_notices.is_empty() {
out.push_str("\n# reading .drums text, map gm stays General MIDI:\n");
for notice in &self.text_notices {
let _ = writeln!(out, "# {notice}");
}
}
out
}
}
pub fn describe_paths(dirs: &Dirs) -> String {
let mut out = String::new();
match &dirs.user {
Some((dir, from)) => {
let _ = writeln!(out, "user config directory: {} ({from})", dir.display());
}
None => out.push_str("user config directory: none (no home directory found)\n"),
}
match &dirs.project {
Some(dir) => {
let _ = writeln!(out, "project config directory: {}", dir.display());
}
None => out.push_str("project config directory: none (no .duet directory found)\n"),
}
out.push_str("files, read in this order over the built-in defaults:\n");
for file in dirs.files() {
let state = if file.is_file() { "found" } else { "not found" };
let _ = writeln!(out, " {} ({state})", file.display());
}
out
}