Skip to content

Commit 85c802c

Browse files
committed
Add string conversion methods to types
1 parent fd468a2 commit 85c802c

7 files changed

Lines changed: 128 additions & 0 deletions

File tree

temporal_capi/src/duration.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::error::ffi::TemporalError;
66
pub mod ffi {
77
use crate::error::ffi::TemporalError;
88
use diplomat_runtime::DiplomatOption;
9+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
10+
use std::str::{self, FromStr};
911

1012
#[diplomat::opaque]
1113
pub struct Duration(pub(crate) temporal_rs::Duration);
@@ -152,6 +154,23 @@ pub mod ffi {
152154
.map(|x| Box::new(Duration(x)))
153155
.map_err(Into::into)
154156
}
157+
158+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
159+
// TODO(#275) This should not need to validate
160+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
161+
temporal_rs::Duration::from_str(s)
162+
.map(|c| Box::new(Self(c)))
163+
.map_err(Into::into)
164+
}
165+
166+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
167+
// TODO(#275) This should not need to convert
168+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
169+
temporal_rs::Duration::from_str(&s)
170+
.map(|c| Box::new(Self(c)))
171+
.map_err(Into::into)
172+
}
173+
155174
pub fn is_time_within_range(&self) -> bool {
156175
self.0.is_time_within_range()
157176
}

temporal_capi/src/instant.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub mod ffi {
55
use crate::duration::ffi::{Duration, TimeDuration};
66
use crate::error::ffi::TemporalError;
77
use crate::options::ffi::{DifferenceSettings, RoundingOptions};
8+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
9+
use std::str::{self, FromStr};
810

911
#[diplomat::opaque]
1012
pub struct Instant(pub temporal_rs::Instant);
@@ -40,6 +42,22 @@ pub mod ffi {
4042
.map_err(Into::into)
4143
}
4244

45+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
46+
// TODO(#275) This should not need to validate
47+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
48+
temporal_rs::Instant::from_str(s)
49+
.map(|c| Box::new(Self(c)))
50+
.map_err(Into::into)
51+
}
52+
53+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
54+
// TODO(#275) This should not need to convert
55+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
56+
temporal_rs::Instant::from_str(&s)
57+
.map(|c| Box::new(Self(c)))
58+
.map_err(Into::into)
59+
}
60+
4361
pub fn add(&self, duration: &Duration) -> Result<Box<Self>, TemporalError> {
4462
self.0
4563
.add(duration.0)

temporal_capi/src/plain_date.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub mod ffi {
1515
use crate::plain_time::ffi::PlainTime;
1616
use crate::plain_year_month::ffi::PlainYearMonth;
1717
use diplomat_runtime::{DiplomatOption, DiplomatStrSlice, DiplomatWrite};
18+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1819
use std::fmt::Write;
20+
use std::str::{self, FromStr};
1921

2022
#[diplomat::opaque]
2123
pub struct PlainDate(pub(crate) temporal_rs::PlainDate);
@@ -78,6 +80,7 @@ pub mod ffi {
7880
.map(|x| Box::new(PlainDate(x)))
7981
.map_err(Into::into)
8082
}
83+
8184
pub fn with(
8285
&self,
8386
partial: PartialDate,
@@ -96,6 +99,22 @@ pub mod ffi {
9699
.map_err(Into::into)
97100
}
98101

102+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
103+
// TODO(#275) This should not need to validate
104+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
105+
temporal_rs::PlainDate::from_str(s)
106+
.map(|c| Box::new(Self(c)))
107+
.map_err(Into::into)
108+
}
109+
110+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
111+
// TODO(#275) This should not need to convert
112+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
113+
temporal_rs::PlainDate::from_str(&s)
114+
.map(|c| Box::new(Self(c)))
115+
.map_err(Into::into)
116+
}
117+
99118
pub fn iso_year(&self) -> i32 {
100119
self.0.iso_year()
101120
}

temporal_capi/src/plain_date_time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub mod ffi {
1515
use crate::plain_date::ffi::{PartialDate, PlainDate};
1616
use crate::plain_time::ffi::{PartialTime, PlainTime};
1717
use diplomat_runtime::DiplomatWrite;
18+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1819
use std::fmt::Write;
20+
use std::str::{self, FromStr};
1921

2022
#[diplomat::opaque]
2123
pub struct PlainDateTime(pub(crate) temporal_rs::PlainDateTime);
@@ -114,6 +116,22 @@ pub mod ffi {
114116
.map_err(Into::into)
115117
}
116118

119+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
120+
// TODO(#275) This should not need to validate
121+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
122+
temporal_rs::PlainDateTime::from_str(s)
123+
.map(|c| Box::new(Self(c)))
124+
.map_err(Into::into)
125+
}
126+
127+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
128+
// TODO(#275) This should not need to convert
129+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
130+
temporal_rs::PlainDateTime::from_str(&s)
131+
.map(|c| Box::new(Self(c)))
132+
.map_err(Into::into)
133+
}
134+
117135
pub fn iso_year(&self) -> i32 {
118136
self.0.iso_year()
119137
}

temporal_capi/src/plain_month_day.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ pub mod ffi {
99
use crate::plain_date::ffi::{PartialDate, PlainDate};
1010

1111
use diplomat_runtime::DiplomatWrite;
12+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1213
use std::fmt::Write;
14+
use std::str::{self, FromStr};
1315

1416
#[diplomat::opaque]
1517
pub struct PlainMonthDay(pub(crate) temporal_rs::PlainMonthDay);
@@ -44,6 +46,22 @@ pub mod ffi {
4446
.map_err(Into::into)
4547
}
4648

49+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
50+
// TODO(#275) This should not need to validate
51+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
52+
temporal_rs::PlainMonthDay::from_str(s)
53+
.map(|c| Box::new(Self(c)))
54+
.map_err(Into::into)
55+
}
56+
57+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
58+
// TODO(#275) This should not need to convert
59+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
60+
temporal_rs::PlainMonthDay::from_str(&s)
61+
.map(|c| Box::new(Self(c)))
62+
.map_err(Into::into)
63+
}
64+
4765
pub fn iso_year(&self) -> i32 {
4866
self.0.iso_year()
4967
}

temporal_capi/src/plain_time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ pub mod ffi {
99
ArithmeticOverflow, DifferenceSettings, RoundingMode, ToStringRoundingOptions, Unit,
1010
};
1111
use diplomat_runtime::{DiplomatOption, DiplomatWrite};
12+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1213
use std::fmt::Write;
14+
use std::str::{self, FromStr};
1315

1416
#[diplomat::opaque]
1517
pub struct PlainTime(pub(crate) temporal_rs::PlainTime);
@@ -75,6 +77,22 @@ pub mod ffi {
7577
.map_err(Into::into)
7678
}
7779

80+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
81+
// TODO(#275) This should not need to validate
82+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
83+
temporal_rs::PlainTime::from_str(s)
84+
.map(|c| Box::new(Self(c)))
85+
.map_err(Into::into)
86+
}
87+
88+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
89+
// TODO(#275) This should not need to convert
90+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
91+
temporal_rs::PlainTime::from_str(&s)
92+
.map(|c| Box::new(Self(c)))
93+
.map_err(Into::into)
94+
}
95+
7896
pub fn hour(&self) -> u8 {
7997
self.0.hour()
8098
}

temporal_capi/src/plain_year_month.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ pub mod ffi {
99
use crate::options::ffi::{ArithmeticOverflow, DifferenceSettings};
1010
use crate::plain_date::ffi::{PartialDate, PlainDate};
1111
use diplomat_runtime::DiplomatWrite;
12+
use diplomat_runtime::{DiplomatStr, DiplomatStr16};
1213
use std::fmt::Write;
14+
use std::str::{self, FromStr};
1315

1416
#[diplomat::opaque]
1517
pub struct PlainYearMonth(pub(crate) temporal_rs::PlainYearMonth);
@@ -44,6 +46,22 @@ pub mod ffi {
4446
.map_err(Into::into)
4547
}
4648

49+
pub fn from_utf8(s: &DiplomatStr) -> Result<Box<Self>, TemporalError> {
50+
// TODO(#275) This should not need to validate
51+
let s = str::from_utf8(s).map_err(|_| temporal_rs::TemporalError::range())?;
52+
temporal_rs::PlainYearMonth::from_str(s)
53+
.map(|c| Box::new(Self(c)))
54+
.map_err(Into::into)
55+
}
56+
57+
pub fn from_utf16(s: &DiplomatStr16) -> Result<Box<Self>, TemporalError> {
58+
// TODO(#275) This should not need to convert
59+
let s = String::from_utf16(s).map_err(|_| temporal_rs::TemporalError::range())?;
60+
temporal_rs::PlainYearMonth::from_str(&s)
61+
.map(|c| Box::new(Self(c)))
62+
.map_err(Into::into)
63+
}
64+
4765
pub fn iso_year(&self) -> i32 {
4866
self.0.iso_year()
4967
}

0 commit comments

Comments
 (0)