use crate::evaluation_diagnostics::{EventStage, Span};
use crate::phase_events::{EventScope, Outcome as EventOutcome, RootIdentity, RootKind};
use serde::Serialize;
use std::cell::{Cell, RefCell};
pub const ROOT_NAME_LIMIT: usize = 256;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SourceKind {
Expr,
Doc,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
Bound,
Defer,
EvalError,
Taint,
Skipped,
Unwound,
Incomplete,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Completion {
Published,
DeferredQueued,
DeferIgnored,
EvalError,
Taint,
Skipped,
}
#[derive(Serialize)]
pub struct RootRecord {
pub root_name: String,
pub root_name_original_bytes: usize,
pub root_name_truncated: bool,
pub initial_phase: u8,
pub source_kind: SourceKind,
pub producer_calls: usize,
pub source_successes: usize,
pub binding_returns: usize,
pub dispatch_outcome: Option<Outcome>,
pub completion: Option<Completion>,
pub outcome: Outcome,
pub interrupted_stage: Option<&'static str>,
pub span: Span,
}
struct State {
record: RootRecord,
active: &'static str,
events: EventScope,
}
thread_local! {
static ROOTS: RefCell<Vec<RootRecord>> = const { RefCell::new(Vec::new()) };
static NEXT_ORDINAL: Cell<usize> = const { Cell::new(0) };
}
pub struct RootAttempt {
state: RefCell<Option<State>>,
}
impl RootAttempt {
pub fn new(name: &str, initial_phase: u8, source_kind: SourceKind) -> Self {
let mut end = name.len().min(ROOT_NAME_LIMIT);
while !name.is_char_boundary(end) {
end -= 1;
}
let root_name = name[..end].to_owned();
let ordinal = NEXT_ORDINAL
.try_with(|next| {
let ordinal = next.get();
next.set(ordinal.saturating_add(1));
ordinal
})
.unwrap_or(usize::MAX);
let identity = RootIdentity::new(
name,
match source_kind {
SourceKind::Expr => RootKind::Expression,
SourceKind::Doc => RootKind::Document,
},
);
Self {
state: RefCell::new(Some(State {
record: RootRecord {
root_name,
root_name_original_bytes: name.len(),
root_name_truncated: end != name.len(),
initial_phase,
source_kind,
producer_calls: 0,
source_successes: 0,
binding_returns: 0,
dispatch_outcome: None,
completion: None,
outcome: Outcome::Incomplete,
interrupted_stage: None,
span: Span::new("root_bind", ordinal),
},
active: "dispatch",
events: EventScope::begin(
EventStage::RootDispatch as u32,
ordinal as u64,
identity,
),
})),
}
}
pub fn source_started(&self) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.producer_calls += 1;
state.active = "source_eval";
state.events.transition(EventStage::RootSourceEval as u32);
}
pub fn source_evaluated(&self) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.source_successes += 1;
state.active = "recursive_bind";
state.events.transition(EventStage::RootTypeBind as u32);
}
pub fn binding_returned(&self) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.binding_returns += 1;
state.active = "dispatch_return";
state
.events
.transition(EventStage::RootDispatchReturn as u32);
}
pub fn dispatch_returned(&self, outcome: Outcome) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.dispatch_outcome = Some(outcome);
state.active = "publish_or_handle";
state
.events
.transition(EventStage::RootPublishOrDefer as u32);
}
pub fn body_returned(&self, completion: Completion) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.completion = Some(completion);
state.active = "scope_release";
state.events.transition(EventStage::RootScopeRelease as u32);
}
pub fn skipped(&self) {
let mut state = self.state.borrow_mut();
let state = state.as_mut().unwrap();
state.record.span.mark(state.active);
state.record.completion = Some(Completion::Skipped);
state.active = "scope_release";
state.events.transition(EventStage::RootScopeRelease as u32);
}
}
impl Drop for RootAttempt {
fn drop(&mut self) {
let Some(mut state) = self.state.get_mut().take() else {
return;
};
let unwound = std::thread::panicking();
state.record.span.mark(state.active);
state.record.outcome = if unwound {
Outcome::Unwound
} else if state.record.completion == Some(Completion::Skipped) {
Outcome::Skipped
} else if state.record.completion.is_some() {
state.record.dispatch_outcome.unwrap_or(Outcome::Incomplete)
} else {
Outcome::Incomplete
};
if unwound || state.record.completion.is_none() {
state.record.interrupted_stage = Some(state.active);
}
state.events.finish(match state.record.outcome {
Outcome::Bound | Outcome::Skipped => EventOutcome::Ok,
Outcome::Defer => EventOutcome::Defer,
Outcome::EvalError => EventOutcome::Eval,
Outcome::Taint => EventOutcome::Taint,
Outcome::Unwound => EventOutcome::Unwound,
Outcome::Incomplete => EventOutcome::Incomplete,
});
let _ = ROOTS.try_with(|roots| roots.borrow_mut().push(state.record));
}
}
pub fn take_root_binding_diagnostics() -> Vec<RootRecord> {
ROOTS.with(|roots| std::mem::take(&mut *roots.borrow_mut()))
}
#[cfg(test)]
#[path = "../tests/private/root_binding_diagnostics_test.rs"]
mod tests;