magi-code 0.62.0

Repository-aware CLI coding agent for terminal work
Documentation
use std::time::{Duration, Instant};

pub(crate) const TOAST_DURATION: Duration = Duration::from_millis(1800);
pub(crate) const COPIED_TO_CLIPBOARD: &str = "Copied to clipboard";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ToastSeverity {
    Success,
    Error,
}

#[derive(Debug, Clone)]
pub(crate) struct ToastState {
    pub(crate) message: String,
    pub(crate) severity: ToastSeverity,
    expires_at: Instant,
}

impl ToastState {
    pub(crate) fn new(message: impl Into<String>, severity: ToastSeverity, now: Instant) -> Self {
        Self {
            message: message.into(),
            severity,
            expires_at: now + TOAST_DURATION,
        }
    }

    pub(crate) fn expires_at(&self) -> Instant {
        self.expires_at
    }

    pub(crate) fn expired(&self, now: Instant) -> bool {
        now >= self.expires_at
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn toast_expires_after_duration() {
        let now = Instant::now();
        let toast = ToastState::new(COPIED_TO_CLIPBOARD, ToastSeverity::Success, now);
        assert!(!toast.expired(now + TOAST_DURATION - Duration::from_millis(1)));
        assert!(toast.expired(now + TOAST_DURATION));
    }
}