use std::path::Path;
use std::time::Instant;
use crate::exit::{EXIT_FAILURE, EXIT_OK, EXIT_USAGE};
use crate::query;
use crate::render;
use crate::usage::READ_USAGE;
use super::answer::{
ambiguous_candidates_out, begin_validation, fallback_advised_out, finish_query,
member_ambiguous_out, with_next_trailer, ZERO_HIT_READ,
};
use super::args::{first_positional, index_options, output_flags, parse_pick};
use super::refs::{member_models_out, refs_model_row_count};
use super::root::{require_graph, require_repo};
use super::sections::{self, SectionAnswer};
pub(crate) fn cmd_read(cwd: &Path, args: &[String]) -> (i32, String, Option<&'static str>) {
let start = Instant::now();
let (json, compact) = match output_flags("read", args) {
Ok(v) => v,
Err((code, out)) => return (code, out, None),
};
let Ok(pick) = parse_pick(args) else {
return (EXIT_USAGE, READ_USAGE.to_string(), None);
};
let Some(q) = first_positional(args) else {
return (EXIT_USAGE, READ_USAGE.to_string(), None);
};
let root = match require_repo(cwd) {
Ok(r) => r,
Err(e) => return (EXIT_FAILURE, format!("error: {e}"), None),
};
begin_validation(&root, cwd);
let g = match require_graph(&root) {
Ok(g) => g,
Err((code, out)) => return (code, out, None),
};
let index = query::load_graph_index_with(&g, &root, index_options(args));
let answer = match (pick, query::build_read_model(&index, q)) {
(Some(pick), query::ReadResult::MemberAmbiguous(candidates)) => {
let picked = sections::narrow(
sections::Candidates::from_members(q, &candidates),
pick,
json,
READ_USAGE,
|picked| read_candidate_out(&index, q, json, compact, picked),
);
match picked {
Ok(a) => a,
Err(e) => return (e.0, e.1, None),
}
}
(Some(pick), query::ReadResult::Ambiguous(ids)) => {
let picked = sections::narrow(
sections::Candidates::from_types(q, &index, &ids),
pick,
json,
READ_USAGE,
|picked| read_candidate_out(&index, q, json, compact, picked),
);
match picked {
Ok(a) => a,
Err(e) => return (e.0, e.1, None),
}
}
(_, result) => read_answer(&index, q, json, compact, result),
};
let note = (answer.outcome != query::Outcome::ZeroHit).then_some(ZERO_HIT_READ);
let (code, out) = finish_query(&root, "read", q, start, json, answer.into_tuple());
(code, out, note)
}
fn read_candidate_out(
index: &query::GraphIndex,
q: &str,
json: bool,
compact: bool,
picked: &query::PickedSeed,
) -> SectionAnswer {
read_answer(index, q, json, compact, query::pick_read(index, picked))
}
fn read_answer(
index: &query::GraphIndex,
q: &str,
json: bool,
compact: bool,
result: query::ReadResult,
) -> SectionAnswer {
match result {
query::ReadResult::NotFound => {
let (code, out) = fallback_advised_out(q, json, format!("no symbol matches \"{q}\""));
SectionAnswer::new(code, out, query::Outcome::FallbackAdvised, 0, Vec::new())
}
query::ReadResult::Ambiguous(ids) => {
let count = ids.len();
let (code, out) = ambiguous_candidates_out(index, q, &ids, json);
SectionAnswer::new(code, out, query::Outcome::Ambiguous, count, Vec::new())
}
query::ReadResult::MemberAmbiguous(candidates) => {
let count = candidates.len();
let (code, out) = member_ambiguous_out(q, &candidates, json);
SectionAnswer::new(code, out, query::Outcome::Ambiguous, count, Vec::new())
}
query::ReadResult::Members(models) => member_models_out(index, q, json, compact, &models),
query::ReadResult::Resolved(model) => {
let count = refs_model_row_count(&model.refs);
let next = query::refs_next(index, &model.refs);
let out = if json {
query::json::read_model_to_json_with_next(&model, &next)
} else if compact {
with_next_trailer(render::render_read_compact(&model), &next)
} else {
with_next_trailer(render::render_read_text(&model), &next)
};
SectionAnswer::new(EXIT_OK, out, query::Outcome::Hit, count, next)
}
}
}