rust_docs_mcp/deps/
outputs.rs1use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
11pub struct CrateIdentifier {
12 pub name: String,
13 pub version: String,
14}
15
16#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
18pub struct Dependency {
19 pub name: String,
21
22 pub version_req: String,
24
25 pub resolved_version: Option<String>,
27
28 pub kind: String,
30
31 pub optional: bool,
33
34 pub features: Vec<String>,
36
37 pub target: Option<String>,
39}
40
41#[derive(Debug, Serialize, Deserialize, PartialEq)]
43pub struct GetDependenciesOutput {
44 pub crate_info: CrateIdentifier,
46
47 pub direct_dependencies: Vec<Dependency>,
49
50 pub dependency_tree: Option<serde_json::Value>,
52
53 pub total_dependencies: usize,
55}
56
57impl GetDependenciesOutput {
58 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#[derive(Debug, Serialize, Deserialize, PartialEq)]
67pub struct DepsErrorOutput {
68 pub error: String,
69}
70
71impl DepsErrorOutput {
72 pub fn new(message: impl Into<String>) -> Self {
74 Self {
75 error: message.into(),
76 }
77 }
78
79 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}