Skip to content

Commit

Permalink
feat(turbopack_core): define trait for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 17, 2023
1 parent d24075f commit 2dc2224
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions crates/turbopack-core/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use anyhow::Result;
use async_trait::async_trait;
use turbo_tasks::{emit, CollectiblesSource, Upcast, Vc};

/// An arbitrary payload can be used to analyze, diagnose
/// Turbopack's behavior.
#[turbo_tasks::value_trait]
pub trait Diagnostic {
// [TODO]: These are subject to change; not finalized yet.
fn category(&self) -> Vc<String>;
fn key(&self) -> Vc<String>;
fn value(&self) -> Vc<String>;
}

pub trait DiagnosticExt {
fn emit(self);
}

impl<T> DiagnosticExt for Vc<T>
where
T: Upcast<Box<dyn Diagnostic>>,
{
fn emit(self) {
let diagnostic = Vc::upcast::<Box<dyn Diagnostic>>(self);
emit(diagnostic);
}
}

#[async_trait]
pub trait DiagnosticContextExt
where
Self: Sized,
{
async fn peek_diagnostics_with_path(self) -> Result<Vc<CapturedDiagnostics>>;
}

#[async_trait]
impl<T> DiagnosticContextExt for T
where
T: CollectiblesSource + Copy + Send,
{
async fn peek_diagnostics_with_path(self) -> Result<Vc<CapturedDiagnostics>> {
Ok(CapturedDiagnostics::cell(CapturedDiagnostics {
diagnostics: self.peek_collectibles().strongly_consistent().await?,
}))
}
}

/// A list of diagnostics captured with
/// [`DiagnosticsVc::peek_diagnostics_with_path`] and
#[derive(Debug)]
#[turbo_tasks::value]
pub struct CapturedDiagnostics {
pub diagnostics: auto_hash_map::AutoSet<Vc<Box<dyn Diagnostic>>>,
}
1 change: 1 addition & 0 deletions crates/turbopack-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod chunk;
pub mod code_builder;
pub mod compile_time_info;
pub mod context;
pub mod diagnostics;
pub mod environment;
pub mod error;
pub mod file_source;
Expand Down

0 comments on commit 2dc2224

Please sign in to comment.