basefmt 0.1.0

A formatter that applies universal formatting rules to any text file
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
use crate::editorconfig;
use std::fs;
use std::io::{self, Read, Write};
use std::path::Path;
use tempfile::NamedTempFile;

/// Result of a format operation.
#[derive(Debug, PartialEq, Eq)]
pub enum FormatResult {
    /// File was modified
    Changed,
    /// File was already properly formatted
    Unchanged,
    /// File was skipped (e.g., binary file)
    Skipped,
}

/// Result of a check operation.
#[derive(Debug, PartialEq, Eq)]
pub enum CheckResult {
    /// File is properly formatted
    Formatted,
    /// File needs formatting
    NeedsFormatting,
    /// File was skipped (e.g., binary file)
    Skipped,
}

fn read_and_format_with_rules(
    path: &Path,
    rules: &editorconfig::FormatRules,
) -> io::Result<Option<(String, String, fs::Metadata)>> {
    let file = fs::File::open(path)?;
    let metadata = file.metadata()?;

    let mut content = String::new();
    let mut reader = io::BufReader::new(file);
    match reader.read_to_string(&mut content) {
        Ok(_) => {
            let formatted = format_content(&content, rules);
            Ok(Some((content, formatted, metadata)))
        }
        Err(err) if err.kind() == io::ErrorKind::InvalidData => {
            // Skip binary files silently
            Ok(None)
        }
        Err(err) => Err(err),
    }
}

/// Formats a file in place, preserving file permissions and metadata.
///
/// Applies formatting rules including:
/// - Removing leading newlines
/// - Removing trailing spaces from each line
/// - Ensuring exactly one final newline
///
/// Binary files (files containing invalid UTF-8) are silently skipped and
/// treated as if they don't need formatting.
///
/// The file is only modified if formatting changes are needed. File permissions
/// and other metadata are preserved through atomic write-and-rename operation.
///
/// # Arguments
///
/// * `path` - Path to the file to format
///
/// # Returns
///
/// Returns:
/// - `Ok(FormatResult::Changed)` if the file was modified
/// - `Ok(FormatResult::Unchanged)` if no changes were needed
/// - `Ok(FormatResult::Skipped)` if the file is binary
/// - `Err(...)` if the file cannot be read or written
///
/// # Examples
///
/// ```no_run
/// use basefmt::format::{format_file, FormatResult};
/// use std::path::Path;
///
/// match format_file(Path::new("file.txt")).unwrap() {
///     FormatResult::Changed => println!("File was formatted"),
///     FormatResult::Unchanged => println!("File was already formatted"),
///     FormatResult::Skipped => println!("File was skipped"),
/// }
/// ```
pub fn format_file(path: &Path) -> io::Result<FormatResult> {
    let rules = editorconfig::get_format_rules(path);
    format_file_with_rules(path, &rules)
}

/// Formats a file in place using precomputed formatting rules.
pub fn format_file_with_rules(
    path: &Path,
    rules: &editorconfig::FormatRules,
) -> io::Result<FormatResult> {
    if let Some((content, formatted, metadata)) = read_and_format_with_rules(path, rules)? {
        let changed = write_formatted_output(path, content, formatted, metadata)?;
        if changed {
            Ok(FormatResult::Changed)
        } else {
            Ok(FormatResult::Unchanged)
        }
    } else {
        Ok(FormatResult::Skipped)
    }
}

fn write_formatted_output(
    path: &Path,
    original: String,
    formatted: String,
    metadata: fs::Metadata,
) -> io::Result<bool> {
    let changed = original != formatted;
    if changed {
        // Write to a temporary file first, then rename to preserve metadata
        let parent_dir = path.parent().unwrap_or_else(|| Path::new("."));
        let mut temp_file = NamedTempFile::new_in(parent_dir)?;
        temp_file.write_all(formatted.as_bytes())?;
        temp_file.as_file().sync_all()?;

        // Set permissions before persisting
        temp_file
            .as_file()
            .set_permissions(metadata.permissions())?;

        // Atomically replace the original file
        temp_file.persist(path)?;
    }

    Ok(changed)
}

/// Checks if a file is properly formatted without modifying it.
///
/// Binary files (files containing invalid UTF-8) are silently skipped.
///
/// # Arguments
///
/// * `path` - Path to the file to check
///
/// # Returns
///
/// Returns:
/// - `Ok(CheckResult::Formatted)` if the file is properly formatted
/// - `Ok(CheckResult::NeedsFormatting)` if formatting is needed
/// - `Ok(CheckResult::Skipped)` if the file is binary
/// - `Err(...)` if the file cannot be read
///
/// # Examples
///
/// ```no_run
/// use basefmt::format::{check_file, CheckResult};
/// use std::path::Path;
///
/// match check_file(Path::new("file.txt")).unwrap() {
///     CheckResult::Formatted => println!("File is properly formatted"),
///     CheckResult::NeedsFormatting => println!("File needs formatting"),
///     CheckResult::Skipped => println!("File was skipped"),
/// }
/// ```
pub fn check_file(path: &Path) -> io::Result<CheckResult> {
    let rules = editorconfig::get_format_rules(path);
    check_file_with_rules(path, &rules)
}

/// Checks a file using already resolved formatting rules.
pub fn check_file_with_rules(
    path: &Path,
    rules: &editorconfig::FormatRules,
) -> io::Result<CheckResult> {
    if let Some((content, formatted, _metadata)) = read_and_format_with_rules(path, rules)? {
        if content == formatted {
            Ok(CheckResult::Formatted)
        } else {
            Ok(CheckResult::NeedsFormatting)
        }
    } else {
        Ok(CheckResult::Skipped)
    }
}

fn format_content(content: &str, rules: &editorconfig::FormatRules) -> String {
    // If no rules are enabled, return content as-is
    if !rules.remove_leading_newlines
        && !rules.remove_trailing_spaces
        && !rules.ensure_final_newline
    {
        return content.to_string();
    }

    // Collect lines, optionally skipping leading empty lines
    let lines_iter = content.lines();
    let mut lines: Vec<&str> = if rules.remove_leading_newlines {
        lines_iter.skip_while(|line| line.is_empty()).collect()
    } else {
        lines_iter.collect()
    };

    // Always remove trailing empty lines (to normalize file endings)
    while lines.last().is_some_and(|line| line.is_empty()) {
        lines.pop();
    }

    if lines.is_empty() {
        return String::new();
    }

    // Build result with capacity hint to avoid reallocations
    let mut result = String::with_capacity(content.len());
    for (i, line) in lines.iter().enumerate() {
        if i > 0 {
            result.push('\n');
        }
        // Optionally trim trailing spaces
        if rules.remove_trailing_spaces {
            result.push_str(line.trim_end());
        } else {
            result.push_str(line);
        }
    }

    // Optionally add final newline
    if rules.ensure_final_newline {
        result.push('\n');
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    // Helper function to create a .editorconfig file with all rules enabled
    fn create_default_editorconfig(dir: &TempDir) {
        let config_path = dir.path().join(".editorconfig");
        fs::write(
            config_path,
            r#"
root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true
trim_leading_newlines = true
"#,
        )
        .unwrap();
    }

    #[test]
    fn test_format_content_removes_leading_newlines() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "\n\nfirst line\nsecond line\n";
        let expected = "first line\nsecond line\n";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_content_removes_trailing_spaces() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "line with trailing spaces   \nanother line with spaces  \n";
        let expected = "line with trailing spaces\nanother line with spaces\n";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_content_adds_final_newline() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "first line\nsecond line";
        let expected = "first line\nsecond line\n";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_content_removes_multiple_final_newlines() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "first line\nsecond line\n\n\n";
        let expected = "first line\nsecond line\n";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_content_empty_file() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "";
        let expected = "";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_content_only_newlines() {
        let rules = editorconfig::FormatRules {
            ensure_final_newline: true,
            remove_trailing_spaces: true,
            remove_leading_newlines: true,
        };
        let input = "\n\n\n";
        let expected = "";
        assert_eq!(format_content(input, &rules), expected);
    }

    #[test]
    fn test_format_file_creates_changes() {
        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "\n\ntest content  \n\n").unwrap();

        let result = format_file(&file_path).unwrap();

        assert_eq!(result, FormatResult::Changed);
        let content = fs::read_to_string(&file_path).unwrap();
        assert_eq!(content, "test content\n");
    }

    #[test]
    fn test_format_file_no_changes() {
        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "test content\n").unwrap();

        let result = format_file(&file_path).unwrap();

        assert_eq!(result, FormatResult::Unchanged);
        let content = fs::read_to_string(&file_path).unwrap();
        assert_eq!(content, "test content\n");
    }

    #[test]
    fn test_check_file_clean() {
        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "test content\n").unwrap();

        let result = check_file(&file_path).unwrap();

        assert_eq!(result, CheckResult::Formatted);
    }

    #[test]
    fn test_check_file_dirty() {
        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "\n\ntest content  \n\n").unwrap();

        let result = check_file(&file_path).unwrap();

        assert_eq!(result, CheckResult::NeedsFormatting);
    }

    #[test]
    #[cfg(unix)]
    fn test_format_file_preserves_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "\n\ntest content  \n\n").unwrap();

        // Set specific permissions (e.g., 0o644)
        let perms = fs::Permissions::from_mode(0o755);
        fs::set_permissions(&file_path, perms).unwrap();

        let original_mode = fs::metadata(&file_path).unwrap().permissions().mode();

        // Format the file
        format_file(&file_path).unwrap();

        // Check permissions are preserved
        let new_mode = fs::metadata(&file_path).unwrap().permissions().mode();
        assert_eq!(original_mode, new_mode);
    }

    #[test]
    fn test_format_file_skips_binary() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("binary.bin");
        // Write invalid UTF-8 bytes
        fs::write(&file_path, [0xFF, 0xFE, 0xFD]).unwrap();

        let result = format_file(&file_path).unwrap();

        // Binary files should be skipped silently
        assert_eq!(result, FormatResult::Skipped);

        // Verify file was not modified
        let content = fs::read(&file_path).unwrap();
        assert_eq!(content, vec![0xFF, 0xFE, 0xFD]);
    }

    #[test]
    fn test_check_file_skips_binary() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("binary.bin");
        // Write invalid UTF-8 bytes
        fs::write(&file_path, [0xFF, 0xFE, 0xFD]).unwrap();

        let result = check_file(&file_path).unwrap();

        // Binary files should be skipped silently
        assert_eq!(result, CheckResult::Skipped);
    }
}