refactor: wrap c enums
This commit is contained in:
parent
13d1783b9f
commit
79725fbc66
3 changed files with 116 additions and 52 deletions
|
|
@ -12,5 +12,6 @@ pub fn main() !void {
|
||||||
std.debug.print("Instant.toString(): {s}\n", .{instant_str});
|
std.debug.print("Instant.toString(): {s}\n", .{instant_str});
|
||||||
|
|
||||||
const dur = try Temporal.Duration.from("P1Y2M3DT4H5M6S");
|
const dur = try Temporal.Duration.from("P1Y2M3DT4H5M6S");
|
||||||
|
|
||||||
defer dur.deinit();
|
defer dur.deinit();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -254,18 +254,34 @@ const RoundingMode_option = c.RoundingMode_option;
|
||||||
|
|
||||||
/// Time unit for Temporal operations.
|
/// Time unit for Temporal operations.
|
||||||
/// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal
|
/// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal
|
||||||
pub const Unit = enum(c_uint) {
|
pub const Unit = enum {
|
||||||
auto = c.Unit_Auto,
|
auto,
|
||||||
nanosecond = c.Unit_Nanosecond,
|
nanosecond,
|
||||||
microsecond = c.Unit_Microsecond,
|
microsecond,
|
||||||
millisecond = c.Unit_Millisecond,
|
millisecond,
|
||||||
second = c.Unit_Second,
|
second,
|
||||||
minute = c.Unit_Minute,
|
minute,
|
||||||
hour = c.Unit_Hour,
|
hour,
|
||||||
day = c.Unit_Day,
|
day,
|
||||||
week = c.Unit_Week,
|
week,
|
||||||
month = c.Unit_Month,
|
month,
|
||||||
year = c.Unit_Year,
|
year,
|
||||||
|
|
||||||
|
fn toCApi(self: Unit) c_uint {
|
||||||
|
return switch (self) {
|
||||||
|
.auto => c.Unit_Auto,
|
||||||
|
.nanosecond => c.Unit_Nanosecond,
|
||||||
|
.microsecond => c.Unit_Microsecond,
|
||||||
|
.millisecond => c.Unit_Millisecond,
|
||||||
|
.second => c.Unit_Second,
|
||||||
|
.minute => c.Unit_Minute,
|
||||||
|
.hour => c.Unit_Hour,
|
||||||
|
.day => c.Unit_Day,
|
||||||
|
.week => c.Unit_Week,
|
||||||
|
.month => c.Unit_Month,
|
||||||
|
.year => c.Unit_Year,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Rounding mode for Temporal operations.
|
/// Rounding mode for Temporal operations.
|
||||||
|
|
|
||||||
127
src/Instant.zig
127
src/Instant.zig
|
|
@ -8,35 +8,82 @@ _inner: *c.Instant,
|
||||||
epoch_milliseconds: i64,
|
epoch_milliseconds: i64,
|
||||||
epoch_nanoseconds: i128,
|
epoch_nanoseconds: i128,
|
||||||
|
|
||||||
pub const Unit = enum(c_uint) {
|
pub const Unit = enum {
|
||||||
auto = c.Unit_Auto,
|
auto,
|
||||||
nanosecond = c.Unit_Nanosecond,
|
nanosecond,
|
||||||
microsecond = c.Unit_Microsecond,
|
microsecond,
|
||||||
millisecond = c.Unit_Millisecond,
|
millisecond,
|
||||||
second = c.Unit_Second,
|
second,
|
||||||
minute = c.Unit_Minute,
|
minute,
|
||||||
hour = c.Unit_Hour,
|
hour,
|
||||||
day = c.Unit_Day,
|
day,
|
||||||
week = c.Unit_Week,
|
week,
|
||||||
month = c.Unit_Month,
|
month,
|
||||||
year = c.Unit_Year,
|
year,
|
||||||
|
|
||||||
|
fn toCApi(self: Unit) c_uint {
|
||||||
|
return switch (self) {
|
||||||
|
.auto => c.Unit_Auto,
|
||||||
|
.nanosecond => c.Unit_Nanosecond,
|
||||||
|
.microsecond => c.Unit_Microsecond,
|
||||||
|
.millisecond => c.Unit_Millisecond,
|
||||||
|
.second => c.Unit_Second,
|
||||||
|
.minute => c.Unit_Minute,
|
||||||
|
.hour => c.Unit_Hour,
|
||||||
|
.day => c.Unit_Day,
|
||||||
|
.week => c.Unit_Week,
|
||||||
|
.month => c.Unit_Month,
|
||||||
|
.year => c.Unit_Year,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const RoundingMode = enum(c_uint) {
|
pub const RoundingMode = enum {
|
||||||
ceil = c.RoundingMode_Ceil,
|
ceil,
|
||||||
floor = c.RoundingMode_Floor,
|
floor,
|
||||||
expand = c.RoundingMode_Expand,
|
expand,
|
||||||
trunc = c.RoundingMode_Trunc,
|
trunc,
|
||||||
half_ceil = c.RoundingMode_HalfCeil,
|
half_ceil,
|
||||||
half_floor = c.RoundingMode_HalfFloor,
|
half_floor,
|
||||||
half_expand = c.RoundingMode_HalfExpand,
|
half_expand,
|
||||||
half_trunc = c.RoundingMode_HalfTrunc,
|
half_trunc,
|
||||||
half_even = c.RoundingMode_HalfEven,
|
half_even,
|
||||||
|
|
||||||
|
fn toCApi(self: RoundingMode) c_uint {
|
||||||
|
return switch (self) {
|
||||||
|
.ceil => c.RoundingMode_Ceil,
|
||||||
|
.floor => c.RoundingMode_Floor,
|
||||||
|
.expand => c.RoundingMode_Expand,
|
||||||
|
.trunc => c.RoundingMode_Trunc,
|
||||||
|
.half_ceil => c.RoundingMode_HalfCeil,
|
||||||
|
.half_floor => c.RoundingMode_HalfFloor,
|
||||||
|
.half_expand => c.RoundingMode_HalfExpand,
|
||||||
|
.half_trunc => c.RoundingMode_HalfTrunc,
|
||||||
|
.half_even => c.RoundingMode_HalfEven,
|
||||||
};
|
};
|
||||||
pub const Sign = enum(c_int) {
|
}
|
||||||
positive = c.Sign_Positive,
|
};
|
||||||
zero = c.Sign_Zero,
|
pub const Sign = enum {
|
||||||
negative = c.Sign_Negative,
|
positive,
|
||||||
|
zero,
|
||||||
|
negative,
|
||||||
|
|
||||||
|
fn toCApi(self: Sign) c_int {
|
||||||
|
return switch (self) {
|
||||||
|
.positive => c.Sign_Positive,
|
||||||
|
.zero => c.Sign_Zero,
|
||||||
|
.negative => c.Sign_Negative,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fromCApi(value: c_int) Sign {
|
||||||
|
return switch (value) {
|
||||||
|
c.Sign_Positive => .positive,
|
||||||
|
c.Sign_Zero => .zero,
|
||||||
|
c.Sign_Negative => .negative,
|
||||||
|
else => .zero,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const TimeZone = c.TimeZone;
|
const TimeZone = c.TimeZone;
|
||||||
const TimeZone_option = c.TimeZone_option;
|
const TimeZone_option = c.TimeZone_option;
|
||||||
|
|
@ -218,8 +265,8 @@ fn optsToRounding(opts: ToStringOptions) ToStringRoundingOptions {
|
||||||
else
|
else
|
||||||
defaultPrecision();
|
defaultPrecision();
|
||||||
|
|
||||||
const smallest_unit = if (opts.smallest_unit) |unit| @intFromEnum(unit) else null;
|
const smallest_unit = if (opts.smallest_unit) |unit| unit.toCApi() else null;
|
||||||
const rounding_mode = if (opts.rounding_mode) |mode| @intFromEnum(mode) else null;
|
const rounding_mode = if (opts.rounding_mode) |mode| mode.toCApi() else null;
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.precision = precision,
|
.precision = precision,
|
||||||
|
|
@ -370,20 +417,20 @@ test until {
|
||||||
defer later.deinit();
|
defer later.deinit();
|
||||||
|
|
||||||
const settings = DifferenceSettings{
|
const settings = DifferenceSettings{
|
||||||
.largest_unit = abi.toUnitOption(@intFromEnum(Unit.hour)),
|
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
||||||
.smallest_unit = abi.toUnitOption(@intFromEnum(Unit.second)),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(@intFromEnum(RoundingMode.trunc)),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
var until_handle = try earlier.until(later, settings);
|
var until_handle = try earlier.until(later, settings);
|
||||||
defer until_handle.deinit();
|
defer until_handle.deinit();
|
||||||
try std.testing.expectEqual(Sign.positive, @as(Sign, @enumFromInt(abi.c.temporal_rs_Duration_sign(until_handle.ptr))));
|
try std.testing.expectEqual(Sign.positive, Sign.fromCApi(abi.c.temporal_rs_Duration_sign(until_handle.ptr)));
|
||||||
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(until_handle.ptr));
|
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(until_handle.ptr));
|
||||||
|
|
||||||
var since_handle = try later.since(earlier, settings);
|
var since_handle = try later.since(earlier, settings);
|
||||||
defer since_handle.deinit();
|
defer since_handle.deinit();
|
||||||
try std.testing.expectEqual(Sign.positive, @as(Sign, @enumFromInt(abi.c.temporal_rs_Duration_sign(since_handle.ptr))));
|
try std.testing.expectEqual(Sign.positive, Sign.fromCApi(abi.c.temporal_rs_Duration_sign(since_handle.ptr)));
|
||||||
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(since_handle.ptr));
|
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(since_handle.ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -394,20 +441,20 @@ test since {
|
||||||
defer later.deinit();
|
defer later.deinit();
|
||||||
|
|
||||||
const settings = DifferenceSettings{
|
const settings = DifferenceSettings{
|
||||||
.largest_unit = abi.toUnitOption(@intFromEnum(Unit.hour)),
|
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
||||||
.smallest_unit = abi.toUnitOption(@intFromEnum(Unit.second)),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(@intFromEnum(RoundingMode.trunc)),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
var until_handle = try earlier.until(later, settings);
|
var until_handle = try earlier.until(later, settings);
|
||||||
defer until_handle.deinit();
|
defer until_handle.deinit();
|
||||||
try std.testing.expectEqual(Sign.positive, @as(Sign, @enumFromInt(abi.c.temporal_rs_Duration_sign(until_handle.ptr))));
|
try std.testing.expectEqual(Sign.positive, Sign.fromCApi(abi.c.temporal_rs_Duration_sign(until_handle.ptr)));
|
||||||
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(until_handle.ptr));
|
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(until_handle.ptr));
|
||||||
|
|
||||||
var since_handle = try later.since(earlier, settings);
|
var since_handle = try later.since(earlier, settings);
|
||||||
defer since_handle.deinit();
|
defer since_handle.deinit();
|
||||||
try std.testing.expectEqual(Sign.positive, @as(Sign, @enumFromInt(abi.c.temporal_rs_Duration_sign(since_handle.ptr))));
|
try std.testing.expectEqual(Sign.positive, Sign.fromCApi(abi.c.temporal_rs_Duration_sign(since_handle.ptr)));
|
||||||
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(since_handle.ptr));
|
try std.testing.expectEqual(@as(i64, 1), abi.c.temporal_rs_Duration_hours(since_handle.ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -417,8 +464,8 @@ test round {
|
||||||
|
|
||||||
const opts = RoundingOptions{
|
const opts = RoundingOptions{
|
||||||
.largest_unit = abi.toUnitOption(null),
|
.largest_unit = abi.toUnitOption(null),
|
||||||
.smallest_unit = abi.toUnitOption(@intFromEnum(Unit.second)),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(@intFromEnum(RoundingMode.half_expand)),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.half_expand.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue