rust_docs_mcp/deps/
outputs.rs

1//! Output types for dependency tools
2//!
3//! These types are used as the return values from dependency tool methods.
4//! They are serialized to JSON strings for the MCP protocol, and can be
5//! deserialized in tests for type-safe validation.
6
7use serde::{Deserialize, Serialize};
8
9/// Identifies a crate with name and version
10#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
11pub struct CrateIdentifier {
12    pub name: String,
13    pub version: String,
14}
15
16/// Information about a single dependency
17#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
18pub struct Dependency {
19    /// Name of the dependency
20    pub name: String,
21
22    /// Version requirement specified in Cargo.toml
23    pub version_req: String,
24
25    /// Actual resolved version
26    pub resolved_version: Option<String>,
27
28    /// Kind of dependency (normal, dev, build)
29    pub kind: String,
30
31    /// Whether this is an optional dependency
32    pub optional: bool,
33
34    /// Features enabled for this dependency
35    pub features: Vec<String>,
36
37    /// Target platform (if dependency is platform-specific)
38    pub target: Option<String>,
39}
40
41/// Output from get_dependencies operation
42#[derive(Debug, Serialize, Deserialize, PartialEq)]
43pub struct GetDependenciesOutput {
44    /// The crate name and version being queried
45    pub crate_info: CrateIdentifier,
46
47    /// Direct dependencies of the crate
48    pub direct_dependencies: Vec<Dependency>,
49
50    /// Full dependency tree (only included if requested)
51    pub dependency_tree: Option<serde_json::Value>,
52
53    /// Total number of dependencies (direct + transitive)
54    pub total_dependencies: usize,
55}
56
57impl GetDependenciesOutput {
58    /// Convert to JSON string for MCP response
59    pub fn to_json(&self) -> String {
60        serde_json::to_string(self)
61            .unwrap_or_else(|_| r#"{"error":"Failed to serialize response"}"#.to_string())
62    }
63}
64
65/// Error output for dependency tools
66#[derive(Debug, Serialize, Deserialize, PartialEq)]
67pub struct DepsErrorOutput {
68    pub error: String,
69}
70
71impl DepsErrorOutput {
72    /// Create a new error output
73    pub fn new(message: impl Into<String>) -> Self {
74        Self {
75            error: message.into(),
76        }
77    }
78
79    /// Convert to JSON string for MCP response
80    pub fn to_json(&self) -> String {
81        serde_json::to_string(self)
82            .unwrap_or_else(|_| r#"{"error":"Failed to serialize error"}"#.to_string())
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_get_dependencies_output_serialization() {
92        let output = GetDependenciesOutput {
93            crate_info: CrateIdentifier {
94                name: "test-crate".to_string(),
95                version: "1.0.0".to_string(),
96            },
97            direct_dependencies: vec![Dependency {
98                name: "serde".to_string(),
99                version_req: "^1.0".to_string(),
100                resolved_version: Some("1.0.193".to_string()),
101                kind: "normal".to_string(),
102                optional: false,
103                features: vec!["derive".to_string()],
104                target: None,
105            }],
106            dependency_tree: None,
107            total_dependencies: 1,
108        };
109
110        let json = output.to_json();
111        let deserialized: GetDependenciesOutput = serde_json::from_str(&json).unwrap();
112        assert_eq!(output, deserialized);
113    }
114
115    #[test]
116    fn test_deps_error_output() {
117        let output = DepsErrorOutput::new("Dependencies not available");
118        let json = output.to_json();
119        let deserialized: DepsErrorOutput = serde_json::from_str(&json).unwrap();
120        assert_eq!(output, deserialized);
121    }
122}