Skip to main content

ggen_core/utils/
versioning.rs

1// API Versioning Helpers
2// Provides compile-time deprecation warnings and migration guidance
3
4/// Deprecation macro with version tracking and migration notes
5///
6/// # Example
7/// ```ignore
8/// use ggen_core::deprecated_since;
9///
10/// #[deprecated_since!("1.0.0", "Use new_api() instead. See migration guide at docs/MIGRATION.md")]
11/// pub fn old_api() -> Result<String, Error> {
12///     // Legacy implementation
13///     Ok("deprecated".to_string())
14/// }
15/// ```
16#[macro_export]
17macro_rules! deprecated_since {
18    ($version:expr, $note:expr) => {
19        #[deprecated(since = $version, note = $note)]
20    };
21}
22
23/// Marks a feature as experimental with version tracking
24///
25/// # Example
26/// ```ignore
27/// use ggen_core::experimental;
28///
29/// #[experimental!("1.0.0", "This API may change in future versions")]
30/// pub fn experimental_feature() -> Result<(), Error> {
31///     // Experimental implementation
32///     Ok(())
33/// }
34/// ```
35#[macro_export]
36macro_rules! experimental {
37    ($since:expr, $note:expr) => {
38        #[doc = concat!("⚠️ **Experimental** (since ", $since, "): ", $note)]
39    };
40}
41
42/// Marks a breaking change with migration path
43///
44/// # Example
45/// ```ignore
46/// use ggen_core::breaking_change;
47///
48/// #[breaking_change!("2.0.0", "Signature changed. Old: fn(i32) -> i32, New: fn(i64) -> Result<i64, Error>")]
49/// pub fn updated_api(value: i64) -> Result<i64, Error> {
50///     Ok(value * 2)
51/// }
52/// ```
53#[macro_export]
54macro_rules! breaking_change {
55    ($version:expr, $change:expr) => {
56        #[doc = concat!("⚠️ **Breaking Change in ", $version, "**: ", $change)]
57    };
58}
59
60/// Version compatibility checker
61pub struct VersionChecker;
62
63impl VersionChecker {
64    /// Checks if a version is compatible with the current API
65    ///
66    /// # Example
67    /// ```
68    /// use crate::utils::versioning::VersionChecker;
69    ///
70    /// let compatible = VersionChecker::is_compatible("1.0.0", "1.2.0");
71    /// assert!(compatible.unwrap());
72    /// ```
73    pub fn is_compatible(requested: &str, current: &str) -> Result<bool, String> {
74        let req_parts = parse_version(requested)?;
75        let cur_parts = parse_version(current)?;
76
77        // SemVer compatibility: same major version, current >= requested
78        Ok(req_parts.0 == cur_parts.0
79            && (cur_parts.1 > req_parts.1
80                || (cur_parts.1 == req_parts.1 && cur_parts.2 >= req_parts.2)))
81    }
82}
83
84fn parse_version(version: &str) -> Result<(u32, u32, u32), String> {
85    let parts: Vec<&str> = version.split('.').collect();
86
87    if parts.len() != 3 {
88        return Err(format!("Invalid version format: {}", version));
89    }
90
91    let major = parts[0]
92        .parse::<u32>()
93        .map_err(|_| format!("Invalid major version: {}", parts[0]))?;
94    let minor = parts[1]
95        .parse::<u32>()
96        .map_err(|_| format!("Invalid minor version: {}", parts[1]))?;
97    let patch = parts[2]
98        .parse::<u32>()
99        .map_err(|_| format!("Invalid patch version: {}", parts[2]))?;
100
101    Ok((major, minor, patch))
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_version_compatibility_same_version() {
110        // Arrange
111        let requested = "1.0.0";
112        let current = "1.0.0";
113
114        // Act
115        let result = VersionChecker::is_compatible(requested, current);
116
117        // Assert
118        assert!(result.unwrap());
119    }
120
121    #[test]
122    fn test_version_compatibility_minor_upgrade() {
123        // Arrange
124        let requested = "1.0.0";
125        let current = "1.2.0";
126
127        // Act
128        let result = VersionChecker::is_compatible(requested, current);
129
130        // Assert
131        assert!(result.unwrap());
132    }
133
134    #[test]
135    fn test_version_compatibility_major_incompatible() {
136        // Arrange
137        let requested = "1.0.0";
138        let current = "2.0.0";
139
140        // Act
141        let result = VersionChecker::is_compatible(requested, current);
142
143        // Assert
144        assert!(!result.unwrap());
145    }
146
147    #[test]
148    fn test_version_parsing_invalid_format() {
149        // Arrange
150        let invalid = "1.0";
151
152        // Act
153        let result = parse_version(invalid);
154
155        // Assert
156        assert!(result.is_err());
157        assert!(result.unwrap_err().contains("Invalid version format"));
158    }
159}