use pedant_types::{ResolutionReportBuilder, ResolutionReportLimits, ResolutionTier};
use crate::resolution::rust::snapshot::RustResolutionSnapshot;
use super::coordinates::{self, LineIndex};
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 {
pub(super) graph: Graph,
pub(super) units: Units,
pub(super) index: Index,
pub(super) entries: Box<[ReferenceEntry]>,
builder: ResolutionReportBuilder,
}
pub(super) fn inventory(
snapshot: &RustResolutionSnapshot,
tier: ResolutionTier,
) -> Result<Inventory, RustResolutionError> {
let mut builder = ResolutionReportBuilder::new(tier, ResolutionReportLimits::default());
let lines: Box<[LineIndex]> = coordinates::index_sources(snapshot);
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,
builder,
})
}
pub(super) fn finish<P: Promotion>(
inventory: Inventory,
snapshot: &RustResolutionSnapshot,
promotion: &P,
) -> Result<RustTargetResolution, RustResolutionError> {
let Inventory {
graph,
units,
index,
entries,
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(snapshot, builder.finish()?)
}