aboutsummaryrefslogtreecommitdiff
path: root/src/stream/root.zig
blob: 11580acc40564a6611763fe2350f528a8be2af66 (plain)
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
const std = @import("std");

pub const Stream = union(enum) {
    const Self = @This();

    pub const File = @import("file.zig");
    pub const Buffer = @import("buffer.zig");

    file: File,
    buffer: Buffer,

    pub fn from_file(file: std.fs.File) Self {
        return .{ .file = File.init(file) };
    }

    pub fn from_buffer(buffer: []const u8) Self {
        return .{ .buffer = Buffer.init(buffer) };
    }

    pub fn reader(self: *Self, buffer: []u8) *std.Io.Reader {
        return switch(self.*) {
            .file => |*f| f.reader(buffer),
            .buffer => |*b| b.reader(buffer),
        };
    }

    pub fn close(self: *Self) void {
        return switch(self.*) {
            .file => |*f| f.close(),
            .buffer => |*b| b.close(),
        };
    }
};