Skip to main content

omni_dev/
resources.rs

1//! Embedded reference resources shared by the CLI and the MCP server.
2//!
3//! Resources are exposed via two surfaces that must stay in lock step:
4//! - CLI: `omni-dev resources show <id>` / `omni-dev resources list`
5//! - MCP: `omni-dev://<id>` (e.g. `omni-dev://specs/jfm`)
6//!
7//! Content is embedded into the binary at compile time so installed builds
8//! (`cargo install omni-dev`) serve it without reading from disk.
9
10/// JFM (JIRA-Flavoured Markdown) specification, embedded from
11/// `docs/specs/jfm.md`.
12pub const SPEC_JFM: &str = include_str!("../docs/specs/jfm.md");
13
14/// One entry in the embedded-resource registry.
15pub struct Resource {
16    /// Canonical, path-style id (e.g. `"specs/jfm"`).
17    pub id: &'static str,
18    /// Raw embedded content.
19    pub content: &'static str,
20    /// MIME type advertised to MCP clients.
21    pub mime_type: &'static str,
22}
23
24/// The complete static registry. Kept lexicographically sorted by `id` so
25/// `list` output stays deterministic as entries are added.
26pub const REGISTRY: &[Resource] = &[Resource {
27    id: "specs/jfm",
28    content: SPEC_JFM,
29    mime_type: "text/markdown",
30}];
31
32/// Returns the resource with the given canonical id, or `None`.
33///
34/// Lookup is exact-string and case-sensitive. The `omni-dev://` URI scheme
35/// must be stripped by the caller before lookup.
36pub fn get(id: &str) -> Option<&'static Resource> {
37    REGISTRY.iter().find(|r| r.id == id)
38}
39
40/// All ids in registry order. Used for `list` output and error messages.
41pub fn ids() -> impl Iterator<Item = &'static str> {
42    REGISTRY.iter().map(|r| r.id)
43}
44
45/// Comma-separated list of known ids, for error messages.
46pub fn known_ids_csv() -> String {
47    ids().collect::<Vec<_>>().join(", ")
48}
49
50#[cfg(test)]
51#[allow(clippy::unwrap_used, clippy::expect_used)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn spec_jfm_is_embedded_and_non_empty() {
57        assert!(!SPEC_JFM.is_empty());
58        // Guard against the file moving or being emptied: the heading is
59        // load-bearing for clients that match on it.
60        assert!(
61            SPEC_JFM.contains("# JFM (JIRA-Flavored Markdown) Specification"),
62            "JFM spec missing expected heading"
63        );
64    }
65
66    #[test]
67    fn get_specs_jfm_returns_resource() {
68        let r = get("specs/jfm").expect("specs/jfm must be registered");
69        assert_eq!(r.id, "specs/jfm");
70        assert_eq!(r.mime_type, "text/markdown");
71        assert_eq!(r.content, SPEC_JFM);
72    }
73
74    #[test]
75    fn get_unknown_returns_none() {
76        assert!(get("specs/bogus").is_none());
77        assert!(get("").is_none());
78        // The old short-form id ("jfm") was replaced by the path-style id;
79        // make sure it no longer resolves.
80        assert!(get("jfm").is_none());
81    }
82
83    #[test]
84    fn get_is_case_sensitive() {
85        assert!(get("Specs/Jfm").is_none());
86        assert!(get("SPECS/JFM").is_none());
87    }
88
89    #[test]
90    fn ids_yields_registered_entries() {
91        assert!(ids().any(|id| id == "specs/jfm"));
92    }
93
94    #[test]
95    fn known_ids_csv_contains_specs_jfm() {
96        assert!(known_ids_csv().contains("specs/jfm"));
97    }
98}