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