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, Default)]
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 SearchOutput {
31    /// Whether search needs complete candidate coverage or only potential matches.
32    #[must_use]
33    pub(crate) const fn candidate_requirement(self) -> CandidateRequirement {
34        match self.mode {
35            SearchMode::Count | SearchMode::FilesWithoutMatch => CandidateRequirement::Complete,
36            SearchMode::CountMatches if matches!(self.include_zero, ZeroCountMode::Include) => {
37                CandidateRequirement::Complete
38            }
39            SearchMode::Standard
40            | SearchMode::OnlyMatching
41            | SearchMode::CountMatches
42            | SearchMode::FilesWithMatches => CandidateRequirement::PotentialMatches,
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn search_output_defaults() {
53        let output = SearchOutput::default();
54        assert_eq!(output.format, SearchOutputFormat::Text);
55        assert_eq!(output.mode, SearchMode::Standard);
56        assert_eq!(output.emission, OutputEmission::Normal);
57        assert!(matches!(output.passthru, PassthruMode::Disabled));
58        assert!(matches!(output.include_zero, ZeroCountMode::Omit));
59    }
60}