refactor: use extractres wrapper
This commit is contained in:
parent
ae74ee2aa7
commit
336dd46651
8 changed files with 71 additions and 64 deletions
|
|
@ -114,13 +114,13 @@ pub fn subtract(self: Instant, duration: *Duration) !Instant {
|
|||
|
||||
/// Difference until another instant (Temporal.Instant.prototype.until).
|
||||
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 };
|
||||
}
|
||||
|
||||
/// Difference since another instant (Temporal.Instant.prototype.since).
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -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).
|
||||
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 };
|
||||
}
|
||||
|
||||
/// Convert to ZonedDateTime using an explicit provider.
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -203,7 +203,7 @@ pub fn deinit(self: Instant) void {
|
|||
// --- Helpers -----------------------------------------------------------------
|
||||
|
||||
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 .{
|
||||
._inner = 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 {
|
||||
_ = abi.success(res) orelse return error.TemporalError;
|
||||
_ = try abi.extractResult(res);
|
||||
}
|
||||
|
||||
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(min_ns, min_inst.epoch_nanoseconds);
|
||||
|
||||
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(max_ns + 1));
|
||||
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(min_ns - 1));
|
||||
try std.testing.expectError(error.RangeError, Instant.fromEpochNanoseconds(max_ns + 1));
|
||||
try std.testing.expectError(error.RangeError, Instant.fromEpochNanoseconds(min_ns - 1));
|
||||
}
|
||||
|
||||
test from {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
const cal_view = abi.toDiplomatStringView(calendar);
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
@ -104,12 +104,12 @@ pub fn subtract(self: PlainDate, duration: Duration) !PlainDate {
|
|||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -122,35 +122,35 @@ pub fn with(self: PlainDate, fields: anytype) !PlainDate {
|
|||
pub fn withCalendar(self: PlainDate, calendar: []const u8) !PlainDate {
|
||||
const cal_view = abi.toDiplomatStringView(calendar);
|
||||
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 ptr = abi.c.temporal_rs_PlainDate_with_calendar(self._inner, cal_kind) 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 abi.TemporalError.Generic;
|
||||
|
||||
return .{ ._inner = ptr };
|
||||
}
|
||||
|
||||
pub fn toPlainDateTime(self: PlainDate, time: ?PlainTime) !PlainDateTime {
|
||||
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 };
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
pub fn toZonedDateTime(self: PlainDate, options: ToZonedDateTimeOptions) !ZonedDateTime {
|
||||
const tz_view = abi.toDiplomatStringView(options.time_zone);
|
||||
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 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 };
|
||||
}
|
||||
|
|
@ -276,13 +276,13 @@ pub fn deinit(self: PlainDate) void {
|
|||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
fn handleVoidResult(res: anytype) !void {
|
||||
_ = abi.success(res) orelse return error.TemporalError;
|
||||
_ = try abi.extractResult(res);
|
||||
}
|
||||
|
||||
test init {
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ pub fn initWithCalendar(
|
|||
) !PlainDateTime {
|
||||
const cal_view = abi.toDiplomatStringView(calendar);
|
||||
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(
|
||||
year_val,
|
||||
month_val,
|
||||
|
|
@ -112,14 +112,14 @@ pub fn from(s: []const u8) !PlainDateTime {
|
|||
fn fromUtf8(utf8: []const u8) !PlainDateTime {
|
||||
const view = abi.toDiplomatStringView(utf8);
|
||||
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));
|
||||
}
|
||||
|
||||
fn fromUtf16(utf16: []const u16) !PlainDateTime {
|
||||
const view = abi.toDiplomatString16View(utf16);
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
@ -151,13 +151,13 @@ pub fn subtract(self: PlainDateTime, duration: Duration) !PlainDateTime {
|
|||
|
||||
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 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 };
|
||||
}
|
||||
|
||||
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 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 };
|
||||
}
|
||||
|
||||
|
|
@ -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_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_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(
|
||||
new_year,
|
||||
|
|
@ -310,8 +310,8 @@ pub fn with(self: PlainDateTime, partial: WithOptions) !PlainDateTime {
|
|||
pub fn withCalendar(self: PlainDateTime, calendar: []const u8) !PlainDateTime {
|
||||
const cal_view = abi.toDiplomatStringView(calendar);
|
||||
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 ptr = abi.c.temporal_rs_PlainDateTime_with_calendar(self._inner, cal_kind) 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 abi.TemporalError.Generic;
|
||||
|
||||
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_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_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(
|
||||
self.year(),
|
||||
|
|
@ -359,14 +359,14 @@ pub fn toPlainTime(self: PlainDateTime) !PlainTime {
|
|||
pub fn toZonedDateTime(self: PlainDateTime, options: ToZonedDateTimeOptions) !ZonedDateTime {
|
||||
const tz_view = abi.toDiplomatStringView(options.timeZone);
|
||||
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
|
||||
const date = try self.toPlainDate();
|
||||
const time = try self.toPlainTime();
|
||||
|
||||
// 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 };
|
||||
}
|
||||
|
|
@ -420,7 +420,7 @@ pub fn deinit(self: *PlainDateTime) void {
|
|||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ pub const WithOptions = struct {
|
|||
|
||||
// Helper to wrap PlainMonthDay pointer
|
||||
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 };
|
||||
}
|
||||
|
|
@ -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_view = abi.toDiplomatStringView(cal);
|
||||
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;
|
||||
|
||||
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 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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ pub const WithOptions = struct {
|
|||
|
||||
// Helper to wrap PlainTime pointer
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -83,14 +83,14 @@ pub fn subtract(self: PlainTime, duration: Duration) !PlainTime {
|
|||
pub fn until(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration {
|
||||
const settings = options.toCApi();
|
||||
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 };
|
||||
}
|
||||
|
||||
pub fn since(self: PlainTime, other: PlainTime, options: DifferenceSettings) !Duration {
|
||||
const settings = options.toCApi();
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ pub const WithOptions = struct {
|
|||
|
||||
// Helper to wrap PlainYearMonth pointer
|
||||
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 };
|
||||
}
|
||||
|
|
@ -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_view = abi.toDiplomatStringView(cal);
|
||||
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;
|
||||
|
||||
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 {
|
||||
const settings = options.toCApi();
|
||||
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 };
|
||||
}
|
||||
|
||||
pub fn since(self: PlainYearMonth, other: PlainYearMonth, options: DifferenceSettings) !Duration {
|
||||
const settings = options.toCApi();
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -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 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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ pub const TimeZone = struct {
|
|||
pub fn init(id: []const u8) !TimeZone {
|
||||
const view = abi.toDiplomatStringView(id);
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +119,7 @@ pub const ToStringOptions = struct {
|
|||
|
||||
/// Helper function to wrap a C API result into a 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 };
|
||||
}
|
||||
|
||||
|
|
@ -170,7 +170,7 @@ pub fn getTimeZoneTransition(self: ZonedDateTime, direction: enum { next, previo
|
|||
.previous => abi.c.TransitionDirection_Previous,
|
||||
};
|
||||
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| {
|
||||
return .{ ._inner = ptr };
|
||||
}
|
||||
|
|
@ -184,7 +184,7 @@ pub fn round(self: ZonedDateTime, options: RoundOptions) !ZonedDateTime {
|
|||
|
||||
/// Calculate duration since another ZonedDateTime
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -256,7 +256,7 @@ pub fn toString(self: ZonedDateTime, allocator: std.mem.Allocator, opts: ToStrin
|
|||
|
||||
/// Calculate duration until another ZonedDateTime
|
||||
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 };
|
||||
}
|
||||
|
||||
|
|
@ -277,7 +277,7 @@ pub fn with(self: ZonedDateTime, allocator: std.mem.Allocator, fields: anytype)
|
|||
pub fn withCalendar(self: ZonedDateTime, calendar: []const u8) !ZonedDateTime {
|
||||
const cal_view = abi.toDiplomatStringView(calendar);
|
||||
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);
|
||||
return .{ ._inner = ptr, .calendar_id = calendar };
|
||||
}
|
||||
|
|
@ -354,7 +354,7 @@ pub fn hour(self: ZonedDateTime) u8 {
|
|||
|
||||
pub fn hoursInDay(self: ZonedDateTime) !f64 {
|
||||
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 {
|
||||
|
|
@ -396,7 +396,7 @@ pub fn offset(self: ZonedDateTime, allocator: std.mem.Allocator) ![]u8 {
|
|||
var write = abi.DiplomatWrite.init(allocator);
|
||||
defer write.deinit();
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -282,6 +282,9 @@ pub const TemporalError = error{
|
|||
pub inline fn extractResult(result: anytype) TemporalError!Success(@TypeOf(result)) {
|
||||
if (success(result)) |value| return value;
|
||||
|
||||
// Handle the error - check if the union has an 'err' field
|
||||
const Union = @FieldType(@TypeOf(result), "unnamed_0");
|
||||
if (@hasField(Union, "err")) {
|
||||
const err = result.unnamed_0.err;
|
||||
// const message = if (fromOption(err.msg)) |sv|
|
||||
// fromDiplomatStringView(sv)
|
||||
|
|
@ -296,4 +299,8 @@ pub inline fn extractResult(result: anytype) TemporalError!Success(@TypeOf(resul
|
|||
c.ErrorKind_Assert => @panic("temporal_rs assertion failed"),
|
||||
else => TemporalError.Unknown,
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback for result types without detailed error information
|
||||
return TemporalError.Generic;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue