use std::io::{self, BufRead, Write};
use host_identity::{ResolveOutcome, UnknownSourceError, resolver_from_ids};
fn main() -> io::Result<()> {
let ids = read_ids();
if ids.is_empty() {
writeln!(
io::stderr(),
"no source identifiers on stdin; expected one per line"
)?;
std::process::exit(2);
}
let resolver = resolver_from_ids(&ids).unwrap_or_else(|err| {
let _ = writeln!(io::stderr(), "{}", describe_build_error(&err));
std::process::exit(1);
});
println!("chain: {:?}", resolver.source_kinds());
match resolver.resolve() {
Ok(id) => println!("resolved: {}", id.summary()),
Err(err) => {
for outcome in resolver.resolve_all() {
if let ResolveOutcome::Errored(kind, e) = outcome {
writeln!(io::stderr(), " {kind}: {e}")?;
}
}
writeln!(io::stderr(), "resolve failed: {err}")?;
std::process::exit(1);
}
}
Ok(())
}
fn read_ids() -> Vec<String> {
io::stdin()
.lock()
.lines()
.map_while(Result::ok)
.map(|l| l.trim().to_owned())
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.collect()
}
fn describe_build_error(err: &UnknownSourceError) -> String {
match err {
UnknownSourceError::Unknown(id) => format!("unknown source identifier: `{id}`"),
UnknownSourceError::RequiresPath(id) => format!(
"source `{id}` requires a caller-supplied path; \
push it manually with its typed constructor"
),
UnknownSourceError::RequiresTransport(id) => format!(
"source `{id}` is a cloud source; use \
resolver_from_ids_with_transport with an HTTP client"
),
UnknownSourceError::FeatureDisabled(id, feat) => format!(
"source `{id}` requires the `{feat}` feature, \
which isn't enabled in this build"
),
}
}