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