jarq 0.8.2

An interactive jq-like JSON query tool with a TUI
Documentation
use std::time::{Duration, Instant};

/// Async evaluation tracking
pub(super) struct EvalTracker {
    request_id: u64,
    pending_id: Option<u64>,
    pub(super) started_at: Option<Instant>,
}

impl EvalTracker {
    pub(super) fn new() -> Self {
        Self {
            request_id: 0,
            pending_id: None,
            started_at: None,
        }
    }

    pub(super) fn next_id(&mut self) -> u64 {
        self.request_id += 1;
        self.request_id
    }

    pub(super) fn start(&mut self, id: u64) {
        self.pending_id = Some(id);
        self.started_at = Some(Instant::now());
    }

    pub(super) fn cancel(&mut self) {
        self.pending_id = None;
        self.started_at = None;
    }

    pub(super) fn complete(&mut self, id: u64) -> bool {
        if Some(id) == self.pending_id {
            self.pending_id = None;
            self.started_at = None;
            true
        } else {
            false
        }
    }

    pub(super) fn is_evaluating(&self) -> bool {
        const EVAL_INDICATOR_DELAY: Duration = Duration::from_millis(100);
        self.started_at
            .is_some_and(|started| started.elapsed() >= EVAL_INDICATOR_DELAY)
    }

    #[cfg(test)]
    pub(super) fn has_pending(&self) -> bool {
        self.pending_id.is_some()
    }
}