use std::fmt;
use std::mem;
use std::sync::Arc;
use super::kind::Kind;
use super::log::{LogLevel, LogRecord};
use super::sheet::{Row, RowState, Shared, Unit};
#[derive(Clone)]
enum Behind {
Nothing,
Above { sheet: Arc<Shared>, nearest: Arc<Row> },
Row { sheet: Arc<Shared>, row: Arc<Row>, below: Option<u32> },
}
impl Behind {
fn limited(mut self, levels: u32) -> Behind {
if let Behind::Row { below, .. } = &mut self {
*below = Some(below.map_or(levels, |below| below.min(levels)));
}
self
}
fn fmt_row(&self, name: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Behind::Row { row, .. } => write!(f, "{name}({})", row.id.0),
_ => write!(f, "{name}(no row)"),
}
}
}
#[derive(Clone)]
pub struct Scope(Behind);
impl Scope {
pub fn none() -> Scope {
Scope(Behind::Nothing)
}
pub fn child(&self, kind: &Kind, label: Option<&str>) -> Pending {
match &self.0 {
Behind::Nothing => Pending(Behind::Nothing),
Behind::Above { sheet, nearest } => {
Pending(Behind::Above { sheet: Arc::clone(sheet), nearest: Arc::clone(nearest) })
}
Behind::Row { sheet, row, below } => {
if *below == Some(0) || row.state() != RowState::Running {
Pending(Behind::Above { sheet: Arc::clone(sheet), nearest: Arc::clone(row) })
} else {
let child = sheet.declare(Some(row.id), kind, label);
Pending(Behind::Row {
sheet: Arc::clone(sheet),
row: child,
below: below.map(|below| below - 1),
})
}
}
}
}
pub fn with_depth_limit(&self, levels: u32) -> Scope {
Scope(self.0.clone().limited(levels))
}
pub fn cancelled(&self) -> bool {
let sheet = match &self.0 {
Behind::Nothing => return false,
Behind::Above { sheet, .. } | Behind::Row { sheet, .. } => sheet,
};
if let Some(checkpoint) = &sheet.observer.checkpoint {
checkpoint();
}
sheet.cancelled()
}
pub(crate) fn cancel_requested(&self) -> bool {
match &self.0 {
Behind::Nothing => false,
Behind::Above { sheet, .. } | Behind::Row { sheet, .. } => sheet.cancelled(),
}
}
pub fn log_enabled(&self, level: LogLevel) -> bool {
match &self.0 {
Behind::Nothing => false,
Behind::Above { sheet, .. } | Behind::Row { sheet, .. } => {
sheet.log_sink.as_ref().is_some_and(|sink| sink.enabled(level))
}
}
}
pub fn log(&self, level: LogLevel, text: &str) {
let (sheet, row) = match &self.0 {
Behind::Nothing => return,
Behind::Above { sheet, nearest: row } | Behind::Row { sheet, row, .. } => (sheet, row),
};
if let Some(sink) = &sheet.log_sink {
sink.log(&LogRecord {
level,
source: row.kind.name(),
text,
row: Some(row.id),
label: row.label.as_deref(),
});
}
}
}
impl fmt::Debug for Scope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_row("Scope", f)
}
}
#[must_use = "a declared row that is dropped ends as not run"]
pub struct Pending(Behind);
impl Pending {
pub fn with_depth_limit(mut self, levels: u32) -> Pending {
let behind = mem::replace(&mut self.0, Behind::Nothing);
Pending(behind.limited(levels))
}
pub fn start(mut self) -> Task {
let behind = mem::replace(&mut self.0, Behind::Nothing);
if let Behind::Row { sheet, row, .. } = &behind {
sheet.start(row, None);
}
Task(behind)
}
pub fn start_counted(mut self, unit: Unit, total: u64) -> Counted {
let behind = mem::replace(&mut self.0, Behind::Nothing);
if let Behind::Row { sheet, row, .. } = &behind {
sheet.start(row, Some((unit, total)));
}
Counted(behind)
}
pub fn start_counted_with(mut self, unit: Unit, total: impl FnOnce() -> u64) -> Counted {
let behind = mem::replace(&mut self.0, Behind::Nothing);
if let Behind::Row { sheet, row, .. } = &behind {
sheet.start(row, Some((unit, total())));
}
Counted(behind)
}
}
impl Drop for Pending {
fn drop(&mut self) {
if let Behind::Row { sheet, row, .. } = &self.0 {
sheet.not_run(row);
}
}
}
impl fmt::Debug for Pending {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_row("Pending", f)
}
}
#[must_use = "a running row that is dropped ends as failed"]
pub struct Task(Behind);
impl Task {
pub(super) fn running(sheet: Arc<Shared>, row: Arc<Row>) -> Task {
Task(Behind::Row { sheet, row, below: None })
}
pub fn scope(&self) -> Scope {
Scope(self.0.clone())
}
pub fn finish(mut self) {
end(&mut self.0, RowState::Finished);
}
}
impl Drop for Task {
fn drop(&mut self) {
end(&mut self.0, RowState::Failed);
}
}
impl fmt::Debug for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_row("Task", f)
}
}
#[must_use = "a running row that is dropped ends as failed"]
pub struct Counted(Behind);
impl Counted {
pub fn advance(&self, n: u64) {
if let Behind::Row { row, .. } = &self.0 {
row.done.fetch_add(n, std::sync::atomic::Ordering::Relaxed);
}
}
pub fn scope(&self) -> Scope {
Scope(self.0.clone())
}
pub fn finish(mut self) {
end(&mut self.0, RowState::Finished);
}
}
impl Drop for Counted {
fn drop(&mut self) {
end(&mut self.0, RowState::Failed);
}
}
impl fmt::Debug for Counted {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_row("Counted", f)
}
}
fn end(behind: &mut Behind, state: RowState) {
if let Behind::Row { sheet, row, .. } = mem::replace(behind, Behind::Nothing) {
sheet.end(&row, state);
}
}
const _: () = {
const fn send_sync<T: Send + Sync>() {}
send_sync::<Scope>();
send_sync::<Pending>();
send_sync::<Task>();
send_sync::<Counted>();
send_sync::<super::Sheet>();
};