mod affected;
mod documented;
pub(crate) mod modules;
mod setup;
pub(crate) mod surfaces;
mod symbols;
mod tests;
use crate::project::{Package, Project};
use documented::Documented;
use modules::{Crate, Summary};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use surfaces::{Configured, Surface};
pub(crate) struct Analysis {
pub(crate) root: PathBuf,
pub(crate) project: Project,
pub(crate) setup: Option<String>,
surfaces: Vec<Surface>,
crates: Mutex<HashMap<String, Arc<Summary>>>,
documented: Mutex<HashMap<String, Result<Arc<Documented>, String>>>,
}
impl Analysis {
pub(crate) fn new(
root: PathBuf,
project: Project,
setup: Option<String>,
configured: &[Configured],
) -> Self {
let mut analysis = Analysis {
root,
project,
setup,
surfaces: Vec::new(),
crates: Mutex::default(),
documented: Mutex::default(),
};
analysis.surfaces = configured.iter().map(|configured| analysis.resolve(configured)).collect();
analysis
}
pub(crate) fn abs(&self, rel: &str) -> PathBuf {
self.root.join(rel)
}
fn rel(&self, path: &Path) -> String {
match path.strip_prefix(&self.root) {
Ok(rel) if rel.as_os_str().is_empty() => ".".to_string(),
Ok(rel) => rel.to_string_lossy().replace('\\', "/"),
Err(_) => path.to_string_lossy().into_owned(),
}
}
fn krate(&self, package: &Package) -> Arc<Summary> {
let mut crates = self.crates.lock().unwrap_or_else(|p| p.into_inner());
let read = |path: &Path| std::fs::read_to_string(path).ok();
let loaded = crates
.entry(package.lib.clone())
.or_insert_with(|| Arc::new(Summary::of(&Crate::load(&package.lib, &package.root, &read))));
Arc::clone(loaded)
}
pub(crate) fn in_code(&self, names: &[String], files: &[String]) -> Vec<String> {
let mut paths: Vec<PathBuf> = files.iter().map(|file| self.abs(file)).collect();
paths.extend(self.surfaces.iter().filter_map(|surface| surface.file.clone()));
let texts: Vec<String> = paths.iter().filter_map(|path| std::fs::read_to_string(path).ok()).collect();
let held = |name: &String| {
texts.iter().any(|text| crate::language::text::identifiers(text).contains(&name.as_str()))
};
names.iter().filter(|name| held(name)).cloned().collect()
}
}