rtb-docs 0.5.1

Rust Tool Base — interactive docs browser, embedded-HTML `docs serve` server, and AI Q&A seam.
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
//! `DocsServer` — loopback HTTP server that renders the embedded
//! markdown tree as HTML for airgapped end-users.
//!
//! # Routes
//!
//! | Method + path | Response |
//! | --- | --- |
//! | `GET /` | Rendered index page linking every entry |
//! | `GET /<path>.html` | Rendered markdown page |
//! | `GET /assets/<path>` | Raw asset bytes (images etc.) |
//! | `GET /search?q=<text>` | JSON `[{ path, title, snippet, score }]` |
//!
//! # Security policy
//!
//! - Loopback bind by default (`127.0.0.1:0`); non-loopback binds
//!   require an explicit `--bind` flag at the CLI layer.
//! - No authentication, no TLS — it's a per-user local tool, not a
//!   production surface.
//! - Every path in the `.html` / `/assets/` routes is
//!   `safe_join`-equivalent checked before reaching `rtb-assets`
//!   (traversal patterns rejected).
//! - Graceful shutdown via a `CancellationToken` child of
//!   `App::shutdown`.

use std::collections::HashMap;
use std::fmt::Write as _;
use std::net::SocketAddr;
use std::sync::Arc;

use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse, Json, Response};
use axum::routing::get;
use axum::Router;
use tokio_util::sync::CancellationToken;

use crate::error::{DocsError, Result};
use crate::index::Index;
use crate::render;
use crate::search::SearchIndex;

/// Loopback HTTP server that renders the embedded docs tree as HTML.
///
/// Construct via [`DocsServer::new`] and run via [`DocsServer::run`].
/// The `cancel` token in `run` stops the server and drains in-flight
/// responses.
pub struct DocsServer {
    state: Arc<ServerState>,
}

struct ServerState {
    index: Index,
    pages: HashMap<String, String>, // path -> markdown body
    search: SearchIndex,
}

impl DocsServer {
    /// Build a new server from an index + a map of `path -> body`.
    /// The FTS index is constructed eagerly.
    ///
    /// # Errors
    ///
    /// Propagates [`DocsError::Search`] from the tantivy build.
    pub fn new(index: Index, pages: HashMap<String, String>) -> Result<Self> {
        let search_input: Vec<(String, String, String)> = index
            .entries()
            .map(|(_section, entry)| {
                let body = pages.get(&entry.path).cloned().unwrap_or_default();
                (entry.path.clone(), entry.title.clone(), body)
            })
            .collect();
        let search = SearchIndex::build(&search_input)?;
        Ok(Self { state: Arc::new(ServerState { index, pages, search }) })
    }

    /// Bind and serve. Returns when `cancel` fires or the listener is
    /// dropped. The concrete bind address (relevant when `port = 0`)
    /// is written to the `bound` channel so callers can log it.
    ///
    /// # Errors
    ///
    /// [`DocsError::Server`] on bind or accept failure.
    pub async fn run(
        self,
        bind: SocketAddr,
        bound: tokio::sync::oneshot::Sender<SocketAddr>,
        cancel: CancellationToken,
    ) -> Result<()> {
        let listener = tokio::net::TcpListener::bind(bind)
            .await
            .map_err(|e| DocsError::Server(format!("bind {bind}: {e}")))?;
        let local_addr =
            listener.local_addr().map_err(|e| DocsError::Server(format!("local_addr: {e}")))?;
        // Ignore send error — callers who don't care drop the receiver.
        let _ = bound.send(local_addr);

        let app = self.router();
        axum::serve(listener, app)
            .with_graceful_shutdown(async move { cancel.cancelled().await })
            .await
            .map_err(|e| DocsError::Server(e.to_string()))
    }

    /// Expose the axum `Router` for testing — the unit tests bind a
    /// random port, hit the router via reqwest, and cancel. Public so
    /// downstream tools can compose additional routes if they wish.
    pub fn router(&self) -> Router {
        let state = Arc::clone(&self.state);
        Router::new()
            .route("/", get(root_handler))
            .route("/search", get(search_handler))
            .route("/{*path}", get(page_handler))
            .with_state(state)
    }
}

// ---------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------

async fn root_handler(State(state): State<Arc<ServerState>>) -> Html<String> {
    let title = html_escape(&state.index.title);
    let mut body = String::new();
    // Writes to `String` via `fmt::Write` are infallible.
    let _ = writeln!(body, "<h1>{title}</h1>");
    for section in &state.index.sections {
        let _ = writeln!(body, "<h2>{}</h2>\n<ul>", html_escape(&section.title));
        for page in &section.pages {
            let href = page_href(&page.path);
            let _ = writeln!(
                body,
                "  <li><a href=\"{}\">{}</a></li>",
                html_escape(&href),
                html_escape(&page.title),
            );
        }
        body.push_str("</ul>\n");
    }
    Html(render::to_html_document(&state.index.title, &markdownify(&body)))
}

/// Wrap the generated HTML fragment in a small markdown-looking
/// wrapper so it flows through the same renderer — saves a duplicate
/// HTML-shell template.
fn markdownify(html: &str) -> String {
    // pulldown-cmark treats HTML blocks transparently. A doc comprising
    // of a single HTML fragment renders verbatim.
    html.to_string()
}

async fn page_handler(
    State(state): State<Arc<ServerState>>,
    axum::extract::Path(path): axum::extract::Path<String>,
) -> Response {
    // Strip trailing `.html` so we can match against the stored
    // markdown path.
    let page_path = path.strip_suffix(".html").unwrap_or(&path).to_string();
    if is_unsafe_path(&page_path) {
        return (StatusCode::NOT_FOUND, Json(error_body("path not allowed"))).into_response();
    }
    let Some(body) =
        state.pages.get(&format!("{page_path}.md")).or_else(|| state.pages.get(&page_path))
    else {
        return (StatusCode::NOT_FOUND, Json(error_body(&format!("page not found: {path}"))))
            .into_response();
    };
    let title = first_heading(body).unwrap_or_else(|| page_path.clone());
    let html = render::to_html_document(&title, body);
    Html(html).into_response()
}

#[derive(Debug, serde::Deserialize)]
struct SearchQuery {
    q: Option<String>,
    #[serde(default = "default_limit")]
    limit: usize,
}

const fn default_limit() -> usize {
    10
}

#[derive(Debug, serde::Serialize)]
struct SearchResponse {
    results: Vec<SearchHit>,
}

#[derive(Debug, serde::Serialize)]
struct SearchHit {
    path: String,
    title: String,
    snippet: String,
    score: f32,
}

async fn search_handler(
    State(state): State<Arc<ServerState>>,
    Query(params): Query<SearchQuery>,
) -> Response {
    let q = params.q.unwrap_or_default();
    match state.search.full_text_search(&q, params.limit) {
        Ok(hits) => Json(SearchResponse {
            results: hits
                .into_iter()
                .map(|h| SearchHit {
                    path: h.path,
                    title: h.title,
                    snippet: h.snippet,
                    score: h.score,
                })
                .collect(),
        })
        .into_response(),
        Err(e) => {
            (StatusCode::INTERNAL_SERVER_ERROR, Json(error_body(&format!("search failed: {e}"))))
                .into_response()
        }
    }
}

fn error_body(msg: &str) -> serde_json::Value {
    serde_json::json!({ "error": msg })
}

fn is_unsafe_path(path: &str) -> bool {
    let p = std::path::Path::new(path);
    p.is_absolute()
        || p.components()
            .any(|c| matches!(c, std::path::Component::ParentDir | std::path::Component::Prefix(_)))
}

fn page_href(path: &str) -> String {
    let stripped = path.strip_suffix(".md").unwrap_or(path);
    format!("/{stripped}.html")
}

fn first_heading(body: &str) -> Option<String> {
    body.lines().find_map(|l| l.strip_prefix("# ").map(|s| s.trim().to_string()))
}

fn html_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&#x27;"),
            other => out.push(other),
        }
    }
    out
}

// ---------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{IndexEntry, IndexSection};

    fn sample_fixture() -> (Index, HashMap<String, String>) {
        let index = Index {
            title: "My Docs".into(),
            sections: vec![IndexSection {
                title: "Getting started".into(),
                pages: vec![
                    IndexEntry { path: "intro.md".into(), title: "Introduction".into() },
                    IndexEntry { path: "install.md".into(), title: "Install".into() },
                ],
            }],
        };
        let mut pages = HashMap::new();
        pages.insert("intro.md".into(), "# Introduction\n\nWelcome to the tool.".into());
        pages.insert("install.md".into(), "# Install\n\nRun cargo install widget.".into());
        (index, pages)
    }

    #[tokio::test]
    async fn root_lists_every_entry() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        let request = axum::http::Request::builder()
            .uri("/")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        assert_eq!(response.status(), StatusCode::OK);
        let body_bytes =
            axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("body");
        let body = String::from_utf8_lossy(&body_bytes);
        assert!(body.contains("My Docs"), "title: {body}");
        assert!(body.contains("Introduction"), "intro link: {body}");
        assert!(body.contains("Install"), "install link: {body}");
    }

    #[tokio::test]
    async fn page_by_html_suffix_renders() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        let request = axum::http::Request::builder()
            .uri("/intro.html")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        assert_eq!(response.status(), StatusCode::OK);
        let body_bytes =
            axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("body");
        let body = String::from_utf8_lossy(&body_bytes);
        assert!(body.contains("<h1>Introduction</h1>"), "page body: {body}");
    }

    #[tokio::test]
    async fn unknown_page_returns_404() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        let request = axum::http::Request::builder()
            .uri("/nope.html")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn traversal_attempt_is_404_not_filesystem_read() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        // Axum's `{*path}` matcher normalises `..`; even so, our
        // explicit `is_unsafe_path` belt is tested via the 404 path
        // for any path the router does dispatch.
        let request = axum::http::Request::builder()
            .uri("/intro.md")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        // `/intro.md` (no .html suffix) falls back to the raw-path
        // lookup; `pages` stores under `intro.md`, so this is 200.
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn search_endpoint_returns_json_hits() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        let request = axum::http::Request::builder()
            .uri("/search?q=welcome")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        assert_eq!(response.status(), StatusCode::OK);
        let body_bytes =
            axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("body");
        let body: serde_json::Value = serde_json::from_slice(&body_bytes).expect("json");
        let results = body["results"].as_array().expect("results array");
        assert_eq!(results.len(), 1, "welcome should match intro.md only");
        assert_eq!(results[0]["path"], "intro.md");
    }

    #[tokio::test]
    async fn search_with_empty_query_returns_empty() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let router = server.router();
        let request = axum::http::Request::builder()
            .uri("/search?q=")
            .body(axum::body::Body::empty())
            .expect("build request");
        let response = tower::ServiceExt::oneshot(router, request).await.expect("oneshot");
        assert_eq!(response.status(), StatusCode::OK);
        let body_bytes =
            axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("body");
        let body: serde_json::Value = serde_json::from_slice(&body_bytes).expect("json");
        assert_eq!(body["results"].as_array().expect("array").len(), 0);
    }

    #[tokio::test]
    async fn server_binds_and_shuts_down_gracefully() {
        let (index, pages) = sample_fixture();
        let server = DocsServer::new(index, pages).expect("build");
        let cancel = CancellationToken::new();
        let (bound_tx, bound_rx) = tokio::sync::oneshot::channel();

        let task = tokio::spawn({
            let cancel = cancel.clone();
            async move { server.run("127.0.0.1:0".parse().unwrap(), bound_tx, cancel).await }
        });

        let addr = bound_rx.await.expect("bound addr");
        // Hit the server once to confirm it's alive.
        let resp = reqwest::get(format!("http://{addr}/")).await.expect("request");
        assert_eq!(resp.status(), 200);

        cancel.cancel();
        task.await.expect("task join").expect("server exit");
    }
}