1use crate::error::Result;
4use std::process::Command;
5use std::sync::Once;
6use tracing::{debug, warn};
7
8const TESTED_VERSION: &str = "2.0.76";
10
11static VERSION_CHECK: Once = Once::new();
13
14pub fn check_claude_version() -> Result<()> {
17 VERSION_CHECK.call_once(|| {
18 if let Err(e) = check_version_impl() {
19 debug!("Failed to check Claude CLI version: {}", e);
20 }
21 });
22 Ok(())
23}
24
25fn check_version_impl() -> Result<()> {
27 let output = Command::new("claude")
29 .arg("--version")
30 .output()
31 .map_err(crate::error::Error::Io)?;
32
33 if !output.status.success() {
34 debug!("Failed to check Claude CLI version - command failed");
35 return Ok(());
36 }
37
38 let version_str = String::from_utf8_lossy(&output.stdout);
39 let version_line = version_str.lines().next().unwrap_or("");
40
41 if let Some(version) = version_line.split_whitespace().next() {
43 if is_version_newer(version, TESTED_VERSION) {
44 warn!(
45 "Claude CLI version {} is newer than tested version {}. \
46 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
47 version, TESTED_VERSION
48 );
49 } else {
50 debug!(
51 "Claude CLI version {} is compatible (tested: {})",
52 version, TESTED_VERSION
53 );
54 }
55 } else {
56 warn!(
57 "Could not parse Claude CLI version from output: '{}'. \
58 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
59 version_line
60 );
61 }
62
63 Ok(())
64}
65
66fn is_version_newer(version: &str, tested: &str) -> bool {
68 let v_parts: Vec<u32> = version.split('.').filter_map(|s| s.parse().ok()).collect();
69 let t_parts: Vec<u32> = tested.split('.').filter_map(|s| s.parse().ok()).collect();
70
71 use std::cmp::Ordering;
72
73 for i in 0..v_parts.len().min(t_parts.len()) {
74 match v_parts[i].cmp(&t_parts[i]) {
75 Ordering::Greater => return true,
76 Ordering::Less => return false,
77 Ordering::Equal => continue,
78 }
79 }
80
81 v_parts.len() > t_parts.len()
83}
84
85pub async fn check_claude_version_async() -> Result<()> {
87 use tokio::sync::OnceCell;
88
89 static ASYNC_VERSION_CHECK: OnceCell<()> = OnceCell::const_new();
91
92 ASYNC_VERSION_CHECK
93 .get_or_init(|| async {
94 if let Err(e) = check_version_impl_async().await {
95 debug!("Failed to check Claude CLI version: {}", e);
96 }
97 })
98 .await;
99
100 Ok(())
101}
102
103async fn check_version_impl_async() -> Result<()> {
105 use tokio::process::Command;
106
107 let output = Command::new("claude")
109 .arg("--version")
110 .output()
111 .await
112 .map_err(crate::error::Error::Io)?;
113
114 if !output.status.success() {
115 debug!("Failed to check Claude CLI version - command failed");
116 return Ok(());
117 }
118
119 let version_str = String::from_utf8_lossy(&output.stdout);
120 let version_line = version_str.lines().next().unwrap_or("");
121
122 if let Some(version) = version_line.split_whitespace().next() {
124 if is_version_newer(version, TESTED_VERSION) {
125 warn!(
126 "Claude CLI version {} is newer than tested version {}. \
127 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
128 version, TESTED_VERSION
129 );
130 } else {
131 debug!(
132 "Claude CLI version {} is compatible (tested: {})",
133 version, TESTED_VERSION
134 );
135 }
136 } else {
137 warn!(
138 "Could not parse Claude CLI version from output: '{}'. \
139 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
140 version_line
141 );
142 }
143
144 Ok(())
145}
146
147#[cfg(test)]
148mod tests {
149 use super::*;
150
151 #[test]
152 fn test_version_comparison() {
153 assert!(is_version_newer("1.0.90", "1.0.89"));
155 assert!(!is_version_newer("1.0.89", "1.0.90"));
156 assert!(!is_version_newer("1.0.89", "1.0.89"));
157
158 assert!(is_version_newer("1.1", "1.0.89"));
160 assert!(!is_version_newer("1.0", "1.0.89"));
161 assert!(is_version_newer("1.0.89.1", "1.0.89"));
162
163 assert!(is_version_newer("2.0.0", "1.99.99"));
165 assert!(!is_version_newer("0.9.99", "1.0.0"));
166 }
167}