Skip to main content

rfc/models/
search.rs

1use serde::Serialize;
2
3use super::Document;
4
5/// Filter for search results
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
7pub enum SearchFilter {
8    /// Only return RFCs
9    RfcsOnly,
10    /// Only return Internet-Drafts
11    DraftsOnly,
12    /// Return both RFCs and drafts
13    #[default]
14    Both,
15}
16
17impl SearchFilter {
18    /// Get the API parameter value for this filter
19    pub fn api_param(&self) -> Option<&'static str> {
20        match self {
21            SearchFilter::RfcsOnly => Some("rfc"),
22            SearchFilter::DraftsOnly => Some("draft"),
23            SearchFilter::Both => None,
24        }
25    }
26}
27
28/// Search results from the API
29#[derive(Debug, Clone, Default, Serialize)]
30pub struct SearchResult {
31    /// List of matching documents
32    pub documents: Vec<Document>,
33    /// Whether there are more results available
34    pub has_more: bool,
35    /// Total number of matching documents available (from API)
36    pub total_count: Option<u32>,
37    /// The query that produced these results
38    pub query: String,
39    /// The filter that was applied
40    pub filter: SearchFilter,
41}
42
43impl SearchResult {
44    /// Check if this result set is empty
45    #[must_use]
46    pub fn is_empty(&self) -> bool {
47        self.documents.is_empty()
48    }
49
50    /// Get the number of documents in this result set
51    #[must_use]
52    pub fn len(&self) -> usize {
53        self.documents.len()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_search_filter_api_param() {
63        assert_eq!(SearchFilter::RfcsOnly.api_param(), Some("rfc"));
64        assert_eq!(SearchFilter::DraftsOnly.api_param(), Some("draft"));
65        assert_eq!(SearchFilter::Both.api_param(), None);
66    }
67
68    #[test]
69    fn test_search_filter_default() {
70        assert_eq!(SearchFilter::default(), SearchFilter::Both);
71    }
72
73    #[test]
74    fn test_search_result_default() {
75        let result = SearchResult::default();
76
77        assert!(result.is_empty());
78        assert_eq!(result.len(), 0);
79        assert!(!result.has_more);
80        assert!(result.query.is_empty());
81        assert_eq!(result.filter, SearchFilter::Both);
82    }
83}