rm_lisa/
tasks.rs

1//! A very small module meant to use to track 'in-progress' tasks.
2
3use std::fmt::{Display, Formatter, Result as FmtResult};
4use valuable::Valuable;
5
6/// The 'thread id', or 'tokio task' id that this executable task is running as.
7#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq, Valuable)]
8#[repr(transparent)]
9pub struct ThreadOrTokioTaskId(pub u64);
10
11impl Display for ThreadOrTokioTaskId {
12	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
13		write!(fmt, "{}", self.0)
14	}
15}
16
17/// A task id unique to a thread or tokio task.
18#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq, Valuable)]
19#[repr(transparent)]
20pub struct LisaTaskId(pub u64);
21
22impl Display for LisaTaskId {
23	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
24		write!(fmt, "{}", self.0)
25	}
26}
27
28/// A globally unique lisa task id.
29pub type GloballyUniqueTaskId = (ThreadOrTokioTaskId, LisaTaskId);
30
31/// The status of a particular task.
32#[derive(Clone, Debug, PartialEq, Eq, Valuable)]
33pub enum LisaTaskStatus {
34	/// The task is not actively running.
35	Inactive,
36	/// The task is actively running, with optional information about what it's
37	/// doing.
38	Running(Option<String>),
39	/// The task is waiting on something (e.g. a network resource), with optional
40	/// information about what it's waiting on.
41	Waiting(Option<String>),
42}
43
44impl Display for LisaTaskStatus {
45	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
46		write!(
47			fmt,
48			"{}",
49			match self {
50				LisaTaskStatus::Inactive => {
51					"inactive".to_owned()
52				}
53				LisaTaskStatus::Running(reason) =>
54					if let Some(real_reason) = reason {
55						format!("running/doing={real_reason}")
56					} else {
57						"running".to_owned()
58					},
59				LisaTaskStatus::Waiting(reason) =>
60					if let Some(real_reason) = reason {
61						format!("waiting/on={real_reason}")
62					} else {
63						"waiting".to_owned()
64					},
65			},
66		)
67	}
68}
69
70/// A task event that has happened.
71#[derive(Clone, Debug, PartialEq, Eq, Valuable)]
72pub enum TaskEvent {
73	/// A task has started.
74	TaskStart(ThreadOrTokioTaskId, LisaTaskId, String, LisaTaskStatus),
75	/// A task status has been updated.
76	TaskStatusUpdate(ThreadOrTokioTaskId, LisaTaskId, LisaTaskStatus),
77	/// A task has ended, and should no longer be displayed.
78	TaskEnd(ThreadOrTokioTaskId, LisaTaskId),
79}
80
81/// A provider of a series of task events that have happened.
82pub trait TaskEventLogProvider: Send + Sync {
83	/// Get a list of task events that have occured since the last time this was called.
84	///
85	/// This should never return the same event twice.
86	#[must_use]
87	fn new_events(&self) -> Vec<TaskEvent>;
88}