diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..170eb90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +./zig-out/ +./.zig-cache/ diff --git a/src/classify.zig b/src/classify.zig new file mode 100644 index 0000000..6208b8b --- /dev/null +++ b/src/classify.zig @@ -0,0 +1,20 @@ +const std = @import("std"); + +pub const Category = enum { image, document, music, video, code, other }; + +const DocumentTypes = [_][]const u8{ ".pdf", ".docx", ".odt" }; + +pub fn classify(name: []const u8) Category { + const extension = std.fs.path.extension(name); + + for (DocumentTypes) |ext| { + const eicDocument = std.ascii.eqlIgnoreCase(extension, ext); + if (eicDocument) { + std.debug.print("document found! \n", .{}); + return .document; + } + } + + std.debug.print("no document found! \n", .{}); + return .other; +} diff --git a/src/main.zig b/src/main.zig index a5e7733..6beda91 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,71 +1,17 @@ const std = @import("std"); -const Io = std.Io; +const organize = @import("organize.zig"); -const file_organizer = @import("file_organizer"); +pub fn main() !void { + var threaded = std.Io.Threaded.init(std.heap.page_allocator, .{}); + defer threaded.deinit(); -pub fn main(init: std.process.Init) !void { - // Prints to stderr, unbuffered, ignoring potential errors. - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + const io = threaded.io(); + const cwd = std.Io.Dir.cwd(); + const dir = try cwd.openDir(io, ".", .{ .iterate = true }); + defer dir.close(io); - // This is appropriate for anything that lives as long as the process. - const arena: std.mem.Allocator = init.arena.allocator(); - - // Accessing command line arguments: - const args = try init.minimal.args.toSlice(arena); - for (args) |arg| { - std.log.info("arg: {s}", .{arg}); + var it = dir.iterate(); + while (try it.next(io)) |entry| { + try organize.organize(io, cwd, entry.name); } - - // In order to do I/O operations need an `Io` instance. - const io = init.io; - - // Stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - var stdout_buffer: [1024]u8 = undefined; - var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer); - const stdout_writer = &stdout_file_writer.interface; - - try file_organizer.printAnotherMessage(stdout_writer); - - try stdout_writer.flush(); // Don't forget to flush! -} - -test "simple test" { - const gpa = std.testing.allocator; - var list: std.ArrayList(i32) = .empty; - defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak! - try list.append(gpa, 42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); -} - -test "fuzz example" { - try std.testing.fuzz({}, testOne, .{}); -} - -fn testOne(context: void, smith: *std.testing.Smith) !void { - _ = context; - // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! - - const gpa = std.testing.allocator; - var list: std.ArrayList(u8) = .empty; - defer list.deinit(gpa); - while (!smith.eos()) switch (smith.value(enum { add_data, dup_data })) { - .add_data => { - const slice = try list.addManyAsSlice(gpa, smith.value(u4)); - smith.bytes(slice); - }, - .dup_data => { - if (list.items.len == 0) continue; - if (list.items.len > std.math.maxInt(u32)) return error.SkipZigTest; - const len = smith.valueRangeAtMost(u32, 1, @min(32, list.items.len)); - const off = smith.valueRangeAtMost(u32, 0, @intCast(list.items.len - len)); - try list.appendSlice(gpa, list.items[off..][0..len]); - try std.testing.expectEqualSlices( - u8, - list.items[off..][0..len], - list.items[list.items.len - len ..], - ); - }, - }; } diff --git a/src/organize.zig b/src/organize.zig new file mode 100644 index 0000000..ab4f5e7 --- /dev/null +++ b/src/organize.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const classify = @import("classify.zig"); + +pub fn organize(io: std.Io, dir: std.Io.Dir, name: []const u8) !void { + const category = classify.classify(name); + try ensureCategoryDir(io, dir, category); +} + +fn ensureCategoryDir(io: std.Io, dir: std.Io.Dir, category: classify.Category) !void { + const dest = switch (category) { + .image => "Pictures", + .document => "Documents", + .music => "Music", + .video => "Videos", + .code => "Code", + .other => return, + }; + + try dir.createDirPath(io, dest); +} diff --git a/src/root.zig b/src/root.zig index 5a71250..e69de29 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,18 +0,0 @@ -//! By convention, root.zig is the root source file when making a package. -const std = @import("std"); -const Io = std.Io; - -/// This is a documentation comment to explain the `printAnotherMessage` function below. -/// -/// Accepting an `Io.Writer` instance is a handy way to write reusable code. -pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void { - try writer.print("Run `zig build test` to run the tests.\n", .{}); -} - -pub fn add(a: i32, b: i32) i32 { - return a + b; -} - -test "basic add functionality" { - try std.testing.expect(add(3, 7) == 10); -}