|
| 1 | +use base64::Engine; |
1 | 2 | pub use rmcp::model::ErrorData; |
| 3 | +use rmcp::model::ResourceContents; |
2 | 4 |
|
3 | | -/// Type alias for tool results |
4 | 5 | pub type ToolResult<T> = Result<T, ErrorData>; |
| 6 | + |
| 7 | +pub fn extract_text_from_resource(resource: &ResourceContents) -> String { |
| 8 | + match resource { |
| 9 | + ResourceContents::TextResourceContents { text, .. } => text.clone(), |
| 10 | + ResourceContents::BlobResourceContents { |
| 11 | + blob, mime_type, .. |
| 12 | + } => match base64::engine::general_purpose::STANDARD.decode(blob) { |
| 13 | + Ok(bytes) => { |
| 14 | + let byte_len = bytes.len(); |
| 15 | + match String::from_utf8(bytes) { |
| 16 | + Ok(text) => text, |
| 17 | + Err(_) => { |
| 18 | + let mime = mime_type |
| 19 | + .as_ref() |
| 20 | + .map(|m| m.as_str()) |
| 21 | + .unwrap_or("application/octet-stream"); |
| 22 | + format!("[Binary content ({}) - {} bytes]", mime, byte_len) |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + Err(_) => blob.clone(), |
| 27 | + }, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[cfg(test)] |
| 32 | +mod tests { |
| 33 | + use super::*; |
| 34 | + use test_case::test_case; |
| 35 | + |
| 36 | + #[test_case("Hello, World!", "Hello, World!" ; "simple text")] |
| 37 | + #[test_case("Hello from GitHub!", "Hello from GitHub!" ; "github content")] |
| 38 | + #[test_case("", "" ; "empty text")] |
| 39 | + fn test_extract_text_from_text_resource(input: &str, expected: &str) { |
| 40 | + let resource = ResourceContents::TextResourceContents { |
| 41 | + uri: "file:///test.txt".to_string(), |
| 42 | + mime_type: Some("text/plain".to_string()), |
| 43 | + text: input.to_string(), |
| 44 | + meta: None, |
| 45 | + }; |
| 46 | + assert_eq!(extract_text_from_resource(&resource), expected); |
| 47 | + } |
| 48 | + |
| 49 | + #[test_case("Hello from GitHub!", "Hello from GitHub!" ; "utf8 markdown")] |
| 50 | + #[test_case("Simple text", "Simple text" ; "utf8 plain")] |
| 51 | + fn test_extract_text_from_blob_utf8(input: &str, expected: &str) { |
| 52 | + let blob = base64::engine::general_purpose::STANDARD.encode(input.as_bytes()); |
| 53 | + let resource = ResourceContents::BlobResourceContents { |
| 54 | + uri: "github://repo/file.md".to_string(), |
| 55 | + mime_type: Some("text/markdown".to_string()), |
| 56 | + blob, |
| 57 | + meta: None, |
| 58 | + }; |
| 59 | + assert_eq!(extract_text_from_resource(&resource), expected); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_extract_text_from_blob_binary() { |
| 64 | + let binary_data: Vec<u8> = vec![0xFF, 0xFE, 0x00, 0x01, 0x89, 0x50, 0x4E, 0x47]; |
| 65 | + let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data); |
| 66 | + |
| 67 | + let resource = ResourceContents::BlobResourceContents { |
| 68 | + uri: "file:///image.png".to_string(), |
| 69 | + mime_type: Some("image/png".to_string()), |
| 70 | + blob, |
| 71 | + meta: None, |
| 72 | + }; |
| 73 | + |
| 74 | + assert_eq!( |
| 75 | + extract_text_from_resource(&resource), |
| 76 | + "[Binary content (image/png) - 8 bytes]" |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_extract_text_from_blob_binary_no_mime_type() { |
| 82 | + let binary_data: Vec<u8> = vec![0xFF, 0xFE]; |
| 83 | + let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data); |
| 84 | + |
| 85 | + let resource = ResourceContents::BlobResourceContents { |
| 86 | + uri: "file:///unknown".to_string(), |
| 87 | + mime_type: None, |
| 88 | + blob, |
| 89 | + meta: None, |
| 90 | + }; |
| 91 | + |
| 92 | + assert_eq!( |
| 93 | + extract_text_from_resource(&resource), |
| 94 | + "[Binary content (application/octet-stream) - 2 bytes]" |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_extract_text_from_blob_invalid_base64() { |
| 100 | + let resource = ResourceContents::BlobResourceContents { |
| 101 | + uri: "file:///test.txt".to_string(), |
| 102 | + mime_type: Some("text/plain".to_string()), |
| 103 | + blob: "not valid base64!!!".to_string(), |
| 104 | + meta: None, |
| 105 | + }; |
| 106 | + assert_eq!(extract_text_from_resource(&resource), "not valid base64!!!"); |
| 107 | + } |
| 108 | +} |
0 commit comments