mirror of
https://github.com/jetzig-framework/jetcommon.git
synced 2026-09-01 22:53:18 -06:00
commit
4b985fce26
@ -4,8 +4,8 @@
|
||||
.version = "0.1.0",
|
||||
.dependencies = .{
|
||||
.zul = .{
|
||||
.url = "https://github.com/karlseguin/zul/archive/e770047f208cf4538fcd9fc64550c84fd5492fc4.tar.gz",
|
||||
.hash = "zul-0.0.0-1oDot9yRBwDA_ovd6GC1M_ViW3LarywMaGrH6vcuEjqv",
|
||||
.url = "https://github.com/karlseguin/zul/archive/bf04b1e725ce57bad6c4cfa0d29e405f86df3015.tar.gz",
|
||||
.hash = "zul-0.0.0-1oDot6-iBwAqlLUK89028p23CMbVwv7z2cIVAwG4bMM9",
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
@ -9,13 +9,13 @@ const Ast = std.zig.Ast;
|
||||
/// to stderr with error information at the lines that failed. If present, `message` is printed
|
||||
/// at the end of the error output. Caller owns allocated slice.
|
||||
pub fn zig(
|
||||
io: std.Io,
|
||||
allocator: Allocator,
|
||||
input: []const u8,
|
||||
message: ?[]const u8,
|
||||
) ![]const u8 {
|
||||
var arena: ArenaAllocator = .init(allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const alloc = arena.allocator();
|
||||
|
||||
var fixups: Ast.Render.Fixups = .{};
|
||||
@ -27,9 +27,13 @@ pub fn zig(
|
||||
.zig,
|
||||
);
|
||||
if (ast.errors.len > 0) {
|
||||
const tty = std.Io.tty.detectConfig(std.fs.File.stderr());
|
||||
var stderr_fw = std.fs.File.stderr().writer(&.{});
|
||||
var write_buf: [256]u8 = undefined;
|
||||
var stderr_fw = std.Io.File.stderr().writer(io, &write_buf);
|
||||
const writer = &stderr_fw.interface;
|
||||
const tty: std.Io.Terminal = .{
|
||||
.writer = writer,
|
||||
.mode = try .detect(io, std.Io.File.stderr(), false, false),
|
||||
};
|
||||
|
||||
var it = std.mem.tokenizeScalar(u8, input, '\n');
|
||||
var line_number: usize = 1;
|
||||
@ -37,12 +41,11 @@ pub fn zig(
|
||||
const maybe_err = for (ast.errors) |err| {
|
||||
if (ast.tokenLocation(0, err.token).line == line_number + 1) break err;
|
||||
} else null;
|
||||
try tty.setColor(writer, if (maybe_err != null) .red else .cyan);
|
||||
try tty.setColor(if (maybe_err != null) .red else .cyan);
|
||||
const error_message = if (maybe_err) |err| blk: {
|
||||
var buf_writer: Writer.Allocating = .init(alloc);
|
||||
var err_writer = buf_writer.writer;
|
||||
try err_writer.writeAll(" // ");
|
||||
try ast.renderError(err, &err_writer);
|
||||
try buf_writer.writer.writeAll(" // ");
|
||||
try ast.renderError(err, &buf_writer.writer);
|
||||
break :blk try buf_writer.toOwnedSlice();
|
||||
} else "";
|
||||
try writer.print("{: <4} {s}{s}\n", .{
|
||||
@ -52,17 +55,18 @@ pub fn zig(
|
||||
});
|
||||
}
|
||||
if (message) |msg| {
|
||||
try tty.setColor(writer, .yellow);
|
||||
try tty.setColor(.yellow);
|
||||
try writer.print("\n{s}\n", .{msg});
|
||||
}
|
||||
try tty.setColor(writer, .reset);
|
||||
try tty.setColor(.reset);
|
||||
return error.JetCommonInvalidZigCode;
|
||||
}
|
||||
|
||||
var aw: Writer.Allocating = .init(alloc);
|
||||
defer aw.deinit();
|
||||
|
||||
try ast.render(allocator, &aw.writer, fixups);
|
||||
|
||||
return aw.toOwnedSlice();
|
||||
try ast.render(alloc, &aw.writer, fixups);
|
||||
const output = try aw.toOwnedSlice();
|
||||
defer alloc.free(output);
|
||||
return allocator.dupe(u8, output);
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zul = @import("zul");
|
||||
|
||||
pub const DateTime = @import("time/DateTime.zig");
|
||||
pub const Date = @import("time/Date.zig");
|
||||
pub const Time = @import("time/Time.zig");
|
||||
pub const TimestampPrecision = enum { seconds, milliseconds, microseconds };
|
||||
pub const TimestampPrecision = zul.DateTime.TimestampPrecision;
|
||||
|
||||
@ -2,23 +2,18 @@ const std = @import("std");
|
||||
const Writer = std.Io.Writer;
|
||||
|
||||
const zul = @import("zul");
|
||||
const Format = zul.Date.Format;
|
||||
|
||||
zul_date: zul.Date,
|
||||
|
||||
const Date = @This();
|
||||
|
||||
const Format = enum { rfc3339, iso8601 };
|
||||
|
||||
pub fn init(year: i16, month: u8, day: u8) !Date {
|
||||
const zul_date = try zul.Date.init(year, month, day);
|
||||
return .{ .zul_date = zul_date };
|
||||
return .{ .zul_date = try .init(year, month, day) };
|
||||
}
|
||||
|
||||
pub fn parse(input: []const u8, fmt: Format) !Date {
|
||||
return switch (fmt) {
|
||||
.rfc3339 => .{ .zul_date = try zul.Date.parse(input, .rfc3339) },
|
||||
.iso8601 => .{ .zul_date = try zul.Date.parse(input, .iso8601) },
|
||||
};
|
||||
return .{ .zul_date = try .parse(input, fmt) };
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
|
||||
@ -2,28 +2,24 @@ const std = @import("std");
|
||||
const Writer = std.Io.Writer;
|
||||
|
||||
const zul = @import("zul");
|
||||
const TimestampPrecision = zul.DateTime.TimestampPrecision;
|
||||
|
||||
const Time = @import("../time.zig").Time;
|
||||
const Date = @import("../time.zig").Date;
|
||||
const TimestampPrecision = @import("../time.zig").TimestampPrecision;
|
||||
const Operator = @import("../../jetcommon.zig").Operator;
|
||||
|
||||
const DateTime = @This();
|
||||
pub const DateTimeFormat = enum { rfc3339, iso8601 };
|
||||
// pub const DateTimeFormat = enum { rfc3339, iso8601 };
|
||||
pub const DateTimeFormat = zul.DateTime.Format;
|
||||
|
||||
zul_datetime: zul.DateTime,
|
||||
|
||||
pub fn fromUTC(year_: i16, month_: u8, day_: u8, hour_: u8, min: u8, sec: u8, micros: u32) !DateTime {
|
||||
return .{ .zul_datetime = try zul.DateTime.initUTC(year_, month_, day_, hour_, min, sec, micros) };
|
||||
return .{ .zul_datetime = try .initUTC(year_, month_, day_, hour_, min, sec, micros) };
|
||||
}
|
||||
|
||||
pub fn fromUnix(value: i64, precision: TimestampPrecision) !DateTime {
|
||||
const zul_datetime = switch (precision) {
|
||||
.seconds => try zul.DateTime.fromUnix(value, .seconds),
|
||||
.milliseconds => try zul.DateTime.fromUnix(value, .milliseconds),
|
||||
.microseconds => try zul.DateTime.fromUnix(value, .microseconds),
|
||||
};
|
||||
return .{ .zul_datetime = zul_datetime };
|
||||
return .{ .zul_datetime = try .fromUnix(value, precision) };
|
||||
}
|
||||
|
||||
pub fn now() DateTime {
|
||||
@ -31,8 +27,7 @@ pub fn now() DateTime {
|
||||
}
|
||||
|
||||
pub fn parse(input: []const u8) !DateTime {
|
||||
const zul_datetime = try zul.DateTime.parse(input, .rfc3339);
|
||||
return .{ .zul_datetime = zul_datetime };
|
||||
return .{ .zul_datetime = try .parse(input, .rfc3339) };
|
||||
}
|
||||
|
||||
pub fn date(self: DateTime) Date {
|
||||
@ -44,28 +39,17 @@ pub fn time(self: DateTime) Time {
|
||||
}
|
||||
|
||||
pub fn unix(self: DateTime, precision: TimestampPrecision) i64 {
|
||||
return switch (precision) {
|
||||
.seconds => self.zul_datetime.unix(.seconds),
|
||||
.milliseconds => self.zul_datetime.unix(.milliseconds),
|
||||
.microseconds => self.zul_datetime.unix(.microseconds),
|
||||
};
|
||||
return self.zul_datetime.unix(precision);
|
||||
}
|
||||
|
||||
pub fn compare(self: DateTime, comptime operator: Operator, other: DateTime) bool {
|
||||
const cmp = self.zul_datetime.order(other.zul_datetime);
|
||||
return switch (cmp) {
|
||||
.eq => switch (operator) {
|
||||
.equal, .less_or_equal, .greater_or_equal => true,
|
||||
else => false,
|
||||
},
|
||||
.lt => switch (operator) {
|
||||
.less_or_equal, .less_than => true,
|
||||
else => false,
|
||||
},
|
||||
.gt => switch (operator) {
|
||||
.greater_or_equal, .greater_than => true,
|
||||
else => false,
|
||||
},
|
||||
return switch (operator) {
|
||||
.equal => cmp == .eq,
|
||||
.less_than => cmp == .lt,
|
||||
.greater_than => cmp == .gt,
|
||||
.less_or_equal => cmp == .eq or cmp == .lt,
|
||||
.greater_or_equal => cmp == .eq or cmp == .gt,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -1,22 +1,16 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zul = @import("zul");
|
||||
pub const Format = zul.Time.Format;
|
||||
|
||||
zul_time: zul.Time,
|
||||
|
||||
const Time = @This();
|
||||
|
||||
pub const Format = enum {
|
||||
rfc3339,
|
||||
};
|
||||
|
||||
pub fn init(hour: u8, min: u8, sec: u8, micros: u32) !Time {
|
||||
const zul_time = try zul.Time.init(hour, min, sec, micros);
|
||||
return .{ .zul_time = zul_time };
|
||||
return .{ .zul_time = try .init(hour, min, sec, micros) };
|
||||
}
|
||||
|
||||
pub fn parse(input: []const u8, fmt: Format) !Time {
|
||||
return switch (fmt) {
|
||||
.rfc3339 => .{ .zul_time = try zul.Time.parse(input, .rfc3339) },
|
||||
};
|
||||
return .{ .zul_time = try .parse(input, fmt) };
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user