rm-lisa 0.3.2

A logging library for rem-verse, with support for inputs, tasks, and more.
Documentation
//! A very small module meant to use to track 'in-progress' tasks.

use std::fmt::{Display, Formatter, Result as FmtResult};
use valuable::Valuable;

/// The 'thread id', or 'tokio task' id that this executable task is running as.
#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq, Valuable)]
#[repr(transparent)]
pub struct ThreadOrTokioTaskId(pub u64);

impl Display for ThreadOrTokioTaskId {
	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
		write!(fmt, "{}", self.0)
	}
}

/// A task id unique to a thread or tokio task.
#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq, Valuable)]
#[repr(transparent)]
pub struct LisaTaskId(pub u64);

impl Display for LisaTaskId {
	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
		write!(fmt, "{}", self.0)
	}
}

/// A globally unique lisa task id.
pub type GloballyUniqueTaskId = (ThreadOrTokioTaskId, LisaTaskId);

/// The status of a particular task.
#[derive(Clone, Debug, PartialEq, Eq, Valuable)]
pub enum LisaTaskStatus {
	/// The task is not actively running.
	Inactive,
	/// The task is actively running, with optional information about what it's
	/// doing.
	Running(Option<String>),
	/// The task is waiting on something (e.g. a network resource), with optional
	/// information about what it's waiting on.
	Waiting(Option<String>),
}

impl Display for LisaTaskStatus {
	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
		write!(
			fmt,
			"{}",
			match self {
				LisaTaskStatus::Inactive => {
					"inactive".to_owned()
				}
				LisaTaskStatus::Running(reason) =>
					if let Some(real_reason) = reason {
						format!("running/doing={real_reason}")
					} else {
						"running".to_owned()
					},
				LisaTaskStatus::Waiting(reason) =>
					if let Some(real_reason) = reason {
						format!("waiting/on={real_reason}")
					} else {
						"waiting".to_owned()
					},
			},
		)
	}
}

/// A task event that has happened.
#[derive(Clone, Debug, PartialEq, Eq, Valuable)]
pub enum TaskEvent {
	/// A task has started.
	TaskStart(ThreadOrTokioTaskId, LisaTaskId, String, LisaTaskStatus),
	/// A task status has been updated.
	TaskStatusUpdate(ThreadOrTokioTaskId, LisaTaskId, LisaTaskStatus),
	/// A task has ended, and should no longer be displayed.
	TaskEnd(ThreadOrTokioTaskId, LisaTaskId),
}

/// A provider of a series of task events that have happened.
pub trait TaskEventLogProvider: Send + Sync {
	/// Get a list of task events that have occured since the last time this was called.
	///
	/// This should never return the same event twice.
	#[must_use]
	fn new_events(&self) -> Vec<TaskEvent>;
}