lasprs 0.14.1

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
//! Provides stream messages that come from a running stream
use std::time::{Instant, SystemTime};
use strum_macros::{Display, EnumMessage};

use crate::tools::get_current_timestamp;

use super::*;

/// Gives the stream status of a (possible) stream, either input / output or duplex.
#[cfg_attr(
    feature = "python-bindings",
    gen_stub_pyclass_complex_enum,
    pyclass(from_py_object)
)]
#[derive(EnumMessage, Debug, Clone, Display, PartialEq)]
pub enum StreamStatus {
    /// Stream is not running
    #[strum(message = "NotRunning", detailed_message = "Stream is not running")]
    NotRunning {},
    /// Stream is running properly
    #[strum(message = "Running", detailed_message = "Stream is running")]
    Running {
        /// Start time as UTC unix time stamp
        start_time: SystemTime,
    },

    /// An error occured in the stream.
    #[strum(
        message = "Error",
        detailed_message = "An error occured with the stream"
    )]
    Error {
        /// In case the stream has an error: e is the field name
        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
    }
    /// Whether the stream has an error.
    pub fn hasError(&self) -> bool {
        matches!(self, StreamStatus::Error { .. })
    }
    /// Get the error if the stream has an error.
    pub fn getError(&self) -> Option<StreamError> {
        use StreamStatus::*;
        if let Error { e } = self {
            Some(e.clone())
        } else {
            None
        }
    }
    /// Number of seconds the stream is currently running
    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 {
    /// Create a new `running` stream status, where start time is initialized.
    pub fn newRunning() -> StreamStatus {
        StreamStatus::Running {
            start_time: SystemTime::now(),
        }
    }
}