hyprstream_rpc/service/doc.rs
1//! Service documentation helper for VFS mount integration.
2//!
3//! `DocFs` wraps a generated `render_doc()` function and provides
4//! VFS-compatible helpers for serving documentation at `/doc/` paths.
5
6use crate::metadata::MethodMeta;
7
8/// Helper for serving service documentation through VFS paths.
9///
10/// Services delegate `["doc", ...]` walk/read/readdir operations to this helper.
11/// Documentation is generated at compile time by the proc macro from Cap'n Proto
12/// schema annotations (`$mcpDescription`, `$paramDescription`, `$docExample`).
13pub struct DocFs {
14 /// The generated `render_doc` function from the proc macro.
15 render: fn(&[&str]) -> Option<String>,
16 /// The generated `schema_metadata` function for directory listing.
17 metadata: fn() -> (&'static str, &'static [MethodMeta]),
18}
19
20impl DocFs {
21 /// Create a new DocFs from generated functions.
22 pub fn new(
23 render: fn(&[&str]) -> Option<String>,
24 metadata: fn() -> (&'static str, &'static [MethodMeta]),
25 ) -> Self {
26 Self { render, metadata }
27 }
28
29 /// Check if a doc path is valid.
30 pub fn exists(&self, path: &[&str]) -> bool {
31 (self.render)(path).is_some()
32 }
33
34 /// Read documentation content for a path.
35 pub fn read(&self, path: &[&str]) -> Option<Vec<u8>> {
36 (self.render)(path).map(String::into_bytes)
37 }
38
39 /// List available documentation entries at a path.
40 ///
41 /// At the root, lists all non-hidden methods from `schema_metadata()`.
42 pub fn list_entries(&self, path: &[&str]) -> Vec<String> {
43 match path {
44 [] | [""] => {
45 let (_, methods) = (self.metadata)();
46 methods
47 .iter()
48 .filter(|m| !m.hidden)
49 .map(|m| m.name.to_owned())
50 .collect()
51 }
52 _ => {
53 // For scoped paths, we could enumerate but for now
54 // just return empty — the render function handles validation.
55 Vec::new()
56 }
57 }
58 }
59}