use pedant_types::{ResolutionReportBuilder, ResolutionReportLimits, ResolutionTier};
use crate::resolution::rust::snapshot::RustResolutionSnapshot;
use crate::resolution::line_index::{LineIndex, index_sources};
use super::corpus::Corpus;
use super::error::RustResolutionError;
use super::graph::{self, Graph};
use super::index::{self, Index};
use super::promotion::Promotion;
use super::references::{self, ReferenceEntry};
use super::target::RustTargetResolution;
use super::units::{self, Units};
use super::{imports, records};
pub(super) struct Inventory<'snapshot> {
pub(super) graph: Graph,
pub(super) units: Units,
pub(super) index: Index,
pub(super) entries: Box<[ReferenceEntry]>,
lines: Box<[LineIndex<'snapshot>]>,
builder: ResolutionReportBuilder,
}
pub(super) fn inventory(
snapshot: &RustResolutionSnapshot,
tier: ResolutionTier,
) -> Result<Inventory<'_>, RustResolutionError> {
let mut builder = ResolutionReportBuilder::new(tier, ResolutionReportLimits::default());
let lines = index_sources(snapshot.sources());
let graph = graph::build(snapshot)?;
let units = units::add_units(&mut builder, snapshot)?;
let index = index::build(&mut builder, snapshot, (&graph, &units, &lines))?;
let entries =
references::add_references(&mut builder, snapshot, &graph, (&index, &units, &lines))?;
Ok(Inventory {
graph,
units,
index,
entries,
lines,
builder,
})
}
pub(super) fn finish<'snapshot, P: Promotion>(
inventory: Inventory<'snapshot>,
snapshot: &'snapshot RustResolutionSnapshot,
promotion: &P,
) -> Result<RustTargetResolution, RustResolutionError> {
let Inventory {
graph,
units,
index,
entries,
lines,
mut builder,
} = inventory;
let corpus = Corpus {
graph: &graph,
index: &index,
units: &units,
};
let imports = imports::bind(&corpus, snapshot, &entries)?;
records::write(
&mut builder,
&corpus,
(&imports, promotion),
(snapshot, &entries, snapshot.limits()),
)?;
RustTargetResolution::try_new_indexed(snapshot, builder.finish()?, lines)
}