Skip to main content

euv_docs/model/
impl.rs

1use super::*;
2
3/// Search and lookup utilities for `DocSite`.
4impl DocSite {
5    /// Searches pages matching `query` (case-insensitive) across title, route, and content blocks.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Documentation site reference.
10    /// - `&str` - Search query text.
11    ///
12    /// # Returns
13    ///
14    /// - `Vec<&DocPage>` - Matching pages in display order.
15    #[must_use]
16    pub fn search(&self, query: &str) -> Vec<&DocPage> {
17        let needle: String = query.trim().to_ascii_lowercase();
18        if needle.is_empty() {
19            return self.get_pages().iter().collect();
20        }
21        self.get_pages()
22            .iter()
23            .filter(|page: &&DocPage| page.matches_query(&needle))
24            .collect()
25    }
26
27    /// Looks up a page by its canonical route path.
28    ///
29    /// # Arguments
30    ///
31    /// - `&Self` - Documentation site reference.
32    /// - `&str` - Route path to locate.
33    ///
34    /// # Returns
35    ///
36    /// - `Option<&DocPage>` - Matching page if present.
37    #[must_use]
38    pub fn find_by_route(&self, route: &str) -> Option<&DocPage> {
39        self.get_pages()
40            .iter()
41            .find(|page: &&DocPage| page.get_route() == route)
42    }
43
44    /// Builds compact full-text search index entries for every page in this site.
45    ///
46    /// # Arguments
47    ///
48    /// - `&Self` - Documentation site reference.
49    ///
50    /// # Returns
51    ///
52    /// - `Vec<SearchIndexEntry>` - Ordered search records ready for JSON serialization.
53    #[must_use]
54    pub fn search_entries(&self) -> Vec<SearchIndexEntry> {
55        self.get_pages()
56            .iter()
57            .map(|page: &DocPage| {
58                let headings: Vec<String> = page
59                    .get_toc()
60                    .iter()
61                    .map(|item: &DocTocItem| item.get_title().clone())
62                    .collect();
63                let mut excerpt: String = String::new();
64                for block in page.get_blocks().iter() {
65                    if excerpt.len() >= 360 {
66                        break;
67                    }
68                    match block {
69                        DocBlock::Paragraph(text)
70                        | DocBlock::ListItem(text)
71                        | DocBlock::BlockQuote(text) => {
72                            if !excerpt.is_empty() {
73                                excerpt.push(' ');
74                            }
75                            excerpt.push_str(text);
76                        }
77                        DocBlock::Table { headers, rows } => {
78                            for h in headers.iter() {
79                                if !excerpt.is_empty() {
80                                    excerpt.push(' ');
81                                }
82                                excerpt.push_str(h);
83                            }
84                            for r in rows.iter().take(3) {
85                                for cell in r.iter() {
86                                    if !excerpt.is_empty() {
87                                        excerpt.push(' ');
88                                    }
89                                    excerpt.push_str(cell);
90                                }
91                            }
92                        }
93                        DocBlock::Image { alt, .. } => {
94                            if !alt.is_empty() {
95                                if !excerpt.is_empty() {
96                                    excerpt.push(' ');
97                                }
98                                excerpt.push_str(alt);
99                            }
100                        }
101                        DocBlock::Heading { .. }
102                        | DocBlock::Code { .. }
103                        | DocBlock::HorizontalRule => {}
104                    }
105                }
106                let trimmed: String = excerpt.chars().take(360).collect();
107                SearchIndexEntry::new(
108                    page.get_route().clone(),
109                    page.get_title().clone(),
110                    page.get_section().clone(),
111                    page.get_summary().clone(),
112                    headings,
113                    trimmed,
114                )
115            })
116            .collect()
117    }
118}
119
120/// Query matching and body attachment helpers for `DocPage`.
121impl DocPage {
122    /// Stores the raw Markdown body slice for full-fidelity HTML generation.
123    ///
124    /// # Arguments
125    ///
126    /// - `&Self` - Mutable documentation page reference.
127    /// - `String` - Raw Markdown body text without frontmatter.
128    pub(crate) fn set_markdown_body(&mut self, body: String) {
129        self.markdown_body = body;
130    }
131
132    /// Returns `true` if `needle` (already lowercased) matches the title, route, summary, or any block.
133    ///
134    /// # Arguments
135    ///
136    /// - `&Self` - Documentation page reference.
137    /// - `&str` - Lowercased search query.
138    ///
139    /// # Returns
140    ///
141    /// - `bool` - Whether the page matches.
142    #[must_use]
143    pub fn matches_query(&self, needle: &str) -> bool {
144        if self.get_title().to_ascii_lowercase().contains(needle)
145            || self.get_route().to_ascii_lowercase().contains(needle)
146            || self.get_summary().to_ascii_lowercase().contains(needle)
147        {
148            return true;
149        }
150        self.get_blocks()
151            .iter()
152            .any(|block: &DocBlock| match block {
153                DocBlock::Heading { text, .. } => text.to_ascii_lowercase().contains(needle),
154                DocBlock::Paragraph(text)
155                | DocBlock::ListItem(text)
156                | DocBlock::BlockQuote(text) => text.to_ascii_lowercase().contains(needle),
157                DocBlock::Code { code, .. } => code.to_ascii_lowercase().contains(needle),
158                DocBlock::Image { alt, .. } => alt.to_ascii_lowercase().contains(needle),
159                DocBlock::Table { headers, rows } => {
160                    let in_headers: bool = headers
161                        .iter()
162                        .any(|h: &String| h.to_ascii_lowercase().contains(needle));
163                    let in_rows: bool = rows.iter().any(|row: &Vec<String>| {
164                        row.iter()
165                            .any(|cell: &String| cell.to_ascii_lowercase().contains(needle))
166                    });
167                    in_headers || in_rows
168                }
169                DocBlock::HorizontalRule => false,
170            })
171    }
172}