use super::*;
pub(super) fn adopt_cmd(root: &Path, args: &[String]) -> ExitCode {
let dry = args.iter().any(|a| a == "--check");
match crate::adopt::unreadable(root) {
Ok(found) if !found.is_empty() => {
return adopt_unreadable(root, &found);
}
Ok(_) => {}
Err(e) => return adopt_failed(&e),
}
match flag_value(args, "--map") {
None => adopt_propose(root),
Some(file) => adopt_with_map(root, file, dry),
}
}
pub(super) fn adopt_unreadable(
root: &Path,
found: &[crate::spec::Unreadable],
) -> ExitCode {
let path = root.join("SPEC.md");
eprintln!(
"adopt: {}: {} id-shaped row(s) in a form this does not read",
path.display(),
found.len()
);
for u in found.iter().take(3) {
eprintln!(" line {}: {}", u.line, u.text.trim());
}
if found.len() > 3 {
eprintln!(" ... and {} more", found.len().saturating_sub(3));
}
eprintln!(
" a row opens its line with the id: `T1|status|task|cites`, \
pipe-delimited (FORMAT.md). convert the table and run this again."
);
ExitCode::from(2)
}
pub(super) fn flag_value<'a>(
args: &'a [String],
flag: &str,
) -> Option<&'a String> {
let at = args.iter().position(|a| a == flag)?;
args.get(at.saturating_add(1))
.filter(|v| !v.starts_with("--"))
}
pub(super) fn adopt_propose(root: &Path) -> ExitCode {
let proposal = match crate::adopt::propose(root) {
Ok(p) => p,
Err(e) => return adopt_failed(&e),
};
for p in &proposal.placements {
println!("{} {}\t# {}", p.id, p.home, p.why.join(" "));
}
adopt_summary(&proposal);
if proposal.placements.is_empty() {
return ExitCode::SUCCESS;
}
ExitCode::from(1)
}
pub(super) fn adopt_summary(p: &crate::adopt::Proposal) {
eprintln!(
" {} rows read · {} placed · {} unplaced",
p.rows_in,
p.placements.len(),
p.unplaced.len()
);
if !p.unplaced.is_empty() {
eprintln!(" staying at root: {}", p.unplaced.join(" "));
}
}
pub(super) fn adopt_with_map(root: &Path, file: &str, dry: bool) -> ExitCode {
let map = match read_map_file(file) {
Ok(m) => m,
Err(e) => return adopt_failed(&e),
};
if dry {
return adopt_dry(root, &map);
}
match crate::adopt::apply(root, &map) {
Ok(r) => adopt_wrote(&r),
Err(e) => adopt_failed(&e),
}
}
pub(super) fn read_map_file(
file: &str,
) -> Result<std::collections::BTreeMap<String, String>, String> {
let text = std::fs::read_to_string(file)
.map_err(|e| format!("adopt: {file}: {e}"))?;
crate::adopt::read_map(&text)
}
pub(super) fn adopt_dry(
root: &Path,
map: &std::collections::BTreeMap<String, String>,
) -> ExitCode {
let refused = crate::adopt::refusals(root, map);
for r in &refused {
eprintln!("{r}");
}
if refused.is_empty() {
eprintln!(" {} rows would move · nothing written", map.len());
return ExitCode::SUCCESS;
}
ExitCode::from(1)
}
pub(super) fn adopt_wrote(r: &crate::adopt::Report) -> ExitCode {
for f in &r.files {
println!("{f}");
}
for v in &r.carried {
eprintln!(" carried in, not caused here: {v}");
}
eprintln!(
" {} rows read · {} moved · {} stayed at root · {} files written",
r.rows_in,
r.moved,
r.stayed,
r.files.len()
);
ExitCode::from(1)
}
pub(super) fn adopt_failed(msg: &str) -> ExitCode {
eprintln!("{msg}");
ExitCode::from(1)
}
#[cfg(test)]
#[path = "tests/adopt.rs"]
mod tests;