Skip to main content

pedant_core/resolution/rust/snapshot/
source.rs

1//! The source view: one snapshotted Rust file, its exact bytes, and its IR.
2
3use std::sync::Arc;
4
5use crate::ir::FileIr;
6
7/// One Rust source a snapshot reached, read and parsed exactly once.
8#[derive(Debug)]
9pub struct RustSource {
10    pub(super) path: Arc<str>,
11    pub(super) text: Arc<str>,
12    pub(super) digest: [u8; 32],
13    pub(super) ir: FileIr,
14}
15
16impl RustSource {
17    /// The repository-relative, `/`-separated path.
18    pub fn path(&self) -> &str {
19        &self.path
20    }
21
22    /// The same path, shared rather than copied, for the spans that name it.
23    pub(in crate::resolution::rust) fn shared_path(&self) -> &Arc<str> {
24        &self.path
25    }
26
27    /// The exact UTF-8 text this snapshot read.
28    pub fn text(&self) -> &str {
29        &self.text
30    }
31
32    /// SHA-256 of the exact bytes behind [`Self::text`].
33    pub fn digest(&self) -> &[u8; 32] {
34        &self.digest
35    }
36
37    /// The one-pass IR extracted while the source was snapshotted.
38    pub fn ir(&self) -> &FileIr {
39        &self.ir
40    }
41}
42
43/// The source at `path`, when the sorted slice holds one.
44pub(super) fn find<'a>(sources: &'a [RustSource], path: &str) -> Option<&'a RustSource> {
45    sources
46        .binary_search_by(|source| (*source.path).cmp(path))
47        .ok()
48        .and_then(|index| sources.get(index))
49}