chore: move all binding code to abi

This commit is contained in:
Nurul Huda (Apon) 2026-01-30 02:56:20 +06:00
parent f4f7c35a23
commit 424b437a33
No known key found for this signature in database
GPG key ID: 5D3F1DE2855A2F79
2 changed files with 59 additions and 48 deletions

View file

@ -29,9 +29,7 @@ pub const TimeZone = struct {
return .{ ._inner = tz }; return .{ ._inner = tz };
} }
fn toCApi(self: TimeZone) abi.c.TimeZone { // ...existing code...
return self._inner;
}
}; };
pub const Disambiguation = enum { pub const Disambiguation = enum {
@ -40,14 +38,7 @@ pub const Disambiguation = enum {
later, later,
reject, reject,
fn toCApi(self: Disambiguation) abi.c.Disambiguation { // ...existing code...
return switch (self) {
.compatible => abi.c.Disambiguation_Compatible,
.earlier => abi.c.Disambiguation_Earlier,
.later => abi.c.Disambiguation_Later,
.reject => abi.c.Disambiguation_Reject,
};
}
}; };
pub const OffsetDisambiguation = enum { pub const OffsetDisambiguation = enum {
@ -56,14 +47,7 @@ pub const OffsetDisambiguation = enum {
ignore_offset, ignore_offset,
reject, reject,
fn toCApi(self: OffsetDisambiguation) abi.c.OffsetDisambiguation { // ...existing code...
return switch (self) {
.use_offset => abi.c.OffsetDisambiguation_Use,
.prefer_offset => abi.c.OffsetDisambiguation_Prefer,
.ignore_offset => abi.c.OffsetDisambiguation_Ignore,
.reject => abi.c.OffsetDisambiguation_Reject,
};
}
}; };
pub const CalendarDisplay = enum { pub const CalendarDisplay = enum {
@ -72,26 +56,14 @@ pub const CalendarDisplay = enum {
never, never,
critical, critical,
fn toCApi(self: CalendarDisplay) abi.c.DisplayCalendar { // ...existing code...
return switch (self) {
.auto => abi.c.DisplayCalendar_Auto,
.always => abi.c.DisplayCalendar_Always,
.never => abi.c.DisplayCalendar_Never,
.critical => abi.c.DisplayCalendar_Critical,
};
}
}; };
pub const DisplayOffset = enum { pub const DisplayOffset = enum {
auto, auto,
never, never,
fn toCApi(self: DisplayOffset) abi.c.DisplayOffset { // ...existing code...
return switch (self) {
.auto => abi.c.DisplayOffset_Auto,
.never => abi.c.DisplayOffset_Never,
};
}
}; };
pub const DisplayTimeZone = enum { pub const DisplayTimeZone = enum {
@ -99,13 +71,7 @@ pub const DisplayTimeZone = enum {
never, never,
critical, critical,
fn toCApi(self: DisplayTimeZone) abi.c.DisplayTimeZone { // ...existing code...
return switch (self) {
.auto => abi.c.DisplayTimeZone_Auto,
.never => abi.c.DisplayTimeZone_Never,
.critical => abi.c.DisplayTimeZone_Critical,
};
}
}; };
pub const ToStringOptions = struct { pub const ToStringOptions = struct {
@ -126,25 +92,25 @@ fn wrapZonedDateTime(result: anytype) !ZonedDateTime {
/// Create a ZonedDateTime from epoch nanoseconds /// Create a ZonedDateTime from epoch nanoseconds
pub fn init(epoch_ns: i128, time_zone: TimeZone) !ZonedDateTime { pub fn init(epoch_ns: i128, time_zone: TimeZone) !ZonedDateTime {
const ns_parts = abi.toI128Nanoseconds(epoch_ns); const ns_parts = abi.toI128Nanoseconds(epoch_ns);
return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_nanoseconds(ns_parts, time_zone.toCApi())); return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_nanoseconds(ns_parts, abi.to.toTimeZone(time_zone)));
} }
/// Create from epoch milliseconds /// Create from epoch milliseconds
pub fn fromEpochMilliseconds(epoch_ms: i64, time_zone: TimeZone) !ZonedDateTime { pub fn fromEpochMilliseconds(epoch_ms: i64, time_zone: TimeZone) !ZonedDateTime {
return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_milliseconds(epoch_ms, time_zone.toCApi())); return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_milliseconds(epoch_ms, abi.to.toTimeZone(time_zone)));
} }
/// Create from epoch nanoseconds /// Create from epoch nanoseconds
pub fn fromEpochNanoseconds(epoch_ns: i128, time_zone: TimeZone) !ZonedDateTime { pub fn fromEpochNanoseconds(epoch_ns: i128, time_zone: TimeZone) !ZonedDateTime {
const ns_parts = abi.toI128Nanoseconds(epoch_ns); const ns_parts = abi.toI128Nanoseconds(epoch_ns);
return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_nanoseconds(ns_parts, time_zone.toCApi())); return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_epoch_nanoseconds(ns_parts, abi.to.toTimeZone(time_zone)));
} }
/// Parse from string /// Parse from string
pub fn from(s: []const u8, time_zone: ?TimeZone, disambiguation: Disambiguation, offset_disambiguation: OffsetDisambiguation) !ZonedDateTime { pub fn from(s: []const u8, time_zone: ?TimeZone, disambiguation: Disambiguation, offset_disambiguation: OffsetDisambiguation) !ZonedDateTime {
_ = time_zone; // The time zone is parsed from the string _ = time_zone; // The time zone is parsed from the string
const view = abi.toDiplomatStringView(s); const view = abi.toDiplomatStringView(s);
return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_utf8(view, disambiguation.toCApi(), offset_disambiguation.toCApi())); return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_from_utf8(view, abi.to.toDisambiguation(disambiguation), abi.to.toOffsetDisambiguation(offset_disambiguation)));
} }
/// Compare two ZonedDateTime instances /// Compare two ZonedDateTime instances
@ -240,9 +206,9 @@ pub fn toString(self: ZonedDateTime, allocator: std.mem.Allocator, opts: ToStrin
const result = abi.c.temporal_rs_ZonedDateTime_to_ixdtf_string( const result = abi.c.temporal_rs_ZonedDateTime_to_ixdtf_string(
self._inner, self._inner,
opts.offset_display.toCApi(), abi.to.displayOffset(opts.offset_display),
opts.time_zone_name.toCApi(), abi.to.toDisplayTimeZone(opts.time_zone_name),
opts.calendar_display.toCApi(), abi.to.calendarDisplay(opts.calendar_display),
abi.to_string_rounding_options_auto, abi.to_string_rounding_options_auto,
&write.inner, &write.inner,
); );
@ -287,7 +253,7 @@ pub fn withPlainTime(self: ZonedDateTime, time: ?PlainTime) !ZonedDateTime {
/// Create a new ZonedDateTime with a different time zone /// Create a new ZonedDateTime with a different time zone
pub fn withTimeZone(self: ZonedDateTime, time_zone: TimeZone) !ZonedDateTime { pub fn withTimeZone(self: ZonedDateTime, time_zone: TimeZone) !ZonedDateTime {
return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_with_timezone(self._inner, time_zone.toCApi())); return wrapZonedDateTime(abi.c.temporal_rs_ZonedDateTime_with_timezone(self._inner, abi.to.toTimeZone(time_zone)));
} }
// Property accessors // Property accessors

View file

@ -310,6 +310,51 @@ const dur = @import("Duration.zig");
const ins = @import("Instant.zig"); const ins = @import("Instant.zig");
pub const to = struct { pub const to = struct {
pub fn toTimeZone(val: anytype) c.TimeZone {
return val._inner;
}
pub fn toDisambiguation(d: anytype) c.Disambiguation {
return switch (d) {
.compatible => c.Disambiguation_Compatible,
.earlier => c.Disambiguation_Earlier,
.later => c.Disambiguation_Later,
.reject => c.Disambiguation_Reject,
};
}
pub fn toOffsetDisambiguation(o: anytype) c.OffsetDisambiguation {
return switch (o) {
.use_offset => c.OffsetDisambiguation_Use,
.prefer_offset => c.OffsetDisambiguation_Prefer,
.ignore_offset => c.OffsetDisambiguation_Ignore,
.reject => c.OffsetDisambiguation_Reject,
};
}
pub fn calendarDisplay(cd: anytype) c.DisplayCalendar {
return switch (cd) {
.auto => c.DisplayCalendar_Auto,
.always => c.DisplayCalendar_Always,
.never => c.DisplayCalendar_Never,
.critical => c.DisplayCalendar_Critical,
};
}
pub fn displayOffset(o: anytype) c.DisplayOffset {
return switch (o) {
.auto => c.DisplayOffset_Auto,
.never => c.DisplayOffset_Never,
};
}
pub fn toDisplayTimeZone(val: anytype) c.DisplayTimeZone {
return switch (val) {
.auto => c.DisplayTimeZone_Auto,
.never => c.DisplayTimeZone_Never,
.critical => c.DisplayTimeZone_Critical,
};
}
pub fn unit(opt: ?t.Unit) ?c.Unit { pub fn unit(opt: ?t.Unit) ?c.Unit {
return if (opt) |u| @as(c.Unit, @intCast(to.unitToCApi(u))) else null; return if (opt) |u| @as(c.Unit, @intCast(to.unitToCApi(u))) else null;
} }