use std::error::Error;
use std::path::{Path, PathBuf};
use crate::types::manifest::PackManifest;
use crate::types::pack::layout::PackLayout;
use crate::types::report::Diagnostics;
use crate::types::tree::TreePath;
use crate::types::tree::Walked;
pub(super) const DIR: &str = "packs/core";
const IDENTITY: &str =
"[pack]\nname = \"core\"\nversion = \"0.1.0\"\napi = 0\ndescription = \"d\"\n";
pub(super) fn paths(texts: &[&str]) -> Vec<TreePath> {
texts
.iter()
.filter_map(|text| TreePath::from_relative(Path::new(text)).ok())
.collect()
}
pub(super) fn refused_walk(texts: &[&str], refused: &[&str]) -> Walked {
Walked::new(
paths(texts),
Vec::new(),
refused.iter().map(PathBuf::from).collect(),
)
}
pub(super) fn layout(texts: &[&str]) -> PackLayout {
PackLayout::classify(Path::new(DIR), &refused_walk(texts, &[]))
.map(|(layout, _)| layout)
.unwrap_or_default()
}
pub(super) fn manifest_with(top: &str, rest: &str) -> Result<PackManifest, Box<dyn Error>> {
let text = format!("{top}\n{IDENTITY}{rest}");
let file = Path::new(DIR).join("pack.toml");
PackManifest::parse(&file, &text).map_err(|diagnostics| {
let shown: Vec<String> = diagnostics.into_iter().map(|d| d.to_string()).collect();
shown.join("; ").into()
})
}
pub(super) fn manifest(rest: &str) -> Result<PackManifest, Box<dyn Error>> {
manifest_with("", rest)
}
pub(super) fn shown(diagnostics: Diagnostics) -> Vec<String> {
diagnostics
.into_sorted()
.iter()
.map(ToString::to_string)
.collect()
}