use std::time::{Instant, SystemTime};
use strum_macros::{Display, EnumMessage};
use crate::tools::get_current_timestamp;
use super::*;
#[cfg_attr(
feature = "python-bindings",
gen_stub_pyclass_complex_enum,
pyclass(from_py_object)
)]
#[derive(EnumMessage, Debug, Clone, Display, PartialEq)]
pub enum StreamStatus {
#[strum(message = "NotRunning", detailed_message = "Stream is not running")]
NotRunning {},
#[strum(message = "Running", detailed_message = "Stream is running")]
Running {
start_time: SystemTime,
},
#[strum(
message = "Error",
detailed_message = "An error occured with the stream"
)]
Error {
e: StreamError,
},
}
#[cfg(feature = "python-bindings")]
#[cfg_attr(feature = "python-bindings", gen_stub_pymethods)]
#[cfg_attr(feature = "python-bindings", pymethods)]
impl StreamStatus {
fn __eq__(&self, other: &Self) -> bool {
self == other
}
pub fn hasError(&self) -> bool {
matches!(self, StreamStatus::Error { .. })
}
pub fn getError(&self) -> Option<StreamError> {
use StreamStatus::*;
if let Error { e } = self {
Some(e.clone())
} else {
None
}
}
fn secondsRunning(&self) -> u64 {
match self {
StreamStatus::NotRunning {} => 0,
StreamStatus::Running { start_time } => start_time
.elapsed()
.expect("System clock has run backward")
.as_secs(),
StreamStatus::Error { .. } => 0,
}
}
}
impl StreamStatus {
pub fn newRunning() -> StreamStatus {
StreamStatus::Running {
start_time: SystemTime::now(),
}
}
}