refactor: re-use common types

This commit is contained in:
Nurul Huda (Apon) 2026-01-26 13:19:52 +06:00
parent 8b925553d4
commit acd7ed05cb
No known key found for this signature in database
GPG key ID: 5D3F1DE2855A2F79
3 changed files with 107 additions and 141 deletions

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const abi = @import("abi.zig"); const abi = @import("abi.zig");
const c = abi.c; const c = abi.c;
const temporal = @import("temporal.zig");
const Duration = @This(); const Duration = @This();
@ -108,7 +109,7 @@ pub fn nanoseconds(self: Duration) f64 {
/// Get the sign of the duration: positive (1), zero (0), or negative (-1). /// Get the sign of the duration: positive (1), zero (0), or negative (-1).
pub fn sign(self: Duration) Sign { pub fn sign(self: Duration) Sign {
return @as(Sign, @enumFromInt(c.temporal_rs_Duration_sign(self._inner))); return Sign.fromCApi(c.temporal_rs_Duration_sign(self._inner));
} }
/// Check if the duration is zero (all fields are zero). /// Check if the duration is zero (all fields are zero).
@ -162,13 +163,13 @@ fn compareWithProvider(self: Duration, other: Duration, relative_to: RelativeTo,
/// Get the total value of the duration in the specified unit (Temporal.Duration.prototype.total). /// Get the total value of the duration in the specified unit (Temporal.Duration.prototype.total).
pub fn total(self: Duration, unit: Unit, relative_to: RelativeTo) !f64 { pub fn total(self: Duration, unit: Unit, relative_to: RelativeTo) !f64 {
const res = c.temporal_rs_Duration_total(self._inner, @intFromEnum(unit), relative_to); const res = c.temporal_rs_Duration_total(self._inner, unit.toCApi(), relative_to);
return abi.success(res) orelse return error.TemporalError; return abi.success(res) orelse return error.TemporalError;
} }
/// Get the total value of the duration with an explicit provider. /// Get the total value of the duration with an explicit provider.
fn totalWithProvider(self: Duration, unit: Unit, relative_to: RelativeTo, provider: *const c.Provider) !f64 { fn totalWithProvider(self: Duration, unit: Unit, relative_to: RelativeTo, provider: *const c.Provider) !f64 {
const res = c.temporal_rs_Duration_total_with_provider(self._inner, @intFromEnum(unit), relative_to, provider); const res = c.temporal_rs_Duration_total_with_provider(self._inner, unit.toCApi(), relative_to, provider);
return abi.success(res) orelse return error.TemporalError; return abi.success(res) orelse return error.TemporalError;
} }
@ -252,70 +253,13 @@ const Precision = c.Precision;
const Unit_option = c.Unit_option; const Unit_option = c.Unit_option;
const RoundingMode_option = c.RoundingMode_option; const RoundingMode_option = c.RoundingMode_option;
/// Time unit for Temporal operations.
/// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal
pub const Unit = enum {
auto,
nanosecond,
microsecond,
millisecond,
second,
minute,
hour,
day,
week,
month,
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.
/// See: https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers
pub const RoundingMode = enum(c_uint) {
/// Round toward positive infinity
ceil = c.RoundingMode_Ceil,
/// Round toward negative infinity
floor = c.RoundingMode_Floor,
/// Round away from zero
expand = c.RoundingMode_Expand,
/// Round toward zero (truncate)
trunc = c.RoundingMode_Trunc,
/// Round half toward positive infinity
half_ceil = c.RoundingMode_HalfCeil,
/// Round half toward negative infinity
half_floor = c.RoundingMode_HalfFloor,
/// Round half away from zero
half_expand = c.RoundingMode_HalfExpand,
/// Round half toward zero
half_trunc = c.RoundingMode_HalfTrunc,
/// Round half to even (banker's rounding)
half_even = c.RoundingMode_HalfEven,
};
pub const Sign = enum(c_int) {
positive = c.Sign_Positive,
zero = c.Sign_Zero,
negative = c.Sign_Negative,
};
pub const RoundingOptions = c.RoundingOptions; pub const RoundingOptions = c.RoundingOptions;
pub const ToStringRoundingOptions = c.ToStringRoundingOptions; pub const ToStringRoundingOptions = c.ToStringRoundingOptions;
pub const Unit = temporal.Unit;
pub const RoundingMode = temporal.RoundingMode;
pub const Sign = temporal.Sign;
// --- Tests ------------------------------------------------------------------- // --- Tests -------------------------------------------------------------------
test init { test init {

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const abi = @import("abi.zig"); const abi = @import("abi.zig");
const c = abi.c; const c = abi.c;
const temporal = @import("temporal.zig");
const Instant = @This(); const Instant = @This();
@ -8,83 +9,9 @@ _inner: *c.Instant,
epoch_milliseconds: i64, epoch_milliseconds: i64,
epoch_nanoseconds: i128, epoch_nanoseconds: i128,
pub const Unit = enum { pub const Unit = temporal.Unit;
auto, pub const RoundingMode = temporal.RoundingMode;
nanosecond, pub const Sign = temporal.Sign;
microsecond,
millisecond,
second,
minute,
hour,
day,
week,
month,
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 {
ceil,
floor,
expand,
trunc,
half_ceil,
half_floor,
half_expand,
half_trunc,
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 {
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;
const Precision = c.Precision; const Precision = c.Precision;

95
src/temporal.zig Normal file
View file

@ -0,0 +1,95 @@
const abi = @import("abi.zig");
const c = abi.c;
/// Time unit for Temporal operations.
/// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal
pub const Unit = enum {
auto,
nanosecond,
microsecond,
millisecond,
second,
minute,
hour,
day,
week,
month,
year,
pub 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.
/// See: https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers
pub const RoundingMode = enum {
/// Round toward positive infinity
ceil,
/// Round toward negative infinity
floor,
/// Round away from zero
expand,
/// Round toward zero (truncate)
trunc,
/// Round half toward positive infinity
half_ceil,
/// Round half toward negative infinity
half_floor,
/// Round half away from zero
half_expand,
/// Round half toward zero
half_trunc,
/// Round half to even (banker's rounding)
half_even,
pub 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,
};
}
};
/// Sign of a duration or time value.
pub const Sign = enum {
positive,
zero,
negative,
pub fn toCApi(self: Sign) c_int {
return switch (self) {
.positive => c.Sign_Positive,
.zero => c.Sign_Zero,
.negative => c.Sign_Negative,
};
}
pub fn fromCApi(value: c_int) Sign {
return switch (value) {
c.Sign_Positive => .positive,
c.Sign_Zero => .zero,
c.Sign_Negative => .negative,
else => .zero,
};
}
};