Skip to main content

pedant_core/resolution/rust/
test_support.rs

1//! Proof-only access to resolution invariants ordinary callers cannot reach.
2
3use std::path::Path;
4
5use super::identity::TargetId;
6use super::project::RustProject;
7
8/// Proof-facing form of a lexical relative-path normalization failure.
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum RelativePathNormalizationError {
11    /// The path does not begin with the root's lexical components.
12    OutsideRoot,
13    /// A relative component has no UTF-8 representation.
14    NonUtf8,
15}
16
17/// Exercise the crate-private lexical path authority without widening the
18/// ordinary resolution surface.
19pub fn normalize_relative_path(
20    root: &Path,
21    path: &Path,
22) -> Result<Box<str>, RelativePathNormalizationError> {
23    super::super::path_normalization::relative_text(root, path).map_err(|error| match error {
24        super::super::path_normalization::RelativePathError::OutsideRoot => {
25            RelativePathNormalizationError::OutsideRoot
26        }
27        super::super::path_normalization::RelativePathError::NonUtf8 => {
28            RelativePathNormalizationError::NonUtf8
29        }
30    })
31}
32
33/// A defensively invalid target identity: this project's authority with a local
34/// index past every target it issued.
35///
36/// `RustSnapshotError::UnknownTarget` guards an invariant the safe public API
37/// cannot break, so proving both snapshot operations refuse it needs a
38/// construction path that only the proof feature compiles.
39pub fn unknown_target_id(project: &RustProject) -> TargetId {
40    let index = u32::try_from(project.targets().len()).unwrap_or(u32::MAX);
41    TargetId::new(project.authority, index)
42}