togl 0.2.2

A CLI tool for toggling code comments across multiple languages
Documentation
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
// File I/O operations for the Toggle CLI

use crate::journal::{self, Journal, JournalEntry, JOURNAL_FILENAME, LOCK_FILENAME};
use crate::platform;
use similar::TextDiff;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tempfile::NamedTempFile;

/// Read file content as UTF-8.
pub fn read_file(path: &Path) -> io::Result<String> {
    let mut file = File::open(path)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    Ok(content)
}

/// Read file content with a specified encoding.
/// Supports any encoding label recognized by the Encoding Standard
/// (e.g., "utf-8", "latin-1", "iso-8859-1", "windows-1252", "ascii").
pub fn read_file_encoded(path: &Path, encoding: &str) -> io::Result<String> {
    if encoding.eq_ignore_ascii_case("utf-8") {
        return read_file(path);
    }
    let bytes = std::fs::read(path)?;
    let enc = resolve_encoding(encoding)?;
    let (decoded, _, had_errors) = enc.decode(&bytes);
    if had_errors {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Failed to decode file as {}", encoding),
        ));
    }
    Ok(decoded.into_owned())
}

/// Resolve an encoding label to an encoding_rs::Encoding.
/// Handles common aliases like "latin-1" that encoding_rs doesn't directly recognize.
fn resolve_encoding(label: &str) -> io::Result<&'static encoding_rs::Encoding> {
    // Try direct lookup first
    if let Some(enc) = encoding_rs::Encoding::for_label(label.as_bytes()) {
        return Ok(enc);
    }
    // Handle common aliases not in the Encoding Standard
    let alias = match label.to_ascii_lowercase().as_str() {
        "latin-1" | "latin1" => Some("iso-8859-1"),
        "ascii" | "us-ascii" => Some("windows-1252"),
        _ => None,
    };
    if let Some(alias_label) = alias {
        if let Some(enc) = encoding_rs::Encoding::for_label(alias_label.as_bytes()) {
            return Ok(enc);
        }
    }
    Err(io::Error::new(
        io::ErrorKind::InvalidInput,
        format!("Unsupported encoding: {}", label),
    ))
}

/// Check if an encoding label is valid/supported.
pub fn is_valid_encoding(label: &str) -> bool {
    if label.eq_ignore_ascii_case("utf-8") {
        return true;
    }
    resolve_encoding(label).is_ok()
}

/// Encode a string into bytes using the specified encoding.
fn encode_string(content: &str, encoding: &str) -> io::Result<Vec<u8>> {
    if encoding.eq_ignore_ascii_case("utf-8") {
        return Ok(content.as_bytes().to_vec());
    }
    let enc = resolve_encoding(encoding)?;
    let (encoded, _, had_errors) = enc.encode(content);
    if had_errors {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Failed to encode content as {}", encoding),
        ));
    }
    Ok(encoded.into_owned())
}

/// Check if a path is a symbolic link.
pub fn is_symlink(path: &Path) -> bool {
    path.symlink_metadata()
        .map(|m| m.file_type().is_symlink())
        .unwrap_or(false)
}

/// Resolve symlink target to an absolute path.
/// If the symlink target is relative, resolves it against the symlink's parent directory.
fn resolve_symlink(path: &Path) -> io::Result<PathBuf> {
    let target = std::fs::read_link(path)?;
    if target.is_absolute() {
        Ok(target)
    } else {
        let parent = path.parent().unwrap_or(Path::new("."));
        Ok(parent.join(target))
    }
}

/// Write file content atomically using a temp file + rename.
/// If `temp_suffix` is provided, uses `path.<suffix>` as the temp file name.
/// Otherwise uses a NamedTempFile in the same directory.
/// If `no_dereference` is true and path is a symlink, writes to the symlink's
/// target instead of replacing the symlink.
pub fn write_file(path: &Path, content: &str, temp_suffix: Option<&str>) -> io::Result<()> {
    write_bytes_impl(path, content.as_bytes(), temp_suffix, false)
}

/// Write file with optional symlink-aware behavior.
pub fn write_file_no_deref(
    path: &Path,
    content: &str,
    temp_suffix: Option<&str>,
    no_dereference: bool,
) -> io::Result<()> {
    let bytes = content.as_bytes();
    write_bytes_impl(path, bytes, temp_suffix, no_dereference)
}

/// Write file with encoding and symlink support.
pub fn write_file_encoded(
    path: &Path,
    content: &str,
    temp_suffix: Option<&str>,
    no_dereference: bool,
    encoding: &str,
) -> io::Result<()> {
    let bytes = encode_string(content, encoding)?;
    write_bytes_impl(path, &bytes, temp_suffix, no_dereference)
}

fn write_bytes_impl(
    path: &Path,
    bytes: &[u8],
    temp_suffix: Option<&str>,
    no_dereference: bool,
) -> io::Result<()> {
    let write_path = if no_dereference && is_symlink(path) {
        resolve_symlink(path)?
    } else {
        path.to_path_buf()
    };
    let dir = write_path.parent().unwrap_or(Path::new("."));

    if let Some(suffix) = temp_suffix {
        // Use explicit temp file name: file.py.tmp (append suffix, not replace extension)
        let mut temp_name = write_path.as_os_str().to_os_string();
        temp_name.push(".");
        temp_name.push(suffix);
        let temp_path = std::path::PathBuf::from(temp_name);
        let mut file = File::create(&temp_path)?;
        file.write_all(bytes)?;
        file.sync_all()?;
        std::fs::rename(&temp_path, &write_path)?;
    } else {
        // Use tempfile crate for safe atomic write
        let mut tmp = NamedTempFile::new_in(dir)?;
        tmp.write_all(bytes)?;
        tmp.as_file().sync_all()?;
        tmp.persist(&write_path).map_err(|e| e.error)?;
    }

    Ok(())
}

/// Print a unified diff between original and modified content.
/// No-ops if content is identical.
pub fn print_diff(path: &Path, original: &str, modified: &str) {
    if original == modified {
        return;
    }
    let diff = TextDiff::from_lines(original, modified);
    let path_str = path.display().to_string();
    print!(
        "{}",
        diff.unified_diff()
            .header(&format!("a/{}", path_str), &format!("b/{}", path_str))
    );
}

/// Create a backup copy of a file by appending the given extension.
/// e.g., create_backup("file.py", ".bak") creates "file.py.bak"
pub fn create_backup(path: &Path, extension: &str) -> io::Result<()> {
    let mut backup_path = path.as_os_str().to_os_string();
    backup_path.push(extension);
    std::fs::copy(path, PathBuf::from(backup_path))?;
    Ok(())
}

/// Normalize line endings in content.
/// - "preserve": return unchanged
/// - "lf": convert all line endings to \n
/// - "crlf": convert all line endings to \r\n
pub fn normalize_eol(content: &str, eol: &str) -> String {
    match eol {
        "lf" => content.replace("\r\n", "\n").replace('\r', "\n"),
        "crlf" => {
            // First normalize to LF, then convert to CRLF
            let lf = content.replace("\r\n", "\n").replace('\r', "\n");
            lf.replace('\n', "\r\n")
        }
        _ => content.to_string(), // "preserve" or any other value
    }
}

/// Function to detect if a file has UTF-8 BOM
pub fn has_utf8_bom(content: &[u8]) -> bool {
    content.starts_with(&[0xEF, 0xBB, 0xBF])
}

/// Detect lines that should never be toggled: shebang and encoding pragma.
/// Only checks the first two non-blank lines (shebangs are only valid on line 1,
/// PEP 263 encoding pragmas on lines 1-2).
/// Returns 0-based line indices of protected lines.
pub fn detect_protected_lines(content: &str) -> Vec<usize> {
    let mut protected = Vec::new();
    let mut non_blank_seen = 0;

    for (i, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        non_blank_seen += 1;
        if non_blank_seen > 2 {
            break;
        }

        // Shebang: must be first non-blank line
        if non_blank_seen == 1 && trimmed.starts_with("#!") {
            protected.push(i);
        }

        // PEP 263 encoding pragma: first or second non-blank line
        if trimmed.starts_with('#')
            && (trimmed.contains("coding:") || trimmed.contains("coding="))
            && !protected.contains(&i)
        {
            protected.push(i);
        }
    }

    protected
}

/// Encode a string for atomic mode staging. Public wrapper around encode_string.
pub fn encode_for_atomic(content: &str, encoding: &str) -> io::Result<Vec<u8>> {
    encode_string(content, encoding)
}

// ── Atomic multi-file batch operations ──

/// Threshold for emitting a batch size warning.
const BATCH_SIZE_WARNING_THRESHOLD: usize = 500;

/// Default backup extension for atomic mode.
const ATOMIC_BACKUP_EXT: &str = ".toggle-atomic-backup";

/// A single staged write: temp file is written and fsynced, ready for rename.
pub struct StagedWrite {
    /// Path to the staged temp file (fd already released via into_temp_path).
    pub temp_path: PathBuf,
    /// Final target path.
    pub target_path: PathBuf,
    /// SHA-256 hex digest of the written content.
    pub content_sha256: String,
    /// Original file permissions to copy to temp before rename.
    pub original_permissions: Option<std::fs::Permissions>,
}

/// Manages a two-phase atomic commit of multiple file writes.
pub struct AtomicBatch {
    staged: Vec<StagedWrite>,
    journal_path: PathBuf,
    lock_path: PathBuf,
    _lock: Option<fd_lock::RwLock<File>>,
    backup_enabled: bool,
    interrupted: Arc<AtomicBool>,
}

impl AtomicBatch {
    /// Create a new atomic batch. Acquires the lock file immediately.
    /// `targets` is used to determine the journal directory.
    /// `backup_enabled` controls whether hard-link backups are created.
    /// `interrupted` is an AtomicBool set by signal handlers.
    pub fn new(
        targets: &[PathBuf],
        backup_enabled: bool,
        interrupted: Arc<AtomicBool>,
    ) -> io::Result<Self> {
        let dir = journal::journal_dir(targets)?;
        let lock_path = dir.join(LOCK_FILENAME);
        let journal_path = dir.join(JOURNAL_FILENAME);

        // Acquire exclusive lock.
        // We keep the RwLock (and its write guard implicitly via try_write)
        // alive for the lifetime of the batch by storing the RwLock itself.
        let lock_file = File::create(&lock_path)?;
        let mut lock = fd_lock::RwLock::new(lock_file);
        // Test that we can acquire the lock; this will fail if another
        // atomic operation is running. The write guard is dropped immediately,
        // but the underlying file descriptor (held by the RwLock) keeps the
        // advisory lock on some platforms. We re-acquire below.
        {
            let _guard = lock.try_write().map_err(|_| {
                io::Error::new(
                    io::ErrorKind::WouldBlock,
                    "Another atomic operation is already in progress in this directory. \
                     Wait for it to complete or remove .toggle-atomic.lock if the previous \
                     process crashed.",
                )
            })?;
            // Guard dropped here but we keep the RwLock (and its fd) alive
        }

        Ok(Self {
            staged: Vec::new(),
            journal_path,
            lock_path,
            _lock: Some(lock),
            backup_enabled,
            interrupted,
        })
    }

    /// Stage a single file write: write content to a temp file in the same
    /// directory as the target, fsync it, then release the fd.
    pub fn stage(&mut self, target_path: &Path, content: &[u8], _encoding: &str) -> io::Result<()> {
        let target_dir = target_path.parent().unwrap_or(Path::new("."));
        let mut tmp = NamedTempFile::new_in(target_dir)?;
        let encoded = content.to_vec();
        tmp.write_all(&encoded)?;
        platform::durable_sync(tmp.as_file())?;

        // Copy permissions from original file if it exists
        let original_permissions = if target_path.exists() {
            let meta = std::fs::metadata(target_path)?;
            let perms = meta.permissions();
            tmp.as_file().set_permissions(perms.clone()).ok();
            Some(perms)
        } else {
            None
        };

        let content_sha256 = journal::sha256_hex(&encoded);

        // Release the fd but keep the path for later rename
        let temp_path_obj = tmp.into_temp_path();
        let temp_path = temp_path_obj.to_path_buf();
        // Prevent TempPath from deleting the file on drop — we manage it ourselves
        temp_path_obj
            .keep()
            .map_err(|e| io::Error::other(format!("Failed to keep temp path: {}", e)))?;

        self.staged.push(StagedWrite {
            temp_path,
            target_path: target_path.to_path_buf(),
            content_sha256,
            original_permissions,
        });

        Ok(())
    }

    /// Emit a warning if the batch size exceeds the threshold.
    pub fn warn_if_large_batch(&self) {
        if self.staged.len() > BATCH_SIZE_WARNING_THRESHOLD {
            eprintln!(
                "Warning: Staging {} files in atomic mode. Large batches may be \
                 slow due to fsync overhead. Consider splitting into smaller \
                 batches if performance is critical.",
                self.staged.len()
            );
        }
    }

    /// Execute the two-phase commit: create backups, write journal, rename all.
    /// Returns Ok(()) if all renames succeed. On failure, attempts rollback
    /// if backups are enabled.
    pub fn commit(self) -> io::Result<()> {
        if self.staged.is_empty() {
            self.cleanup_lock();
            return Ok(());
        }

        self.warn_if_large_batch();

        // Build journal entries
        let mut journal_entries: Vec<JournalEntry> = Vec::with_capacity(self.staged.len());
        for sw in &self.staged {
            let backup_path = if self.backup_enabled {
                let mut bp = sw.target_path.as_os_str().to_os_string();
                bp.push(ATOMIC_BACKUP_EXT);
                Some(PathBuf::from(bp))
            } else {
                None
            };
            journal_entries.push(JournalEntry {
                target_path: sw.target_path.clone(),
                temp_path: sw.temp_path.clone(),
                backup_path,
                content_sha256: sw.content_sha256.clone(),
                rename_completed: false,
            });
        }

        let mut j = Journal::new(journal_entries, self.backup_enabled);

        // Persist journal in Staged state
        journal::persist_journal(&j, &self.journal_path)?;

        // Create hard-link backups if enabled
        if self.backup_enabled {
            for entry in &j.entries {
                if let Some(ref backup_path) = entry.backup_path {
                    if entry.target_path.exists() {
                        if let Err(e) = std::fs::hard_link(&entry.target_path, backup_path) {
                            eprintln!(
                                "Error: failed to create backup for '{}': {}",
                                entry.target_path.display(),
                                e
                            );
                            self.rollback_staged(&j);
                            return Err(e);
                        }
                    }
                }
            }
        }

        // Transition to Committing
        j.transition_to_committing();
        journal::persist_journal(&j, &self.journal_path)?;

        if !self.backup_enabled {
            eprintln!(
                "Warning: Running without backups. If the rename phase fails, \
                 rollback is not possible."
            );
        }

        // Phase 2: Rename all temp files to targets
        let entry_count = j.entries.len();
        for idx in 0..entry_count {
            // Check for signal interrupt between renames
            if self.interrupted.load(Ordering::Relaxed) {
                eprintln!("Interrupted. Journal preserved for recovery.");
                journal::persist_journal(&j, &self.journal_path)?;
                return Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    "Atomic commit interrupted by signal. \
                     Run with --recover to clean up.",
                ));
            }

            let temp_path = j.entries[idx].temp_path.clone();
            let target_path = j.entries[idx].target_path.clone();

            // Copy permissions before rename
            if let Some(ref perms) = self.staged[idx].original_permissions {
                let _ = std::fs::set_permissions(&temp_path, perms.clone());
            }

            match platform::rename_with_retry(&temp_path, &target_path) {
                Ok(()) => {
                    j.mark_entry_completed(idx);
                    journal::persist_journal_best_effort(&j, &self.journal_path);
                }
                Err(e) => {
                    eprintln!(
                        "Error: rename failed for '{}': {}",
                        target_path.display(),
                        e
                    );
                    if self.backup_enabled {
                        eprintln!("Attempting rollback...");
                        if let Err(rb_err) = journal::recover_rollback(&j, &self.journal_path) {
                            eprintln!("Rollback also failed: {}", rb_err);
                        }
                    } else {
                        let _ = journal::persist_journal(&j, &self.journal_path);
                        eprintln!(
                            "No backups available. Journal preserved at '{}' for manual recovery.",
                            self.journal_path.display()
                        );
                    }
                    return Err(e);
                }
            }
        }

        // Finalization: fsync parent directories
        let mut synced_dirs = std::collections::HashSet::new();
        for entry in &j.entries {
            if let Some(parent) = entry.target_path.parent() {
                if synced_dirs.insert(parent.to_path_buf()) {
                    let _ = platform::sync_dir(parent);
                }
            }
        }

        // Delete journal
        journal::delete_journal(&self.journal_path)?;

        // Clean up atomic backup files
        if self.backup_enabled {
            for entry in &j.entries {
                if let Some(ref backup_path) = entry.backup_path {
                    let _ = std::fs::remove_file(backup_path);
                }
            }
        }

        self.cleanup_lock();
        Ok(())
    }

    /// Rollback from Staged state: delete all temp files and backups, delete journal.
    fn rollback_staged(&self, journal: &Journal) {
        for entry in &journal.entries {
            if entry.temp_path.exists() {
                let _ = std::fs::remove_file(&entry.temp_path);
            }
            if let Some(ref backup_path) = entry.backup_path {
                if backup_path.exists() {
                    let _ = std::fs::remove_file(backup_path);
                }
            }
        }
        let _ = journal::delete_journal(&self.journal_path);
        self.cleanup_lock();
    }

    /// Clean up the lock file.
    fn cleanup_lock(&self) {
        let _ = std::fs::remove_file(&self.lock_path);
    }
}

impl Drop for AtomicBatch {
    fn drop(&mut self) {
        // If we're being dropped without commit() having cleaned up,
        // the lock file should still be removed.
        // Note: staged temp files are NOT cleaned up on drop since we called
        // keep() on them. The journal (if written) provides recovery info.
    }
}

/// Trait abstracting filesystem operations for testability.
/// Production code uses `RealFileOps`; tests can inject failures.
pub trait FileOps {
    fn rename(&self, from: &Path, to: &Path) -> io::Result<()>;
    fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()>;
    fn remove_file(&self, path: &Path) -> io::Result<()>;
    fn sync_dir(&self, path: &Path) -> io::Result<()>;
}

/// Production filesystem operations using std::fs.
pub struct RealFileOps;

impl FileOps for RealFileOps {
    fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
        platform::rename_with_retry(from, to)
    }

    fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()> {
        std::fs::hard_link(src, dst)
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        std::fs::remove_file(path)
    }

    fn sync_dir(&self, path: &Path) -> io::Result<()> {
        platform::sync_dir(path)
    }
}