Skip to main content

Grove/API/
mod.rs

1//! API Module
2//!
3//! Provides the VS Code API facade and types for Grove.
4//! Implements compatible API surface with Cocoon for extension compatibility.
5
6#[path = "Types.rs"]
7pub mod Types;
8
9#[path = "VSCode.rs"]
10pub mod VSCode;
11
12/// VS Code API version compatibility
13pub const VS_CODE_API_VERSION:&str = "1.85.0";
14
15/// Minimum supported VS Code API version
16pub const MIN_VS_CODE_API_VERSION:&str = "1.80.0";
17
18/// Maximum supported VS Code API version
19pub const MAX_VS_CODE_API_VERSION:&str = "1.90.0";
20
21/// Check if an API version is supported
22pub fn is_api_version_supported(version:&str) -> bool {
23	match version.parse::<semver::Version>() {
24		Ok(v) => {
25			let min = MIN_VS_CODE_API_VERSION.parse::<semver::Version>().unwrap();
26
27			let max = MAX_VS_CODE_API_VERSION.parse::<semver::Version>().unwrap();
28
29			v >= min && v <= max
30		},
31
32		Err(_) => false,
33	}
34}
35
36/// Common VS Code API utilities
37pub mod utils {
38
39	/// Convert a JSON Value to a specific type
40	pub fn from_json_value<T:serde::de::DeserializeOwned>(value:&serde_json::Value) -> Result<T, String> {
41		serde_json::from_value(value.clone()).map_err(|e| format!("Failed to deserialize JSON value: {}", e))
42	}
43
44	/// Convert a value to a JSON Value
45	pub fn to_json_value<T:serde::Serialize>(value:&T) -> Result<serde_json::Value, String> {
46		serde_json::to_value(value).map_err(|e| format!("Failed to serialize to JSON value: {}", e))
47	}
48
49	/// Check if a JSON value is null
50	pub fn is_null(value:&serde_json::Value) -> bool { matches!(value, serde_json::Value::Null) }
51}
52
53#[cfg(test)]
54mod tests {
55
56	use super::*;
57
58	#[test]
59	fn test_api_version_constants() {
60		assert!(!VS_CODE_API_VERSION.is_empty());
61
62		assert!(!MIN_VS_CODE_API_VERSION.is_empty());
63
64		assert!(!MAX_VS_CODE_API_VERSION.is_empty());
65	}
66
67	#[test]
68	fn test_is_api_version_supported() {
69		assert!(is_api_version_supported("1.85.0"));
70
71		assert!(is_api_version_supported("1.80.0"));
72
73		assert!(is_api_version_supported("1.90.0"));
74
75		assert!(!is_api_version_supported("1.79.0"));
76
77		assert!(!is_api_version_supported("1.91.0"));
78
79		assert!(!is_api_version_supported("invalid"));
80	}
81
82	#[test]
83	fn test_json_utils() {
84		use serde::{Deserialize, Serialize};
85
86		#[derive(Debug, Serialize, Deserialize, PartialEq)]
87		struct TestValue {
88			value:i32,
89		}
90
91		let value = TestValue { value:42 };
92
93		let json = utils::to_json_value(&value).unwrap();
94
95		assert_eq!(json["value"], 42);
96
97		let recovered:TestValue = utils::from_json_value(&json).unwrap();
98
99		assert_eq!(recovered, value);
100	}
101
102	#[test]
103	fn test_is_null() {
104		assert!(utils::is_null(&serde_json::Value::Null));
105
106		assert!(!utils::is_null(&serde_json::json!(42)));
107	}
108}