heroforge-core 0.2.2

Pure Rust core library for reading and writing Fossil SCM repositories
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
//! Filesystem operations and metadata types
//!
//! This module defines the core types for filesystem operations and file metadata.

/// File kind/type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
    /// Regular file
    File,
    /// Directory
    Directory,
    /// Symbolic link
    Symlink,
}

/// File metadata
#[derive(Debug, Clone)]
pub struct FileMetadata {
    /// File path
    pub path: String,

    /// Whether this is a directory
    pub is_dir: bool,

    /// File size in bytes
    pub size: u64,

    /// File permissions
    pub permissions: FilePermissions,

    /// Whether path is a symlink
    pub is_symlink: bool,

    /// Symlink target (if is_symlink)
    pub symlink_target: Option<String>,

    /// Last modified time (Unix timestamp)
    pub modified: i64,

    /// File hash (for versioning)
    pub hash: Option<String>,

    /// File kind
    pub kind: FileKind,
}

impl FileMetadata {
    /// Check if file is readable
    pub fn is_readable(&self) -> bool {
        self.permissions.owner_read
    }

    /// Check if file is writable
    pub fn is_writable(&self) -> bool {
        self.permissions.owner_write
    }

    /// Check if file is executable
    pub fn is_executable(&self) -> bool {
        self.permissions.owner_exec
    }
}

/// File permission bits (Unix-style)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilePermissions {
    /// Owner read
    pub owner_read: bool,
    /// Owner write
    pub owner_write: bool,
    /// Owner execute
    pub owner_exec: bool,
    /// Group read
    pub group_read: bool,
    /// Group write
    pub group_write: bool,
    /// Group execute
    pub group_exec: bool,
    /// Other read
    pub other_read: bool,
    /// Other write
    pub other_write: bool,
    /// Other execute
    pub other_exec: bool,
}

impl FilePermissions {
    /// Default file permissions (644)
    pub fn file() -> Self {
        Self {
            owner_read: true,
            owner_write: true,
            owner_exec: false,
            group_read: true,
            group_write: false,
            group_exec: false,
            other_read: true,
            other_write: false,
            other_exec: false,
        }
    }

    /// Default executable permissions (755)
    pub fn executable() -> Self {
        Self {
            owner_read: true,
            owner_write: true,
            owner_exec: true,
            group_read: true,
            group_write: false,
            group_exec: true,
            other_read: true,
            other_write: false,
            other_exec: true,
        }
    }

    /// Read-only permissions (444)
    pub fn readonly() -> Self {
        Self {
            owner_read: true,
            owner_write: false,
            owner_exec: false,
            group_read: true,
            group_write: false,
            group_exec: false,
            other_read: true,
            other_write: false,
            other_exec: false,
        }
    }

    /// Convert from octal notation (e.g., 0o755)
    pub fn from_octal(mode: u32) -> Self {
        let owner = (mode >> 6) & 7;
        let group = (mode >> 3) & 7;
        let other = mode & 7;

        Self {
            owner_read: owner & 4 != 0,
            owner_write: owner & 2 != 0,
            owner_exec: owner & 1 != 0,
            group_read: group & 4 != 0,
            group_write: group & 2 != 0,
            group_exec: group & 1 != 0,
            other_read: other & 4 != 0,
            other_write: other & 2 != 0,
            other_exec: other & 1 != 0,
        }
    }

    /// Convert to octal notation
    pub fn to_octal(&self) -> u32 {
        let mut mode = 0u32;
        if self.owner_read {
            mode |= 4 << 6;
        }
        if self.owner_write {
            mode |= 2 << 6;
        }
        if self.owner_exec {
            mode |= 1 << 6;
        }
        if self.group_read {
            mode |= 4 << 3;
        }
        if self.group_write {
            mode |= 2 << 3;
        }
        if self.group_exec {
            mode |= 1 << 3;
        }
        if self.other_read {
            mode |= 4;
        }
        if self.other_write {
            mode |= 2;
        }
        if self.other_exec {
            mode |= 1;
        }
        mode
    }

    /// Convert to string representation (e.g., "rwxr-xr-x")
    pub fn to_string(&self) -> String {
        let mut s = String::with_capacity(9);
        s.push(if self.owner_read { 'r' } else { '-' });
        s.push(if self.owner_write { 'w' } else { '-' });
        s.push(if self.owner_exec { 'x' } else { '-' });
        s.push(if self.group_read { 'r' } else { '-' });
        s.push(if self.group_write { 'w' } else { '-' });
        s.push(if self.group_exec { 'x' } else { '-' });
        s.push(if self.other_read { 'r' } else { '-' });
        s.push(if self.other_write { 'w' } else { '-' });
        s.push(if self.other_exec { 'x' } else { '-' });
        s
    }
}

impl Default for FilePermissions {
    fn default() -> Self {
        Self::file()
    }
}

impl From<u32> for FilePermissions {
    fn from(mode: u32) -> Self {
        Self::from_octal(mode)
    }
}

impl From<FilePermissions> for u32 {
    fn from(perms: FilePermissions) -> Self {
        perms.to_octal()
    }
}

/// Filesystem operations
#[derive(Debug, Clone)]
pub enum FsOperation {
    /// Write file content
    WriteFile { path: String, content: Vec<u8> },

    /// Copy file
    CopyFile { src: String, dst: String },

    /// Copy directory recursively
    CopyDir { src: String, dst: String },

    /// Move file
    MoveFile { src: String, dst: String },

    /// Move directory recursively
    MoveDir { src: String, dst: String },

    /// Delete file
    DeleteFile { path: String },

    /// Delete directory recursively
    DeleteDir { path: String },

    /// Change file permissions
    Chmod {
        path: String,
        permissions: FilePermissions,
        recursive: bool,
    },

    /// Make file executable
    MakeExecutable { path: String },

    /// Create symbolic link
    Symlink {
        link_path: String,
        target_path: String,
    },
}

impl FsOperation {
    /// Get human-readable description of operation
    pub fn describe(&self) -> String {
        match self {
            FsOperation::WriteFile { path, .. } => format!("Write file: {}", path),
            FsOperation::CopyFile { src, dst } => format!("Copy file: {} -> {}", src, dst),
            FsOperation::CopyDir { src, dst } => format!("Copy directory: {} -> {}", src, dst),
            FsOperation::MoveFile { src, dst } => format!("Move file: {} -> {}", src, dst),
            FsOperation::MoveDir { src, dst } => format!("Move directory: {} -> {}", src, dst),
            FsOperation::DeleteFile { path } => format!("Delete file: {}", path),
            FsOperation::DeleteDir { path } => format!("Delete directory: {}", path),
            FsOperation::Chmod {
                path,
                permissions,
                recursive,
            } => {
                if *recursive {
                    format!("Chmod {} (recursive): {}", path, permissions.to_string())
                } else {
                    format!("Chmod {}: {}", path, permissions.to_string())
                }
            }
            FsOperation::MakeExecutable { path } => format!("Make executable: {}", path),
            FsOperation::Symlink {
                link_path,
                target_path,
            } => {
                format!("Create symlink: {} -> {}", link_path, target_path)
            }
        }
    }
}

/// Find operation results
#[derive(Debug, Clone)]
pub struct FindResults {
    /// Matched file paths
    pub files: Vec<String>,

    /// Total count of matches
    pub count: usize,

    /// Directories traversed
    pub dirs_traversed: usize,
}

impl FindResults {
    /// Create new empty results
    pub fn new() -> Self {
        Self {
            files: Vec::new(),
            count: 0,
            dirs_traversed: 0,
        }
    }

    /// Filter results to only directories
    pub fn dirs_only(self) -> Vec<String> {
        self.files
            .into_iter()
            .filter(|p| p.ends_with('/'))
            .collect()
    }

    /// Filter results to only files
    pub fn files_only(self) -> Vec<String> {
        self.files
            .into_iter()
            .filter(|p| !p.ends_with('/'))
            .collect()
    }
}

impl Default for FindResults {
    fn default() -> Self {
        Self::new()
    }
}

/// Directory listing entry
#[derive(Debug, Clone)]
pub struct DirectoryEntry {
    /// Entry name (not full path)
    pub name: String,

    /// Whether this is a directory
    pub is_dir: bool,

    /// File size (0 for directories)
    pub size: u64,

    /// File permissions
    pub permissions: FilePermissions,

    /// Last modified time
    pub modified: i64,
}

/// Batch operation summary
#[derive(Debug, Clone)]
pub struct OperationSummary {
    /// All operations to be performed
    pub operations: Vec<FsOperation>,

    /// Total files affected
    pub files_affected: usize,

    /// Total directories affected
    pub dirs_affected: usize,

    /// Estimated bytes changed
    pub bytes_changed: u64,
}

impl OperationSummary {
    /// Create new summary
    pub fn new() -> Self {
        Self {
            operations: Vec::new(),
            files_affected: 0,
            dirs_affected: 0,
            bytes_changed: 0,
        }
    }

    /// Add operation to summary
    pub fn add_operation(&mut self, op: FsOperation, bytes: u64) {
        match &op {
            FsOperation::WriteFile { .. } | FsOperation::CopyFile { .. } => {
                self.files_affected += 1;
            }
            FsOperation::CopyDir { .. } => {
                self.dirs_affected += 1;
            }
            FsOperation::DeleteFile { .. } | FsOperation::DeleteDir { .. } => {
                self.files_affected += 1;
            }
            _ => {}
        }
        self.bytes_changed += bytes;
        self.operations.push(op);
    }
}

impl Default for OperationSummary {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_permissions_octal() {
        let perms = FilePermissions::from_octal(0o755);
        assert!(perms.owner_read);
        assert!(perms.owner_write);
        assert!(perms.owner_exec);
        assert_eq!(perms.to_octal(), 0o755);
    }

    #[test]
    fn test_permissions_string() {
        let perms = FilePermissions::executable();
        assert_eq!(perms.to_string(), "rwxr-xr-x");
    }

    #[test]
    fn test_operation_describe() {
        let op = FsOperation::WriteFile {
            path: "/tmp/test.txt".to_string(),
            content: vec![],
        };
        assert_eq!(op.describe(), "Write file: /tmp/test.txt");
    }

    #[test]
    fn test_find_results() {
        let results1 = FindResults {
            files: vec!["file.txt".to_string(), "dir/".to_string()],
            count: 2,
            dirs_traversed: 1,
        };
        let results2 = FindResults {
            files: vec!["file.txt".to_string(), "dir/".to_string()],
            count: 2,
            dirs_traversed: 1,
        };
        assert_eq!(results1.files_only().len(), 1);
        assert_eq!(results2.dirs_only().len(), 1);
    }
}