Skip to content

[capi] Add from_utf8/from_utf16 for OwnedRelativeTo #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion temporal_capi/bindings/c/OwnedRelativeTo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion temporal_capi/bindings/cpp/temporal_rs/OwnedRelativeTo.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions temporal_capi/bindings/cpp/temporal_rs/OwnedRelativeTo.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 39 additions & 17 deletions temporal_capi/src/zoned_date_time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(feature = "compiled_data")]
use crate::error::ffi::TemporalError;
#[cfg(feature = "compiled_data")]
use temporal_rs::options::RelativeTo;

#[diplomat::bridge]
#[diplomat::abi_rename = "temporal_rs_{0}"]
Expand Down Expand Up @@ -53,24 +55,27 @@ pub mod ffi {
}

impl OwnedRelativeTo {
pub fn try_from_str(source: &DiplomatStr) -> Result<Self, TemporalError> {
use temporal_rs::options::RelativeTo;
// Retained for compatability
// TODO remove in any version after 0.0.10
pub fn try_from_str(s: &DiplomatStr) -> Result<Self, TemporalError> {
Self::from_utf8(s)
}

pub fn from_utf8(s: &DiplomatStr) -> Result<Self, TemporalError> {
// TODO(#275) This should not need to check
let s =
core::str::from_utf8(source).map_err(|_| temporal_rs::TemporalError::range())?;

let converted = RelativeTo::try_from_str(s).map_err(Into::<TemporalError>::into)?;

match converted {
RelativeTo::PlainDate(d) => Ok(Self {
date: Some(Box::new(PlainDate(d))),
zoned: None,
}),
RelativeTo::ZonedDateTime(d) => Ok(Self {
zoned: Some(Box::new(ZonedDateTime(d))),
date: None,
}),
}
let s = core::str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;

super::RelativeTo::try_from_str(s)
.map(Into::into)
.map_err(Into::<TemporalError>::into)
}

pub fn from_utf16(s: &DiplomatStr16) -> Result<Self, TemporalError> {
// TODO(#275) This should not need to convert
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
super::RelativeTo::try_from_str(&s)
.map(Into::into)
.map_err(Into::<TemporalError>::into)
}

pub fn empty() -> Self {
Expand Down Expand Up @@ -483,3 +488,20 @@ impl From<ffi::RelativeTo<'_>> for Option<temporal_rs::options::RelativeTo> {
}
}
}

#[cfg(feature = "compiled_data")]
impl From<RelativeTo> for ffi::OwnedRelativeTo {
fn from(other: RelativeTo) -> Self {
use alloc::boxed::Box;
match other {
RelativeTo::PlainDate(d) => Self {
date: Some(Box::new(crate::plain_date::ffi::PlainDate(d))),
zoned: None,
},
RelativeTo::ZonedDateTime(d) => Self {
zoned: Some(Box::new(ffi::ZonedDateTime(d))),
date: None,
},
}
}
}