use super::*;
pub(super) fn adopt_cmd(
root: &Path,
dir: &str,
map: Option<&str>,
dry: bool,
) -> ExitCode {
let node = adopt_node(root, &arg_dir(Some(dir), root));
match crate::adopt::unreadable_at(root, &node) {
Ok(found) if !found.is_empty() => {
return adopt_unreadable_at(root, &node, &found);
}
Ok(_) => {}
Err(e) => return adopt_unusable(&e),
}
match map {
None => adopt_propose_at(root, &node),
Some(file) => adopt_with_map((root, &node), file, dry),
}
}
#[cfg(test)]
pub(super) fn adopt_unreadable(
root: &Path,
found: &[crate::spec::Unreadable],
) -> ExitCode {
adopt_unreadable_at(root, ".", found)
}
pub(super) fn adopt_unreadable_at(
root: &Path,
node: &str,
found: &[crate::spec::Unreadable],
) -> ExitCode {
let dir = if node == "." {
root.to_path_buf()
} else {
root.join(node)
};
let path = dir.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)
}
#[cfg(test)]
pub(super) fn adopt_propose(root: &Path) -> ExitCode {
adopt_propose_at(root, ".")
}
pub(super) fn adopt_propose_at(root: &Path, node: &str) -> ExitCode {
let proposal = match crate::adopt::propose_at(root, node) {
Ok(p) => p,
Err(e) => return adopt_unusable(&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, node): (&Path, &str),
file: &str,
dry: bool,
) -> ExitCode {
let map = match read_map_file(file) {
Ok(m) => m,
Err(e) => return adopt_unusable(&e),
};
if dry {
return adopt_dry((root, node), &map);
}
match crate::adopt::apply_at(root, node, &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, node): (&Path, &str),
map: &std::collections::BTreeMap<String, String>,
) -> ExitCode {
let refused = crate::adopt::refusals_at(root, node, 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)
}
pub(super) fn adopt_unusable(msg: &str) -> ExitCode {
eprintln!("{msg}");
ExitCode::from(2)
}
#[cfg(test)]
#[path = "tests/adopt.rs"]
mod tests;
pub(super) fn adopt_node(root: &Path, dir: &Path) -> String {
let rel = dir.strip_prefix(root).unwrap_or(dir);
let parts: Vec<String> = rel
.components()
.filter(|c| !matches!(c, std::path::Component::CurDir))
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect();
if parts.is_empty() {
".".into()
} else {
parts.join("/")
}
}