rch 2.1.18

Remote Compilation Helper - PreToolUse hook CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Core types for the update system.

use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;

/// Cache duration for version check results (24 hours).
pub const CACHE_DURATION: Duration = Duration::from_secs(24 * 60 * 60);

/// Maximum number of backup versions to keep.
pub const MAX_BACKUPS: usize = 3;

/// Error types for update operations.
#[derive(Error, Debug)]
pub enum UpdateError {
    #[error("Failed to check for updates: {0}")]
    CheckFailed(String),

    #[error("Failed to download release: {0}")]
    DownloadFailed(String),

    #[error("Checksum verification failed: expected {expected}, got {actual}")]
    ChecksumMismatch { expected: String, actual: String },

    #[error("Checksum file missing for release asset: {asset}. Use --skip-verify to override.")]
    ChecksumMissing { asset: String },

    #[error("Installation failed: {0}")]
    InstallFailed(String),

    #[error("Update lock held by another process")]
    LockHeld,

    #[error("No backup available for rollback")]
    NoBackupAvailable,

    #[error("Network error: {0}")]
    NetworkError(String),

    #[error("Invalid version format: {0}")]
    InvalidVersion(String),

    #[error("Unsupported platform: {0}")]
    UnsupportedPlatform(String),

    #[error("Signature verification failed: {0}")]
    SignatureVerificationFailed(String),
}

/// Release channel for updates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Channel {
    #[default]
    Stable,
    Beta,
    Nightly,
}

impl FromStr for Channel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "stable" => Ok(Channel::Stable),
            "beta" => Ok(Channel::Beta),
            "nightly" => Ok(Channel::Nightly),
            _ => Err(format!("Unknown channel: {}", s)),
        }
    }
}

impl fmt::Display for Channel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Channel::Stable => write!(f, "stable"),
            Channel::Beta => write!(f, "beta"),
            Channel::Nightly => write!(f, "nightly"),
        }
    }
}

/// Semantic version with comparison support.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version {
    pub major: u32,
    pub minor: u32,
    pub patch: u32,
    pub prerelease: Option<String>,
    raw: String,
}

impl Version {
    /// Parse a version string (e.g., "0.1.0", "v0.1.0", "0.1.0-beta.1").
    pub fn parse(s: &str) -> Result<Self, UpdateError> {
        let s = s.strip_prefix('v').unwrap_or(s);
        let raw = s.to_string();

        // Split prerelease suffix
        let (version_part, prerelease) = if let Some(idx) = s.find('-') {
            (&s[..idx], Some(s[idx + 1..].to_string()))
        } else {
            (s, None)
        };

        let parts: Vec<&str> = version_part.split('.').collect();
        if parts.len() < 2 || parts.len() > 3 {
            return Err(UpdateError::InvalidVersion(raw));
        }

        let major = parts[0]
            .parse()
            .map_err(|_| UpdateError::InvalidVersion(raw.clone()))?;
        let minor = parts[1]
            .parse()
            .map_err(|_| UpdateError::InvalidVersion(raw.clone()))?;
        let patch = if parts.len() > 2 {
            parts[2]
                .parse()
                .map_err(|_| UpdateError::InvalidVersion(raw.clone()))?
        } else {
            0
        };

        Ok(Self {
            major,
            minor,
            patch,
            prerelease,
            raw,
        })
    }

    /// Check if this is a prerelease version.
    #[allow(dead_code)]
    pub fn is_prerelease(&self) -> bool {
        self.prerelease.is_some()
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref pre) = self.prerelease {
            write!(f, "{}.{}.{}-{}", self.major, self.minor, self.patch, pre)
        } else {
            write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
        }
    }
}

impl PartialEq for Version {
    fn eq(&self, other: &Self) -> bool {
        self.major == other.major
            && self.minor == other.minor
            && self.patch == other.patch
            && self.prerelease == other.prerelease
    }
}

impl Eq for Version {}

impl PartialOrd for Version {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Version {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.major.cmp(&other.major) {
            Ordering::Equal => {}
            ord => return ord,
        }
        match self.minor.cmp(&other.minor) {
            Ordering::Equal => {}
            ord => return ord,
        }
        match self.patch.cmp(&other.patch) {
            Ordering::Equal => {}
            ord => return ord,
        }

        // Prerelease versions are less than release versions
        match (&self.prerelease, &other.prerelease) {
            (None, None) => Ordering::Equal,
            (Some(_), None) => Ordering::Less,
            (None, Some(_)) => Ordering::Greater,
            (Some(a), Some(b)) => cmp_prerelease(a, b),
        }
    }
}

fn cmp_prerelease(left: &str, right: &str) -> Ordering {
    let mut left_parts = left.split('.');
    let mut right_parts = right.split('.');

    loop {
        match (left_parts.next(), right_parts.next()) {
            (Some(left_part), Some(right_part)) => {
                let ordering = cmp_prerelease_identifier(left_part, right_part);
                if ordering != Ordering::Equal {
                    return ordering;
                }
            }
            (None, None) => return Ordering::Equal,
            (None, Some(_)) => return Ordering::Less,
            (Some(_), None) => return Ordering::Greater,
        }
    }
}

fn cmp_prerelease_identifier(left: &str, right: &str) -> Ordering {
    match (numeric_identifier(left), numeric_identifier(right)) {
        (Some(left_num), Some(right_num)) => cmp_numeric_identifier(left_num, right_num),
        (Some(_), None) => Ordering::Less,
        (None, Some(_)) => Ordering::Greater,
        (None, None) => left.cmp(right),
    }
}

fn numeric_identifier(identifier: &str) -> Option<&str> {
    if identifier.is_empty() || !identifier.bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    Some(identifier)
}

fn cmp_numeric_identifier(left: &str, right: &str) -> Ordering {
    left.len().cmp(&right.len()).then_with(|| left.cmp(right))
}

/// Information about a GitHub release.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseInfo {
    pub tag_name: String,
    pub name: String,
    pub prerelease: bool,
    pub draft: bool,
    pub html_url: String,
    pub body: Option<String>,
    pub assets: Vec<ReleaseAsset>,
    pub published_at: Option<String>,
}

/// A release asset (downloadable file).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseAsset {
    pub name: String,
    pub browser_download_url: String,
    pub size: u64,
    pub content_type: String,
}

#[allow(dead_code)]
impl ReleaseAsset {
    /// Check if this asset is for the current platform.
    pub fn is_for_current_platform(&self) -> bool {
        current_release_targets()
            .into_iter()
            .any(|target| self.name.contains(&target))
    }
}

fn release_os_name(os: &str) -> &str {
    match os {
        "macos" => "darwin",
        other => other,
    }
}

fn target_for(os: &str, arch: &str) -> String {
    let os_str = match os {
        "linux" => "unknown-linux-gnu",
        "macos" => "apple-darwin",
        "windows" => "pc-windows-msvc",
        other => other,
    };

    format!("{arch}-{os_str}")
}

/// Get the current target triple.
pub fn current_target() -> String {
    target_for(std::env::consts::OS, std::env::consts::ARCH)
}

fn release_targets_for(os: &str, arch: &str) -> Vec<String> {
    let mut targets = vec![target_for(os, arch)];

    if os == "linux" && arch == "x86_64" {
        // Linux x86_64 releases are shipped as static musl builds.
        targets.push("x86_64-unknown-linux-musl".to_string());
    }

    targets.push(format!("{}-{}", release_os_name(os), arch));
    targets.dedup();
    targets
}

pub fn current_release_targets() -> Vec<String> {
    release_targets_for(std::env::consts::OS, std::env::consts::ARCH)
}

pub fn current_release_archive_extension() -> &'static str {
    match std::env::consts::OS {
        "windows" => ".zip",
        _ => ".tar.gz",
    }
}

/// Result of checking for updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateCheck {
    pub current_version: Version,
    pub latest_version: Version,
    pub update_available: bool,
    pub release_url: String,
    pub release_notes: Option<String>,
    pub changelog_diff: Option<String>,
    pub assets: Vec<ReleaseAsset>,
}

/// Cached version check result with timestamp.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedCheck {
    /// The cached update check result.
    pub result: UpdateCheck,
    /// Unix timestamp (seconds) when the check was cached.
    pub cached_at_secs: u64,
}

impl CachedCheck {
    /// Check if the cache is still valid (less than CACHE_DURATION old).
    pub fn is_valid(&self) -> bool {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        now.saturating_sub(self.cached_at_secs) < CACHE_DURATION.as_secs()
    }
}

/// Metadata for a backup entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupEntry {
    /// Version string of the backup.
    pub version: String,
    /// Unix timestamp (seconds) when the backup was created.
    pub created_at: u64,
    /// Original installation path that was backed up.
    pub original_path: PathBuf,
    /// The backup directory path.
    #[serde(default)]
    pub backup_path: PathBuf,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version_parse() {
        let v = Version::parse("0.1.0").unwrap();
        assert_eq!(v.major, 0);
        assert_eq!(v.minor, 1);
        assert_eq!(v.patch, 0);
        assert!(v.prerelease.is_none());
    }

    #[test]
    fn test_version_parse_with_v_prefix() {
        let v = Version::parse("v1.2.3").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
    }

    #[test]
    fn test_version_parse_prerelease() {
        let v = Version::parse("0.2.0-beta.1").unwrap();
        assert_eq!(v.major, 0);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 0);
        assert_eq!(v.prerelease, Some("beta.1".to_string()));
        assert!(v.is_prerelease());
    }

    #[test]
    fn test_version_comparison() {
        let v1 = Version::parse("0.1.0").unwrap();
        let v2 = Version::parse("0.2.0").unwrap();
        let v3 = Version::parse("0.2.0-beta.1").unwrap();

        assert!(v1 < v2);
        assert!(v3 < v2); // prerelease < release
        assert!(v1 < v3);
    }

    #[test]
    fn test_version_display() {
        let v = Version::parse("1.2.3").unwrap();
        assert_eq!(v.to_string(), "1.2.3");

        let v_pre = Version::parse("1.2.3-rc.1").unwrap();
        assert_eq!(v_pre.to_string(), "1.2.3-rc.1");
    }

    #[test]
    fn test_channel_from_str() {
        assert_eq!(Channel::from_str("stable").unwrap(), Channel::Stable);
        assert_eq!(Channel::from_str("BETA").unwrap(), Channel::Beta);
        assert_eq!(Channel::from_str("Nightly").unwrap(), Channel::Nightly);
        assert!(Channel::from_str("unknown").is_err());
    }

    #[test]
    fn test_current_target() {
        let target = current_target();
        assert!(!target.is_empty());
        // Should contain architecture
        assert!(target.contains("x86_64") || target.contains("aarch64") || target.contains("arm"));
    }

    #[test]
    fn test_release_targets_for_linux_x86_64_include_musl_and_alias() {
        let targets = release_targets_for("linux", "x86_64");
        assert_eq!(
            targets,
            vec![
                "x86_64-unknown-linux-gnu".to_string(),
                "x86_64-unknown-linux-musl".to_string(),
                "linux-x86_64".to_string()
            ]
        );
    }

    #[test]
    fn test_release_targets_for_macos_aarch64_include_darwin_alias() {
        let targets = release_targets_for("macos", "aarch64");
        assert_eq!(
            targets,
            vec![
                "aarch64-apple-darwin".to_string(),
                "darwin-aarch64".to_string()
            ]
        );
    }

    #[test]
    fn test_current_release_archive_extension_is_platform_appropriate() {
        let extension = current_release_archive_extension();
        if std::env::consts::OS == "windows" {
            assert_eq!(extension, ".zip");
        } else {
            assert_eq!(extension, ".tar.gz");
        }
    }

    #[test]
    fn test_version_parse_invalid_empty() {
        let result = Version::parse("");
        assert!(result.is_err());
    }

    #[test]
    fn test_version_parse_invalid_non_numeric() {
        let result = Version::parse("a.b.c");
        assert!(result.is_err());
    }

    #[test]
    fn test_version_parse_invalid_single_number() {
        let result = Version::parse("1");
        assert!(result.is_err());
    }

    #[test]
    fn test_version_parse_two_parts() {
        // Two-part version should work (patch defaults to 0)
        let v = Version::parse("1.2").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 0);
    }

    #[test]
    fn test_version_parse_too_many_parts() {
        let result = Version::parse("1.2.3.4");
        assert!(result.is_err());
    }

    #[test]
    fn test_version_equality() {
        let v1 = Version::parse("1.2.3").unwrap();
        let v2 = Version::parse("1.2.3").unwrap();
        assert_eq!(v1, v2);

        let v3 = Version::parse("v1.2.3").unwrap();
        assert_eq!(v1, v3); // v prefix should be stripped
    }

    #[test]
    fn test_version_prerelease_ordering() {
        let alpha = Version::parse("1.0.0-alpha").unwrap();
        let beta = Version::parse("1.0.0-beta").unwrap();
        let release = Version::parse("1.0.0").unwrap();

        assert!(alpha < beta); // alpha < beta lexicographically
        assert!(beta < release); // prerelease < release
        assert!(alpha < release);
    }

    #[test]
    fn test_version_prerelease_numeric_identifiers_order_semver() {
        let beta_2 = Version::parse("1.0.0-beta.2").unwrap();
        let beta_10 = Version::parse("1.0.0-beta.10").unwrap();
        let rc_1 = Version::parse("1.0.0-rc.1").unwrap();
        let rc_2 = Version::parse("1.0.0-rc.2").unwrap();

        assert!(beta_2 < beta_10);
        assert!(rc_1 < rc_2);
    }

    #[test]
    fn test_version_prerelease_numeric_identifiers_do_not_overflow() {
        let lower = Version::parse("1.0.0-beta.99999999999999999999999999999999999999").unwrap();
        let higher = Version::parse("1.0.0-beta.100000000000000000000000000000000000000").unwrap();

        assert!(lower < higher);
    }

    #[test]
    fn test_version_prerelease_numeric_identifiers_keep_noncanonical_forms_distinct() {
        let beta_1 = Version::parse("1.0.0-beta.1").unwrap();
        let beta_01 = Version::parse("1.0.0-beta.01").unwrap();

        assert_ne!(beta_1, beta_01);
        assert!(beta_1 < beta_01);
    }

    #[test]
    fn test_version_prerelease_numeric_identifiers_sort_before_text() {
        let alpha_1 = Version::parse("1.0.0-alpha.1").unwrap();
        let alpha_beta = Version::parse("1.0.0-alpha.beta").unwrap();

        assert!(alpha_1 < alpha_beta);
    }

    #[test]
    fn test_version_prerelease_longer_identifier_list_has_higher_precedence() {
        let beta = Version::parse("1.0.0-beta").unwrap();
        let beta_2 = Version::parse("1.0.0-beta.2").unwrap();
        let beta_2_1 = Version::parse("1.0.0-beta.2.1").unwrap();

        assert!(beta < beta_2);
        assert!(beta_2 < beta_2_1);
    }

    #[test]
    fn test_version_ordering_across_major() {
        let v1 = Version::parse("0.9.9").unwrap();
        let v2 = Version::parse("1.0.0").unwrap();
        assert!(v1 < v2);
    }

    #[test]
    fn test_channel_display() {
        assert_eq!(Channel::Stable.to_string(), "stable");
        assert_eq!(Channel::Beta.to_string(), "beta");
        assert_eq!(Channel::Nightly.to_string(), "nightly");
    }

    #[test]
    fn test_cached_check_is_valid_fresh() {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let cached = CachedCheck {
            result: UpdateCheck {
                current_version: Version::parse("1.0.0").unwrap(),
                latest_version: Version::parse("1.0.0").unwrap(),
                update_available: false,
                release_url: String::new(),
                release_notes: None,
                changelog_diff: None,
                assets: vec![],
            },
            cached_at_secs: now,
        };

        assert!(cached.is_valid());
    }

    #[test]
    fn test_cached_check_is_valid_stale() {
        // Set cached_at to 25 hours ago (beyond CACHE_DURATION of 24 hours)
        let stale_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            - (25 * 60 * 60);

        let cached = CachedCheck {
            result: UpdateCheck {
                current_version: Version::parse("1.0.0").unwrap(),
                latest_version: Version::parse("1.0.0").unwrap(),
                update_available: false,
                release_url: String::new(),
                release_notes: None,
                changelog_diff: None,
                assets: vec![],
            },
            cached_at_secs: stale_time,
        };

        assert!(!cached.is_valid());
    }

    #[test]
    fn test_update_error_display() {
        let err = UpdateError::ChecksumMismatch {
            expected: "abc".to_string(),
            actual: "def".to_string(),
        };
        let msg = format!("{}", err);
        assert!(msg.contains("abc"));
        assert!(msg.contains("def"));

        let err = UpdateError::LockHeld;
        assert!(format!("{}", err).contains("lock"));

        let err = UpdateError::InvalidVersion("bad".to_string());
        assert!(format!("{}", err).contains("bad"));
    }

    #[test]
    fn test_backup_entry_serde() {
        let entry = BackupEntry {
            version: "1.0.0".to_string(),
            created_at: 1234567890,
            original_path: PathBuf::from("/usr/bin/rch"),
            backup_path: PathBuf::from("/var/backup/rch-1.0.0"),
        };

        let json = serde_json::to_string(&entry).unwrap();
        let parsed: BackupEntry = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.version, "1.0.0");
        assert_eq!(parsed.created_at, 1234567890);
    }
}