ferritin 0.1.0

Human-friendly CLI for browsing Rust documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use super::*;
use crate::styled_string::{DocumentNode, LinkTarget, TruncationLevel};
use crate::{generate_docsrs_url::generate_docsrs_url, markdown::MarkdownRenderer};

/// Information about documentation text with truncation details
#[derive(Debug, Clone, Default)]
pub(crate) struct DocInfo {
    /// The truncated documentation text (may be complete if not truncated)
    pub(crate) text: String,
    /// Total number of lines in the original documentation
    pub(crate) total_lines: usize,
    /// Number of lines included in the truncated text
    pub(crate) displayed_lines: usize,
    /// Whether the documentation was truncated
    pub(crate) is_truncated: bool,
}

impl DocInfo {
    /// Get the number of lines that were elided (hidden)
    pub(crate) fn elided_lines(&self) -> usize {
        self.total_lines.saturating_sub(self.displayed_lines)
    }

    /// Format the elided line count for display (e.g., "[+5 lines]")
    pub(crate) fn elided_indicator(&self) -> Option<String> {
        if self.is_truncated {
            Some(format!("[+{} lines elided]", self.elided_lines()))
        } else {
            None
        }
    }
}

impl Request {
    /// Render markdown documentation to structured DocumentNodes
    pub(crate) fn render_docs<'a>(
        &'a self,
        item: DocRef<'a, Item>,
        markdown: &str,
    ) -> Vec<DocumentNode<'a>> {
        // Create a link resolver that extracts link targets without loading external crates
        let link_resolver =
            |url: &str| -> Option<(String, LinkTarget<'a>)> { self.extract_link_target(item, url) };

        MarkdownRenderer::render_with_resolver(markdown, link_resolver)
    }

    /// Extract the link target from an intra-doc link without loading external crates
    ///
    /// Returns either a resolved DocRef (for same-crate items) or an unresolved path string
    /// (for external items), avoiding the need to load external crates just for rendering.
    fn extract_link_target<'a>(
        &'a self,
        origin: DocRef<'a, Item>,
        url: &str,
    ) -> Option<(String, LinkTarget<'a>)> {
        // Handle fragment-only links
        if url.starts_with('#') {
            return None; // Keep as-is
        }

        // Handle external URLs
        if url.starts_with("http://") || url.starts_with("https://") {
            return None; // Keep as-is
        }

        // Split off any fragment/anchor
        let (path, _fragment) = url.split_once('#').unwrap_or((url, ""));

        // Check if this is a relative HTML URL (e.g., "task/index.html", "attr.main.html")
        // These are hand-written markdown links in the source that point to HTML docs
        if path.ends_with(".html") || path.contains("/") {
            log::debug!("extract_link_target: parsing relative URL '{}'", path);

            // Try to extract the item path from the HTML filename for navigation
            if let Some(item_path) = self.parse_html_path_to_item_path(origin, path) {
                // Convert to absolute docs.rs URL
                let absolute_url = self.make_relative_url_absolute(origin, path);
                log::debug!(
                    "  → Extracted item path: '{}', URL: '{}'",
                    item_path,
                    absolute_url
                );
                return Some((
                    absolute_url,
                    LinkTarget::Path(std::borrow::Cow::Owned(item_path)),
                ));
            } else {
                // Can't parse to an item path - return None to keep as external URL
                log::debug!("  → Could not extract item path, keeping as external URL");
                return None;
            }
        }

        log::debug!("extract_link_target: processing link '{}'", path);

        // Try to get the path from rustdoc's pre-resolved links map
        // Rustdoc sometimes stores links with backticks, sometimes without
        // Try both formats
        let link_id = origin
            .links
            .get(path)
            .or_else(|| origin.links.get(&format!("`{}`", path)));

        if let Some(link_id) = link_id {
            log::debug!("  ✓ Found in origin.links with ID {:?}", link_id);
            // Check if it's in the same crate (fast path - no external loading)
            if let Some(item) = origin.get(link_id) {
                // Generate accurate URL and return resolved DocRef
                let url = generate_docsrs_url(item);
                log::debug!(
                    "  → Same-crate item: path='{}', kind={:?}, URL='{}'",
                    self.get_item_full_path(item),
                    item.kind(),
                    url
                );
                return Some((url, LinkTarget::Resolved(item)));
            }

            log::debug!("  → Not in same crate index, checking external paths");
            // It's in an external crate - extract path from item_summary without loading
            if let Some(item_summary) = origin.crate_docs().paths.get(link_id) {
                log::debug!(
                    "  ✓ Found in paths map: {:?}, kind: {:?}",
                    item_summary.path,
                    item_summary.kind
                );
                let full_path = item_summary.path.join("::");

                // Try to get the html_root_url from the external crate
                let url = if let Some(external_crate) = origin
                    .crate_docs()
                    .external_crates
                    .get(&item_summary.crate_id)
                {
                    if let Some(html_root) = &external_crate.html_root_url {
                        // Use the exact documentation URL from the external crate
                        self.generate_url_from_html_root(
                            html_root,
                            &item_summary.path,
                            item_summary.kind,
                        )
                    } else {
                        // Fallback to heuristic generation
                        self.generate_url_from_path_and_kind(&full_path, item_summary.kind)
                    }
                } else {
                    // No external crate info, use heuristic
                    self.generate_url_from_path_and_kind(&full_path, item_summary.kind)
                };

                return Some((url, LinkTarget::Path(std::borrow::Cow::Owned(full_path))));
            }
        }

        // Fallback: try to resolve path relative to current crate
        // Handle "crate::", "self::", and absolute paths
        log::debug!("  ✗ Not found in links map, using fallback for '{}'", path);
        let qualified_path = if let Some(rest) = path.strip_prefix("crate::") {
            format!("{}::{}", origin.crate_docs().name(), rest)
        } else if let Some(rest) = path.strip_prefix("self::") {
            format!("{}::{}", origin.crate_docs().name(), rest)
        } else if path.contains("::") {
            path.to_string()
        } else {
            format!("{}::{}", origin.crate_docs().name(), path)
        };

        // Generate a search URL since we don't know the actual item kind
        log::debug!(
            "  → Qualified path: '{}', generating search URL",
            qualified_path
        );
        let url = self.generate_search_url(&qualified_path);
        Some((
            url,
            LinkTarget::Path(std::borrow::Cow::Owned(qualified_path)),
        ))
    }

    /// Parse a relative HTML path to an item path for navigation
    ///
    /// Examples:
    /// - `./macro.trace.html` → `log::trace`
    /// - `macro.trace.html` → `log::trace`
    /// - `task/index.html` → `tokio::task`
    /// - `task/spawn/index.html` → `tokio::task::spawn`
    /// - `struct.TcpStream.html` → `tokio::TcpStream`
    fn parse_html_path_to_item_path(
        &self,
        origin: DocRef<'_, Item>,
        html_path: &str,
    ) -> Option<String> {
        let crate_name = origin.crate_docs().name();

        // Strip leading ./
        let path = html_path.strip_prefix("./").unwrap_or(html_path);

        // Must end with .html
        if !path.ends_with(".html") {
            return None;
        }

        // Handle module paths: "task/index.html" or "task/spawn/index.html"
        if path.ends_with("/index.html") {
            let module_path = path.strip_suffix("/index.html")?;
            let module_parts: Vec<&str> = module_path.split('/').collect();
            return Some(format!("{}::{}", crate_name, module_parts.join("::")));
        }

        // Handle item-specific HTML files: "struct.Name.html", "macro.trace.html", etc.
        // Format: "{kind}.{name}.html"
        let without_html = path.strip_suffix(".html")?;

        // Check for kind.name pattern
        if let Some((_kind, name)) = without_html.split_once('.') {
            // The kind prefix (struct, enum, macro, etc.) tells us the type,
            // but we just need the name for the path
            return Some(format!("{}::{}", crate_name, name));
        }

        // Fallback: if no dot separator, treat whole thing as name
        // (e.g., "something.html" -> "crate::something")
        Some(format!("{}::{}", crate_name, without_html))
    }

    /// Convert a relative HTML URL to an absolute docs.rs URL
    ///
    /// Hand-written markdown in documentation often contains relative HTML links
    /// like `task/index.html` or `../other_crate/index.html`. We convert these
    /// to absolute URLs based on the current crate's documentation location.
    fn make_relative_url_absolute(&self, origin: DocRef<'_, Item>, relative_url: &str) -> String {
        let crate_docs = origin.crate_docs();
        let crate_name = crate_docs.name();
        let version = crate_docs
            .version()
            .map(|v| v.to_string())
            .unwrap_or_else(|| "latest".to_string());

        let is_std = crate_docs.provenance().is_std();

        let base = if is_std {
            format!("https://doc.rust-lang.org/nightly/{}", crate_name)
        } else {
            format!("https://docs.rs/{}/{}/{}", crate_name, version, crate_name)
        };

        // Join the relative URL with the base
        // Remove leading "./" if present
        let relative = relative_url.strip_prefix("./").unwrap_or(relative_url);

        // If it starts with "../", we can't easily resolve it, just use crate root
        if relative.starts_with("../") {
            return format!("{}/{}", base, relative.trim_start_matches("../"));
        }

        format!("{}/{}", base, relative)
    }

    /// Get the full path of an item (e.g., "std::vec::Vec")
    fn get_item_full_path(&self, item: DocRef<'_, Item>) -> String {
        if let Some(path) = item.path() {
            path.to_string()
        } else if let Some(name) = item.name() {
            format!("{}::{}", item.crate_docs().name(), name)
        } else {
            item.crate_docs().name().to_string()
        }
    }

    /// Generate a docs.rs URL from a path and ItemKind
    fn generate_url_from_path_and_kind(&self, path: &str, kind: rustdoc_types::ItemKind) -> String {
        let parts: Vec<&str> = path.split("::").collect();
        if parts.is_empty() {
            return String::new();
        }

        let crate_name = parts[0];
        let is_std = matches!(crate_name, "std" | "core" | "alloc" | "proc_macro");

        let base = if is_std {
            "https://doc.rust-lang.org/nightly".to_string()
        } else {
            format!("https://docs.rs/{}/latest", crate_name)
        };

        if parts.len() == 1 {
            // Just the crate name
            return format!("{}/{}/index.html", base, crate_name);
        }

        // Assume the last part is the item name, everything before is the module path
        let module_parts = &parts[1..parts.len() - 1];
        let item_name = parts[parts.len() - 1];

        let module_path = if module_parts.is_empty() {
            crate_name.to_string()
        } else {
            format!("{}/{}", crate_name, module_parts.join("/"))
        };

        // Generate URL based on the actual item kind
        use rustdoc_types::ItemKind;
        match kind {
            ItemKind::Module => {
                // Modules use the full path with index.html
                let full_module_path = format!("{}/{}", module_path, item_name);
                format!("{}/{}/index.html", base, full_module_path)
            }
            ItemKind::Struct => format!("{}/{}/struct.{}.html", base, module_path, item_name),
            ItemKind::Enum => format!("{}/{}/enum.{}.html", base, module_path, item_name),
            ItemKind::Trait => format!("{}/{}/trait.{}.html", base, module_path, item_name),
            ItemKind::Function => format!("{}/{}/fn.{}.html", base, module_path, item_name),
            ItemKind::TypeAlias => format!("{}/{}/type.{}.html", base, module_path, item_name),
            ItemKind::Constant => format!("{}/{}/constant.{}.html", base, module_path, item_name),
            ItemKind::Static => format!("{}/{}/static.{}.html", base, module_path, item_name),
            ItemKind::Union => format!("{}/{}/union.{}.html", base, module_path, item_name),
            ItemKind::Macro | ItemKind::ProcAttribute | ItemKind::ProcDerive => {
                format!("{}/{}/macro.{}.html", base, module_path, item_name)
            }
            ItemKind::Primitive => format!("{}/{}/primitive.{}.html", base, crate_name, item_name),
            _ => {
                // Fallback for unknown kinds - default to struct guess
                format!("{}/{}/struct.{}.html", base, module_path, item_name)
            }
        }
    }

    /// Generate a heuristic docs.rs URL from a path like "std::vec::Vec" when we don't know the kind
    fn generate_heuristic_url(&self, path: &str) -> String {
        // Default to struct as a reasonable guess
        self.generate_url_from_path_and_kind(path, rustdoc_types::ItemKind::Struct)
    }

    /// Generate a search URL for a path when we can't determine the item kind
    ///
    /// Example: "tokio::something::UnknownType" becomes
    /// "https://docs.rs/tokio/latest/tokio/index.html?search=tokio::something::UnknownType"
    fn generate_search_url(&self, path: &str) -> String {
        let parts: Vec<&str> = path.split("::").collect();
        if parts.is_empty() {
            return String::new();
        }

        let crate_name = parts[0];
        let is_std = matches!(crate_name, "std" | "core" | "alloc" | "proc_macro");

        let base = if is_std {
            "https://doc.rust-lang.org/nightly".to_string()
        } else {
            format!("https://docs.rs/{}/latest", crate_name)
        };

        // Link to the deepest module we can infer, with a search query for the full path
        let module_path = if parts.len() > 2 {
            // Use parent module path
            parts[1..parts.len() - 1].join("/")
        } else if parts.len() == 2 {
            // Just one level deep - link to crate root
            String::new()
        } else {
            String::new()
        };

        let index_path = if module_path.is_empty() {
            format!("{}/{}/index.html", base, crate_name)
        } else {
            format!("{}/{}/{}/index.html", base, crate_name, module_path)
        };

        // Add search query for the full path
        use percent_encoding::{NON_ALPHANUMERIC, utf8_percent_encode};
        format!(
            "{}?search={}",
            index_path,
            utf8_percent_encode(path, NON_ALPHANUMERIC)
        )
    }

    /// Generate a documentation URL from html_root_url and item path
    ///
    /// The html_root_url points to the crate root (e.g., "https://docs.rs/tokio/1.49.0")
    /// We need to append the correct path based on item kind and location
    fn generate_url_from_html_root(
        &self,
        html_root: &str,
        path: &[String],
        kind: rustdoc_types::ItemKind,
    ) -> String {
        // Strip trailing slash from html_root to avoid double slashes
        let html_root = html_root.trim_end_matches('/');

        if path.is_empty() {
            return html_root.to_string();
        }

        // path[0] is the crate name, skip it since html_root already includes it
        let crate_name = &path[0];
        let remaining_parts = &path[1..];

        if remaining_parts.is_empty() {
            // Just the crate root
            return format!("{}/{}/index.html", html_root, crate_name);
        }

        let item_name = &remaining_parts[remaining_parts.len() - 1];
        let module_parts = &remaining_parts[..remaining_parts.len() - 1];

        let module_path = if module_parts.is_empty() {
            crate_name.to_string()
        } else {
            format!("{}/{}", crate_name, module_parts.join("/"))
        };

        // Generate URL based on the actual item kind
        use rustdoc_types::ItemKind;
        match kind {
            ItemKind::Module => {
                let full_module_path = format!("{}/{}", module_path, item_name);
                format!("{}/{}/index.html", html_root, full_module_path)
            }
            ItemKind::Struct => format!("{}/{}/struct.{}.html", html_root, module_path, item_name),
            ItemKind::Enum => format!("{}/{}/enum.{}.html", html_root, module_path, item_name),
            ItemKind::Trait => format!("{}/{}/trait.{}.html", html_root, module_path, item_name),
            ItemKind::Function => format!("{}/{}/fn.{}.html", html_root, module_path, item_name),
            ItemKind::TypeAlias => format!("{}/{}/type.{}.html", html_root, module_path, item_name),
            ItemKind::Constant => {
                format!("{}/{}/constant.{}.html", html_root, module_path, item_name)
            }
            ItemKind::Static => format!("{}/{}/static.{}.html", html_root, module_path, item_name),
            ItemKind::Union => format!("{}/{}/union.{}.html", html_root, module_path, item_name),
            ItemKind::Macro | ItemKind::ProcAttribute | ItemKind::ProcDerive => {
                format!("{}/{}/macro.{}.html", html_root, module_path, item_name)
            }
            ItemKind::Primitive => {
                format!("{}/{}/primitive.{}.html", html_root, crate_name, item_name)
            }
            _ => {
                // Fallback for unknown kinds
                format!("{}/{}/struct.{}.html", html_root, module_path, item_name)
            }
        }
    }

    /// Get documentation to show for an item
    ///
    /// Returns None if no docs should be shown, Some(docs) if docs should be displayed.
    /// Docs are wrapped in a TruncatedBlock with appropriate level hint.
    pub(crate) fn docs_to_show<'a>(
        &'a self,
        item: DocRef<'a, Item>,
        truncation_level: TruncationLevel,
    ) -> Option<Vec<DocumentNode<'a>>> {
        // Extract docs from item
        let docs = item.docs.as_deref()?;
        if docs.is_empty() {
            return None;
        }

        let nodes = self.render_docs(item, docs);
        Some(vec![DocumentNode::truncated_block(nodes, truncation_level)])
    }

    /// Count the number of lines in a text string
    pub(crate) fn count_lines(&self, text: &str) -> usize {
        if text.is_empty() {
            0
        } else {
            text.lines().count()
        }
    }

    /// Truncate text to first paragraph or max_lines, whichever comes first
    pub(crate) fn truncate_to_paragraph_or_lines(&self, text: &str, max_lines: usize) -> String {
        // Look for the second occurrence of "\n\n" (second paragraph break)
        if let Some(first_break) = text.find("\n\n") {
            let after_first_break = &text[first_break + 2..];
            if let Some(second_break_offset) = after_first_break.find("\n\n") {
                // Found second paragraph break - truncate there
                let second_break_pos = first_break + 2 + second_break_offset;
                let first_section = &text[..second_break_pos];
                let first_section_lines = self.count_lines(first_section);

                // If first section is within line limit, use it
                if first_section_lines <= max_lines {
                    return first_section.to_string();
                }
            }
        }

        // Fall back to line-based truncation (no second paragraph break found, or too long)
        let lines: Vec<&str> = text.lines().collect();
        let cutoff = max_lines.min(lines.len());
        lines[..cutoff].join("\n")
    }
}