use crate::{prelude::*, LintContext};
use guppy::graph::PackageGraph;
use hakari::Hakari;
use std::path::{Path, PathBuf};
use x_core::{WorkspaceSubset, XCoreContext};
pub trait ProjectLinter: Linter {
#[allow(clippy::trivially_copy_pass_by_ref)]
fn run<'l>(
&self,
ctx: &ProjectContext<'l>,
out: &mut LintFormatter<'l, '_>,
) -> Result<RunStatus<'l>>;
}
#[derive(Debug)]
pub struct ProjectContext<'l> {
core: &'l XCoreContext,
}
impl<'l> ProjectContext<'l> {
pub fn new(core: &'l XCoreContext) -> Self {
Self { core }
}
pub fn core(&self) -> &'l XCoreContext {
self.core
}
pub fn project_root(&self) -> &'l Path {
self.core.project_root()
}
pub fn package_graph(&self) -> Result<&'l PackageGraph> {
Ok(self.core.package_graph()?)
}
pub fn full_path(&self, path: impl AsRef<Path>) -> PathBuf {
self.core.project_root().join(path.as_ref())
}
pub fn default_members(&self) -> Result<&WorkspaceSubset> {
Ok(self.core.subsets()?.default_members())
}
pub fn hakari(&self) -> Result<Hakari<'l, 'static>> {
Ok(self.core.hakari_builder()?.compute())
}
}
impl<'l> LintContext<'l> for ProjectContext<'l> {
fn kind(&self) -> LintKind<'l> {
LintKind::Project
}
}