Skip to main content

sift_core/search/output/
mod.rs

1pub mod error;
2pub mod format;
3pub mod mode;
4pub mod passthru;
5pub mod style;
6
7use crate::query::CandidateRequirement;
8use mode::{OutputEmission, SearchMode, ZeroCountMode};
9use passthru::PassthruMode;
10use style::{SearchLineStyle, SearchRecordStyle};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum SearchOutputFormat {
14    #[default]
15    Text,
16    Json,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct SearchOutput {
21    pub format: SearchOutputFormat,
22    pub mode: SearchMode,
23    pub emission: OutputEmission,
24    pub lines: SearchLineStyle,
25    pub records: SearchRecordStyle,
26    pub passthru: PassthruMode,
27    pub include_zero: ZeroCountMode,
28}
29
30impl Default for SearchOutput {
31    fn default() -> Self {
32        Self {
33            format: SearchOutputFormat::Text,
34            mode: SearchMode::Standard,
35            emission: OutputEmission::Normal,
36            lines: SearchLineStyle::default(),
37            records: SearchRecordStyle::default(),
38            passthru: PassthruMode::Disabled,
39            include_zero: ZeroCountMode::Omit,
40        }
41    }
42}
43
44impl SearchOutput {
45    /// Whether search needs complete candidate coverage or only potential matches.
46    #[must_use]
47    pub(crate) const fn candidate_requirement(self) -> CandidateRequirement {
48        match self.mode {
49            SearchMode::Count | SearchMode::FilesWithoutMatch => CandidateRequirement::Complete,
50            SearchMode::CountMatches if matches!(self.include_zero, ZeroCountMode::Include) => {
51                CandidateRequirement::Complete
52            }
53            SearchMode::Standard
54            | SearchMode::OnlyMatching
55            | SearchMode::CountMatches
56            | SearchMode::FilesWithMatches => CandidateRequirement::PotentialMatches,
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn search_output_defaults() {
67        let output = SearchOutput::default();
68        assert_eq!(output.format, SearchOutputFormat::Text);
69        assert_eq!(output.mode, SearchMode::Standard);
70        assert_eq!(output.emission, OutputEmission::Normal);
71        assert!(matches!(output.passthru, PassthruMode::Disabled));
72        assert!(matches!(output.include_zero, ZeroCountMode::Omit));
73    }
74}