Skip to content

Commit d64c19b

Browse files
authored
Add from_partial for YearMonth/MonthDay FFI (#351)
1 parent 89c6d0a commit d64c19b

11 files changed

Lines changed: 92 additions & 3 deletions

File tree

src/builtins/core/month_day.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ impl PlainMonthDay {
221221
)
222222
}
223223

224+
/// Create a `PlainYearMonth` from a `PartialDate`
225+
pub fn from_partial(
226+
partial: PartialDate,
227+
overflow: Option<ArithmeticOverflow>,
228+
) -> TemporalResult<Self> {
229+
partial
230+
.calendar
231+
.month_day_from_partial(&partial, overflow.unwrap_or_default())
232+
}
233+
224234
/// Create a `PlainMonthDay` with the provided fields from a [`PartialDate`].
225235
pub fn with(
226236
&self,

src/builtins/core/year_month.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,11 @@ impl PlainYearMonth {
520520
/// Create a `PlainYearMonth` from a `PartialYearMonth`
521521
pub fn from_partial(
522522
partial: PartialYearMonth,
523-
overflow: ArithmeticOverflow,
523+
overflow: Option<ArithmeticOverflow>,
524524
) -> TemporalResult<Self> {
525-
partial.calendar.year_month_from_partial(&partial, overflow)
525+
partial
526+
.calendar
527+
.year_month_from_partial(&partial, overflow.unwrap_or_default())
526528
}
527529

528530
// Converts a UTF-8 encoded string into a `PlainYearMonth`.
@@ -562,7 +564,7 @@ impl PlainYearMonth {
562564
// [[Day]] field of the [[ISODate]] internal slot of the result.
563565
// 14. Set isoDate to ? CalendarYearMonthFromFields(calendar, result, constrain).
564566
// 15. Return ! CreateTemporalYearMonth(isoDate, calendar).
565-
PlainYearMonth::from_partial(partial, ArithmeticOverflow::Constrain)
567+
PlainYearMonth::from_partial(partial, Some(ArithmeticOverflow::Constrain))
566568
}
567569

568570
/// Returns the iso year value for this `YearMonth`.

temporal_capi/bindings/c/PlainMonthDay.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/c/PlainYearMonth.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PlainMonthDay.d.hpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PlainMonthDay.hpp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PlainYearMonth.d.hpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PlainYearMonth.hpp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/src/plain_date.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,34 @@ impl TryFrom<ffi::PartialDate<'_>> for temporal_rs::partial::PartialDate {
343343
})
344344
}
345345
}
346+
347+
impl TryFrom<ffi::PartialDate<'_>> for temporal_rs::partial::PartialYearMonth {
348+
type Error = TemporalError;
349+
fn try_from(other: ffi::PartialDate<'_>) -> Result<Self, TemporalError> {
350+
use temporal_rs::TinyAsciiStr;
351+
352+
let month_code = if other.month_code.is_empty() {
353+
None
354+
} else {
355+
Some(MonthCode::try_from_utf8(other.month_code.into()).map_err(TemporalError::from)?)
356+
};
357+
358+
let era = if other.era.is_empty() {
359+
None
360+
} else {
361+
Some(TinyAsciiStr::try_from_utf8(other.era.into()).map_err(|_| {
362+
TemporalError::from(
363+
temporal_rs::TemporalError::range().with_message("Invalid era code."),
364+
)
365+
})?)
366+
};
367+
Ok(Self {
368+
year: other.year.into(),
369+
month: other.month.into(),
370+
month_code,
371+
era_year: other.era_year.into(),
372+
era,
373+
calendar: temporal_rs::Calendar::new(other.calendar.into()),
374+
})
375+
}
376+
}

temporal_capi/src/plain_month_day.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ pub mod ffi {
3838
.map_err(Into::into)
3939
}
4040

41+
pub fn from_partial(
42+
partial: PartialDate,
43+
overflow: Option<ArithmeticOverflow>,
44+
) -> Result<Box<PlainMonthDay>, TemporalError> {
45+
temporal_rs::PlainMonthDay::from_partial(partial.try_into()?, overflow.map(Into::into))
46+
.map(|x| Box::new(PlainMonthDay(x)))
47+
.map_err(Into::into)
48+
}
49+
4150
pub fn with(
4251
&self,
4352
partial: PartialDate,

0 commit comments

Comments
 (0)