refactor: use extractres wrapper

This commit is contained in:
Nurul Huda (Apon) 2026-01-27 00:59:55 +06:00
parent 9283912529
commit 76c4ee4228
No known key found for this signature in database
GPG key ID: 5D3F1DE2855A2F79
8 changed files with 71 additions and 64 deletions

View file

@ -114,13 +114,13 @@ pub fn subtract(self: Instant, duration: *Duration) !Instant {
/// Difference until another instant (Temporal.Instant.prototype.until). /// Difference until another instant (Temporal.Instant.prototype.until).
pub fn until(self: Instant, other: Instant, settings: DifferenceSettings) !Duration { pub fn until(self: Instant, other: Instant, settings: DifferenceSettings) !Duration {
const ptr = (abi.success(abi.c.temporal_rs_Instant_until(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_Instant_until(self._inner, other._inner, settings.toCApi()))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
/// Difference since another instant (Temporal.Instant.prototype.since). /// Difference since another instant (Temporal.Instant.prototype.since).
pub fn since(self: Instant, other: Instant, settings: DifferenceSettings) !Duration { pub fn since(self: Instant, other: Instant, settings: DifferenceSettings) !Duration {
const ptr = (abi.success(abi.c.temporal_rs_Instant_since(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_Instant_since(self._inner, other._inner, settings.toCApi()))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -180,13 +180,13 @@ pub fn toLocaleString(self: Instant, allocator: std.mem.Allocator) ![]u8 {
/// Convert to ZonedDateTime using built-in provider (Temporal.Instant.prototype.toZonedDateTimeISO). /// Convert to ZonedDateTime using built-in provider (Temporal.Instant.prototype.toZonedDateTimeISO).
pub fn toZonedDateTimeISO(self: Instant, zone: TimeZone) !ZonedDateTime { pub fn toZonedDateTimeISO(self: Instant, zone: TimeZone) !ZonedDateTime {
const ptr = (abi.success(abi.c.temporal_rs_Instant_to_zoned_date_time_iso(self._inner, zone._inner)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_Instant_to_zoned_date_time_iso(self._inner, zone._inner))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
/// Convert to ZonedDateTime using an explicit provider. /// Convert to ZonedDateTime using an explicit provider.
fn toZonedDateTimeIsoWithProvider(self: Instant, zone: TimeZone, provider: *const abi.c.Provider) !ZonedDateTime { fn toZonedDateTimeIsoWithProvider(self: Instant, zone: TimeZone, provider: *const abi.c.Provider) !ZonedDateTime {
const ptr = (abi.success(abi.c.temporal_rs_Instant_to_zoned_date_time_iso_with_provider(self._inner, zone._inner, provider)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_Instant_to_zoned_date_time_iso_with_provider(self._inner, zone._inner, provider))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -203,7 +203,7 @@ pub fn deinit(self: Instant) void {
// --- Helpers ----------------------------------------------------------------- // --- Helpers -----------------------------------------------------------------
fn wrapInstant(res: anytype) !Instant { fn wrapInstant(res: anytype) !Instant {
const ptr = (abi.success(res) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(res)) orelse return abi.TemporalError.Generic;
return .{ return .{
._inner = ptr, ._inner = ptr,
.epoch_milliseconds = abi.c.temporal_rs_Instant_epoch_milliseconds(ptr), .epoch_milliseconds = abi.c.temporal_rs_Instant_epoch_milliseconds(ptr),
@ -212,7 +212,7 @@ fn wrapInstant(res: anytype) !Instant {
} }
fn handleVoidResult(res: anytype) !void { fn handleVoidResult(res: anytype) !void {
_ = abi.success(res) orelse return error.TemporalError; _ = try abi.extractResult(res);
} }
fn defaultPrecision() abi.c.Precision { fn defaultPrecision() abi.c.Precision {
@ -274,8 +274,8 @@ test fromEpochNanoseconds {
try std.testing.expectEqual(max_ns, max_inst.epoch_nanoseconds); try std.testing.expectEqual(max_ns, max_inst.epoch_nanoseconds);
try std.testing.expectEqual(min_ns, min_inst.epoch_nanoseconds); try std.testing.expectEqual(min_ns, min_inst.epoch_nanoseconds);
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(max_ns + 1)); try std.testing.expectError(error.RangeError, Instant.fromEpochNanoseconds(max_ns + 1));
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(min_ns - 1)); try std.testing.expectError(error.RangeError, Instant.fromEpochNanoseconds(min_ns - 1));
} }
test from { test from {

View file

@ -50,7 +50,7 @@ pub fn init(year_val: i32, month_val: u8, day_val: u8) !PlainDate {
pub fn initWithCalendar(year_val: i32, month_val: u8, day_val: u8, calendar: []const u8) !PlainDate { pub fn initWithCalendar(year_val: i32, month_val: u8, day_val: u8, calendar: []const u8) !PlainDate {
const cal_view = abi.toDiplomatStringView(calendar); const cal_view = abi.toDiplomatStringView(calendar);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
return wrapPlainDate(abi.c.temporal_rs_PlainDate_try_new(year_val, month_val, day_val, cal_kind)); return wrapPlainDate(abi.c.temporal_rs_PlainDate_try_new(year_val, month_val, day_val, cal_kind));
} }
@ -104,12 +104,12 @@ pub fn subtract(self: PlainDate, duration: Duration) !PlainDate {
} }
pub fn until(self: PlainDate, other: PlainDate, settings: DifferenceSettings) !Duration { pub fn until(self: PlainDate, other: PlainDate, settings: DifferenceSettings) !Duration {
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_until(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_until(self._inner, other._inner, settings.toCApi()))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn since(self: PlainDate, other: PlainDate, settings: DifferenceSettings) !Duration { pub fn since(self: PlainDate, other: PlainDate, settings: DifferenceSettings) !Duration {
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_since(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_since(self._inner, other._inner, settings.toCApi()))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -122,35 +122,35 @@ pub fn with(self: PlainDate, fields: anytype) !PlainDate {
pub fn withCalendar(self: PlainDate, calendar: []const u8) !PlainDate { pub fn withCalendar(self: PlainDate, calendar: []const u8) !PlainDate {
const cal_view = abi.toDiplomatStringView(calendar); const cal_view = abi.toDiplomatStringView(calendar);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
const ptr = abi.c.temporal_rs_PlainDate_with_calendar(self._inner, cal_kind) orelse return error.TemporalError; const ptr = abi.c.temporal_rs_PlainDate_with_calendar(self._inner, cal_kind) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn toPlainDateTime(self: PlainDate, time: ?PlainTime) !PlainDateTime { pub fn toPlainDateTime(self: PlainDate, time: ?PlainTime) !PlainDateTime {
const time_ptr = if (time) |t| t._inner else null; const time_ptr = if (time) |t| t._inner else null;
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_to_plain_date_time(self._inner, time_ptr)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_to_plain_date_time(self._inner, time_ptr))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn toPlainMonthDay(self: PlainDate) !PlainMonthDay { pub fn toPlainMonthDay(self: PlainDate) !PlainMonthDay {
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_to_plain_month_day(self._inner)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_to_plain_month_day(self._inner))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn toPlainYearMonth(self: PlainDate) !PlainYearMonth { pub fn toPlainYearMonth(self: PlainDate) !PlainYearMonth {
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_to_plain_year_month(self._inner)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_to_plain_year_month(self._inner))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn toZonedDateTime(self: PlainDate, options: ToZonedDateTimeOptions) !ZonedDateTime { pub fn toZonedDateTime(self: PlainDate, options: ToZonedDateTimeOptions) !ZonedDateTime {
const tz_view = abi.toDiplomatStringView(options.time_zone); const tz_view = abi.toDiplomatStringView(options.time_zone);
const tz_result = abi.c.temporal_rs_TimeZone_try_from_str(tz_view); const tz_result = abi.c.temporal_rs_TimeZone_try_from_str(tz_view);
const time_zone = abi.success(tz_result) orelse return error.TemporalError; const time_zone = try abi.extractResult(tz_result);
const time_ptr = if (options.plain_time) |t| t._inner else null; const time_ptr = if (options.plain_time) |t| t._inner else null;
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_to_zoned_date_time(self._inner, time_zone, time_ptr)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_to_zoned_date_time(self._inner, time_zone, time_ptr))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -276,13 +276,13 @@ pub fn deinit(self: PlainDate) void {
} }
fn wrapPlainDate(res: anytype) !PlainDate { fn wrapPlainDate(res: anytype) !PlainDate {
const ptr = (abi.success(res) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(res)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
fn handleVoidResult(res: anytype) !void { fn handleVoidResult(res: anytype) !void {
_ = abi.success(res) orelse return error.TemporalError; _ = try abi.extractResult(res);
} }
test init { test init {

View file

@ -89,7 +89,7 @@ pub fn initWithCalendar(
) !PlainDateTime { ) !PlainDateTime {
const cal_view = abi.toDiplomatStringView(calendar); const cal_view = abi.toDiplomatStringView(calendar);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new( return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new(
year_val, year_val,
month_val, month_val,
@ -112,14 +112,14 @@ pub fn from(s: []const u8) !PlainDateTime {
fn fromUtf8(utf8: []const u8) !PlainDateTime { fn fromUtf8(utf8: []const u8) !PlainDateTime {
const view = abi.toDiplomatStringView(utf8); const view = abi.toDiplomatStringView(utf8);
const parsed = abi.c.temporal_rs_ParsedDateTime_from_utf8(view); const parsed = abi.c.temporal_rs_ParsedDateTime_from_utf8(view);
const ptr = abi.success(parsed) orelse return error.TemporalError; const ptr = try abi.extractResult(parsed);
return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_from_parsed(ptr)); return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_from_parsed(ptr));
} }
fn fromUtf16(utf16: []const u16) !PlainDateTime { fn fromUtf16(utf16: []const u16) !PlainDateTime {
const view = abi.toDiplomatString16View(utf16); const view = abi.toDiplomatString16View(utf16);
const parsed = abi.c.temporal_rs_ParsedDateTime_from_utf16(view); const parsed = abi.c.temporal_rs_ParsedDateTime_from_utf16(view);
const ptr = abi.success(parsed) orelse return error.TemporalError; const ptr = try abi.extractResult(parsed);
return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_from_parsed(ptr)); return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_from_parsed(ptr));
} }
@ -151,13 +151,13 @@ pub fn subtract(self: PlainDateTime, duration: Duration) !PlainDateTime {
pub fn until(self: PlainDateTime, other: PlainDateTime, options: DifferenceSettings) !Duration { pub fn until(self: PlainDateTime, other: PlainDateTime, options: DifferenceSettings) !Duration {
const result = abi.c.temporal_rs_PlainDateTime_until(self._inner, other._inner, options.toCApi()); const result = abi.c.temporal_rs_PlainDateTime_until(self._inner, other._inner, options.toCApi());
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn since(self: PlainDateTime, other: PlainDateTime, options: DifferenceSettings) !Duration { pub fn since(self: PlainDateTime, other: PlainDateTime, options: DifferenceSettings) !Duration {
const result = abi.c.temporal_rs_PlainDateTime_since(self._inner, other._inner, options.toCApi()); const result = abi.c.temporal_rs_PlainDateTime_since(self._inner, other._inner, options.toCApi());
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -291,7 +291,7 @@ pub fn with(self: PlainDateTime, partial: WithOptions) !PlainDateTime {
const cal_id_view = abi.c.temporal_rs_Calendar_identifier(calendar_ptr); const cal_id_view = abi.c.temporal_rs_Calendar_identifier(calendar_ptr);
const cal_view = abi.c.DiplomatStringView{ .data = cal_id_view.data, .len = cal_id_view.len }; const cal_view = abi.c.DiplomatStringView{ .data = cal_id_view.data, .len = cal_id_view.len };
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new( return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new(
new_year, new_year,
@ -310,8 +310,8 @@ pub fn with(self: PlainDateTime, partial: WithOptions) !PlainDateTime {
pub fn withCalendar(self: PlainDateTime, calendar: []const u8) !PlainDateTime { pub fn withCalendar(self: PlainDateTime, calendar: []const u8) !PlainDateTime {
const cal_view = abi.toDiplomatStringView(calendar); const cal_view = abi.toDiplomatStringView(calendar);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
const ptr = abi.c.temporal_rs_PlainDateTime_with_calendar(self._inner, cal_kind) orelse return error.TemporalError; const ptr = abi.c.temporal_rs_PlainDateTime_with_calendar(self._inner, cal_kind) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -328,7 +328,7 @@ pub fn withPlainTime(self: PlainDateTime, time: ?PlainTime) !PlainDateTime {
const cal_id_view = abi.c.temporal_rs_Calendar_identifier(calendar_ptr); const cal_id_view = abi.c.temporal_rs_Calendar_identifier(calendar_ptr);
const cal_view = abi.c.DiplomatStringView{ .data = cal_id_view.data, .len = cal_id_view.len }; const cal_view = abi.c.DiplomatStringView{ .data = cal_id_view.data, .len = cal_id_view.len };
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new( return wrapPlainDateTime(abi.c.temporal_rs_PlainDateTime_try_new(
self.year(), self.year(),
@ -359,14 +359,14 @@ pub fn toPlainTime(self: PlainDateTime) !PlainTime {
pub fn toZonedDateTime(self: PlainDateTime, options: ToZonedDateTimeOptions) !ZonedDateTime { pub fn toZonedDateTime(self: PlainDateTime, options: ToZonedDateTimeOptions) !ZonedDateTime {
const tz_view = abi.toDiplomatStringView(options.timeZone); const tz_view = abi.toDiplomatStringView(options.timeZone);
const tz_result = abi.c.temporal_rs_TimeZone_try_from_str(tz_view); const tz_result = abi.c.temporal_rs_TimeZone_try_from_str(tz_view);
const time_zone = abi.success(tz_result) orelse return error.TemporalError; const time_zone = try abi.extractResult(tz_result);
// Convert to PlainDate and PlainTime // Convert to PlainDate and PlainTime
const date = try self.toPlainDate(); const date = try self.toPlainDate();
const time = try self.toPlainTime(); const time = try self.toPlainTime();
// Use PlainDate's toZonedDateTime with the time component // Use PlainDate's toZonedDateTime with the time component
const ptr = (abi.success(abi.c.temporal_rs_PlainDate_to_zoned_date_time(date._inner, time_zone, time._inner)) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(abi.c.temporal_rs_PlainDate_to_zoned_date_time(date._inner, time_zone, time._inner))) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -420,7 +420,7 @@ pub fn deinit(self: *PlainDateTime) void {
} }
fn wrapPlainDateTime(res: anytype) !PlainDateTime { fn wrapPlainDateTime(res: anytype) !PlainDateTime {
const ptr = (abi.success(res) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(res)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }

View file

@ -26,7 +26,7 @@ pub const WithOptions = struct {
// Helper to wrap PlainMonthDay pointer // Helper to wrap PlainMonthDay pointer
fn wrapPlainMonthDay(result: anytype) !PlainMonthDay { fn wrapPlainMonthDay(result: anytype) !PlainMonthDay {
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -36,7 +36,7 @@ pub fn init(month_val: u8, day_val: u8, calendar: ?[]const u8) !PlainMonthDay {
const cal_kind = if (calendar) |cal| blk: { const cal_kind = if (calendar) |cal| blk: {
const cal_view = abi.toDiplomatStringView(cal); const cal_view = abi.toDiplomatStringView(cal);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
break :blk abi.success(cal_result) orelse return error.TemporalError; break :blk try abi.extractResult(cal_result);
} else abi.c.AnyCalendarKind_Iso; } else abi.c.AnyCalendarKind_Iso;
const overflow = abi.c.ArithmeticOverflow_Constrain; const overflow = abi.c.ArithmeticOverflow_Constrain;
@ -139,7 +139,7 @@ pub fn toPlainDate(self: PlainMonthDay, year: i32) !PlainDate {
}; };
const result = abi.c.temporal_rs_PlainMonthDay_to_plain_date(self._inner, partial_date); const result = abi.c.temporal_rs_PlainMonthDay_to_plain_date(self._inner, partial_date);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return PlainDate{ ._inner = ptr }; return PlainDate{ ._inner = ptr };
} }

View file

@ -24,7 +24,7 @@ pub const WithOptions = struct {
// Helper to wrap PlainTime pointer // Helper to wrap PlainTime pointer
fn wrapPlainTime(result: anytype) !PlainTime { fn wrapPlainTime(result: anytype) !PlainTime {
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return PlainTime{ ._inner = ptr }; return PlainTime{ ._inner = ptr };
} }
@ -83,14 +83,14 @@ pub fn subtract(self: PlainTime, duration: Duration) !PlainTime {
pub fn until(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration { pub fn until(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration {
const settings = options.toCApi(); const settings = options.toCApi();
const result = abi.c.temporal_rs_PlainTime_until(self._inner, other._inner, settings); const result = abi.c.temporal_rs_PlainTime_until(self._inner, other._inner, settings);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn since(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration { pub fn since(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration {
const settings = options.toCApi(); const settings = options.toCApi();
const result = abi.c.temporal_rs_PlainTime_since(self._inner, other._inner, settings); const result = abi.c.temporal_rs_PlainTime_since(self._inner, other._inner, settings);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }

View file

@ -34,7 +34,7 @@ pub const WithOptions = struct {
// Helper to wrap PlainYearMonth pointer // Helper to wrap PlainYearMonth pointer
fn wrapPlainYearMonth(result: anytype) !PlainYearMonth { fn wrapPlainYearMonth(result: anytype) !PlainYearMonth {
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -44,7 +44,7 @@ pub fn init(year_val: i32, month_val: u8, calendar: ?[]const u8) !PlainYearMonth
const cal_kind = if (calendar) |cal| blk: { const cal_kind = if (calendar) |cal| blk: {
const cal_view = abi.toDiplomatStringView(cal); const cal_view = abi.toDiplomatStringView(cal);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
break :blk abi.success(cal_result) orelse return error.TemporalError; break :blk try abi.extractResult(cal_result);
} else abi.c.AnyCalendarKind_Iso; } else abi.c.AnyCalendarKind_Iso;
const overflow = abi.c.ArithmeticOverflow_Constrain; const overflow = abi.c.ArithmeticOverflow_Constrain;
@ -97,14 +97,14 @@ pub fn subtract(self: PlainYearMonth, duration: Duration) !PlainYearMonth {
pub fn until(self: PlainYearMonth, other: PlainYearMonth, options: DifferenceSettings) !Duration { pub fn until(self: PlainYearMonth, other: PlainYearMonth, options: DifferenceSettings) !Duration {
const settings = options.toCApi(); const settings = options.toCApi();
const result = abi.c.temporal_rs_PlainYearMonth_until(self._inner, other._inner, settings); const result = abi.c.temporal_rs_PlainYearMonth_until(self._inner, other._inner, settings);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
pub fn since(self: PlainYearMonth, other: PlainYearMonth, options: DifferenceSettings) !Duration { pub fn since(self: PlainYearMonth, other: PlainYearMonth, options: DifferenceSettings) !Duration {
const settings = options.toCApi(); const settings = options.toCApi();
const result = abi.c.temporal_rs_PlainYearMonth_since(self._inner, other._inner, settings); const result = abi.c.temporal_rs_PlainYearMonth_since(self._inner, other._inner, settings);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -212,7 +212,7 @@ pub fn toPlainDate(self: PlainYearMonth, day: u8) !PlainDate {
}; };
const result = abi.c.temporal_rs_PlainYearMonth_to_plain_date(self._inner, partial_date); const result = abi.c.temporal_rs_PlainYearMonth_to_plain_date(self._inner, partial_date);
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return PlainDate{ ._inner = ptr }; return PlainDate{ ._inner = ptr };
} }

View file

@ -25,7 +25,7 @@ pub const TimeZone = struct {
pub fn init(id: []const u8) !TimeZone { pub fn init(id: []const u8) !TimeZone {
const view = abi.toDiplomatStringView(id); const view = abi.toDiplomatStringView(id);
const result = abi.c.temporal_rs_TimeZone_try_from_str(view); const result = abi.c.temporal_rs_TimeZone_try_from_str(view);
const tz = abi.success(result) orelse return error.TemporalError; const tz = try abi.extractResult(result);
return .{ ._inner = tz }; return .{ ._inner = tz };
} }
@ -119,7 +119,7 @@ pub const ToStringOptions = struct {
/// Helper function to wrap a C API result into a ZonedDateTime /// Helper function to wrap a C API result into a ZonedDateTime
fn wrapZonedDateTime(result: anytype) !ZonedDateTime { fn wrapZonedDateTime(result: anytype) !ZonedDateTime {
const ptr = (abi.success(result) orelse return error.TemporalError) orelse return error.TemporalError; const ptr = (try abi.extractResult(result)) orelse return abi.TemporalError.Generic;
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -170,7 +170,7 @@ pub fn getTimeZoneTransition(self: ZonedDateTime, direction: enum { next, previo
.previous => abi.c.TransitionDirection_Previous, .previous => abi.c.TransitionDirection_Previous,
}; };
const result = abi.c.temporal_rs_ZonedDateTime_get_time_zone_transition(self._inner, dir); const result = abi.c.temporal_rs_ZonedDateTime_get_time_zone_transition(self._inner, dir);
const maybe_ptr = abi.success(result) orelse return error.TemporalError; const maybe_ptr = try abi.extractResult(result);
if (maybe_ptr) |ptr| { if (maybe_ptr) |ptr| {
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -184,7 +184,7 @@ pub fn round(self: ZonedDateTime, options: RoundOptions) !ZonedDateTime {
/// Calculate duration since another ZonedDateTime /// Calculate duration since another ZonedDateTime
pub fn since(self: ZonedDateTime, other: ZonedDateTime, settings: DifferenceSettings) !Duration { pub fn since(self: ZonedDateTime, other: ZonedDateTime, settings: DifferenceSettings) !Duration {
const ptr = abi.success(abi.c.temporal_rs_ZonedDateTime_since(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError; const ptr = try abi.extractResult(abi.c.temporal_rs_ZonedDateTime_since(self._inner, other._inner, settings.toCApi()));
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -256,7 +256,7 @@ pub fn toString(self: ZonedDateTime, allocator: std.mem.Allocator, opts: ToStrin
/// Calculate duration until another ZonedDateTime /// Calculate duration until another ZonedDateTime
pub fn until(self: ZonedDateTime, other: ZonedDateTime, settings: DifferenceSettings) !Duration { pub fn until(self: ZonedDateTime, other: ZonedDateTime, settings: DifferenceSettings) !Duration {
const ptr = abi.success(abi.c.temporal_rs_ZonedDateTime_until(self._inner, other._inner, settings.toCApi())) orelse return error.TemporalError; const ptr = try abi.extractResult(abi.c.temporal_rs_ZonedDateTime_until(self._inner, other._inner, settings.toCApi()));
return .{ ._inner = ptr }; return .{ ._inner = ptr };
} }
@ -277,7 +277,7 @@ pub fn with(self: ZonedDateTime, allocator: std.mem.Allocator, fields: anytype)
pub fn withCalendar(self: ZonedDateTime, calendar: []const u8) !ZonedDateTime { pub fn withCalendar(self: ZonedDateTime, calendar: []const u8) !ZonedDateTime {
const cal_view = abi.toDiplomatStringView(calendar); const cal_view = abi.toDiplomatStringView(calendar);
const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view); const cal_result = abi.c.temporal_rs_AnyCalendarKind_parse_temporal_calendar_string(cal_view);
const cal_kind = abi.success(cal_result) orelse return error.TemporalError; const cal_kind = try abi.extractResult(cal_result);
const ptr = abi.c.temporal_rs_ZonedDateTime_with_calendar(self._inner, cal_kind); const ptr = abi.c.temporal_rs_ZonedDateTime_with_calendar(self._inner, cal_kind);
return .{ ._inner = ptr, .calendar_id = calendar }; return .{ ._inner = ptr, .calendar_id = calendar };
} }
@ -354,7 +354,7 @@ pub fn hour(self: ZonedDateTime) u8 {
pub fn hoursInDay(self: ZonedDateTime) !f64 { pub fn hoursInDay(self: ZonedDateTime) !f64 {
const result = abi.c.temporal_rs_ZonedDateTime_hours_in_day(self._inner); const result = abi.c.temporal_rs_ZonedDateTime_hours_in_day(self._inner);
return abi.success(result) orelse error.TemporalError; return try abi.extractResult(result);
} }
pub fn inLeapYear(self: ZonedDateTime) bool { pub fn inLeapYear(self: ZonedDateTime) bool {
@ -396,7 +396,7 @@ pub fn offset(self: ZonedDateTime, allocator: std.mem.Allocator) ![]u8 {
var write = abi.DiplomatWrite.init(allocator); var write = abi.DiplomatWrite.init(allocator);
defer write.deinit(); defer write.deinit();
const res = abi.c.temporal_rs_ZonedDateTime_offset(self._inner, &write.inner); const res = abi.c.temporal_rs_ZonedDateTime_offset(self._inner, &write.inner);
_ = abi.success(res) orelse return error.TemporalError; _ = try abi.extractResult(res);
return try write.toOwnedSlice(); return try write.toOwnedSlice();
} }

View file

@ -282,18 +282,25 @@ pub const TemporalError = error{
pub inline fn extractResult(result: anytype) TemporalError!Success(@TypeOf(result)) { pub inline fn extractResult(result: anytype) TemporalError!Success(@TypeOf(result)) {
if (success(result)) |value| return value; if (success(result)) |value| return value;
const err = result.unnamed_0.err; // Handle the error - check if the union has an 'err' field
// const message = if (fromOption(err.msg)) |sv| const Union = @FieldType(@TypeOf(result), "unnamed_0");
// fromDiplomatStringView(sv) if (@hasField(Union, "err")) {
// else const err = result.unnamed_0.err;
// "(no error message)"; // const message = if (fromOption(err.msg)) |sv|
// fromDiplomatStringView(sv)
// else
// "(no error message)";
return switch (err.kind) { return switch (err.kind) {
c.ErrorKind_Generic => TemporalError.Generic, c.ErrorKind_Generic => TemporalError.Generic,
c.ErrorKind_Type => TemporalError.TypeError, c.ErrorKind_Type => TemporalError.TypeError,
c.ErrorKind_Range => TemporalError.RangeError, c.ErrorKind_Range => TemporalError.RangeError,
c.ErrorKind_Syntax => TemporalError.SyntaxError, c.ErrorKind_Syntax => TemporalError.SyntaxError,
c.ErrorKind_Assert => @panic("temporal_rs assertion failed"), c.ErrorKind_Assert => @panic("temporal_rs assertion failed"),
else => TemporalError.Unknown, else => TemporalError.Unknown,
}; };
}
// Fallback for result types without detailed error information
return TemporalError.Generic;
} }