81 lines
1.7 KiB
Plaintext
81 lines
1.7 KiB
Plaintext
type .{ .Result } = @import("./error.spl");
|
|
|
|
pub type Display = shape {
|
|
fn fmt(@Self self: *@self, out: @[*Sink]): bool;
|
|
};
|
|
|
|
pub type Debug = shape {
|
|
fn debug(@Self self: *@self, out: @[*Sink]): bool;
|
|
};
|
|
|
|
pub type Sink = shape {
|
|
fn write_byte(@Self self: *@self, byte: u8): bool;
|
|
|
|
// TODO shape default value for `fn write_bytes(@Self self: *@self, bytes: []const u8): bool;`
|
|
};
|
|
|
|
pub type Spec = struct {
|
|
align: enum { Left, Center, Right },
|
|
width: usize,
|
|
fill: u8,
|
|
// attrs: enum {
|
|
// number: struct {},
|
|
// }
|
|
};
|
|
|
|
pub type Error = enum {
|
|
WriteFailed,
|
|
UnclosedLeftBrace,
|
|
UnclosedRightBrace,
|
|
UnknownSpec,
|
|
MissingArg,
|
|
TooManyArgs,
|
|
};
|
|
|
|
// not have { and }
|
|
pub fn parse_spec(pattern: []const u8): @Result(Spec, Error) {
|
|
var res = Spec {
|
|
.fill = ' ',
|
|
.width = 0,
|
|
.align = .Left,
|
|
};
|
|
ret .Ok(res);
|
|
}
|
|
|
|
pub comptime fn Format(S: Sink): type {
|
|
ret struct {
|
|
out: S,
|
|
|
|
pub fn with_sink(out: S): @self {
|
|
ret .{ .out = out };
|
|
}
|
|
|
|
fn write(@Self self: *@self, str: []const u8): bool {
|
|
var res = true;
|
|
for i in str {
|
|
res = self.out.write_byte(i);
|
|
if (res == false) {
|
|
ret false;
|
|
}
|
|
}
|
|
ret res;
|
|
}
|
|
|
|
pub fn format(@Self self: *@self, pattern: []const u8, args: []const DisplayArg): Error {
|
|
var starti = 0;
|
|
for i in 0..pattern.len() {
|
|
var ch = pattern[i];
|
|
match pattern[i] {
|
|
'{' => {
|
|
|
|
},
|
|
'}' => {
|
|
|
|
},
|
|
_ => { continue; },
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|