Skip to content

Commit

Permalink
Add additional functions to uniffi-fixture-time (#1489)
Browse files Browse the repository at this point in the history
`to_string_timestamp` and `get_pre_epoch_timestamp` separate 2-way back
and forth conversion process, making it possible to test 1-way
conversion.

`to_string_timestamp` ensures that foreign language bindings lower the
timestamp correctly, and that foreign language bindings interpret the
timestamp the same way that Rust does.

`get_pre_epoch_timestamp` ensures that foreign language bindings lift
the timestamp correctly, and that Rust interprets the timestamp the same
way that foreign language bindings do.
  • Loading branch information
arg0d authored Mar 15, 2023
1 parent 5875834 commit ff3d639
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/uniffi-fixture-time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ name = "uniffi_chronological"
[dependencies]
uniffi = {path = "../../uniffi"}
thiserror = "1.0"
chrono = { version = "0.4.23", default-features = false, features = ["alloc", "std"] }

[build-dependencies]
uniffi = {path = "../../uniffi", features = ["build"] }
Expand Down
10 changes: 10 additions & 0 deletions fixtures/uniffi-fixture-time/src/chronological.udl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ enum ChronologicalError {
};

namespace chronological {
[Throws=ChronologicalError]
timestamp return_timestamp(timestamp a);

[Throws=ChronologicalError]
duration return_duration(duration a);

string to_string_timestamp(timestamp a);

timestamp get_pre_epoch_timestamp();

[Throws=ChronologicalError]
timestamp add(timestamp a, duration b);

Expand Down
22 changes: 22 additions & 0 deletions fixtures/uniffi-fixture-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use std::time::{Duration, SystemTime};

use chrono::offset::Utc;
use chrono::DateTime;

#[derive(Debug, thiserror::Error)]
pub enum ChronologicalError {
#[error("Time overflow on an operation with {a:?} and {b:?}")]
Expand All @@ -12,6 +15,25 @@ pub enum ChronologicalError {
TimeDiffError { a: SystemTime, b: SystemTime },
}

fn return_timestamp(a: SystemTime) -> Result<SystemTime> {
Ok(a)
}

fn return_duration(a: Duration) -> Result<Duration> {
Ok(a)
}

fn to_string_timestamp(a: SystemTime) -> String {
let datetime: DateTime<Utc> = a.into();
datetime.format("%Y-%m-%dT%H:%M:%S.%fZ").to_string()
}

fn get_pre_epoch_timestamp() -> SystemTime {
std::time::SystemTime::UNIX_EPOCH
.checked_sub(std::time::Duration::new(1, 1_000_000))
.unwrap()
}

fn add(a: SystemTime, b: Duration) -> Result<SystemTime> {
a.checked_add(b)
.ok_or(ChronologicalError::TimeOverflow { a, b })
Expand Down

0 comments on commit ff3d639

Please sign in to comment.